UUID Validator: Validate UUID Format, Version, and Variant
If you've built a public-facing API, you can't assume that data coming from a client is valid. One common example is an identifier. Whether an API expects a UUID v4 for a user profile or a UUID v7 for a transaction, malformed input should be rejected before it reaches application logic or the database.
A UUID validator provides that first line of defense. It checks whether an input conforms to the UUID representation and validation rules required by the application.
Validation is useful at the API boundary because it turns malformed input into a predictable client error, such as 400 Bad Request, instead of allowing invalid data to reach a database driver, ORM, or business-logic layer and potentially produce an unexpected exception.
What Does a UUID Validator Do?
At its simplest, a UUID validator checks whether a string is a valid representation of a UUID.
For the standard textual representation, that means checking things such as:
- The expected length and structure
- The position of the hyphens
- Whether the characters are valid hexadecimal digits
- Whether the UUID uses an expected variant
- Whether the version field contains an allowed UUID version
A UUID is a 128-bit identifier, but the meaning of individual bits depends on the UUID version.
For example, UUID v4 uses its version-specific fields for random data, while UUID v7 contains a timestamp along with additional generation data.
A validator can therefore do more than simply check whether a string looks like hexadecimal text.
Who Uses UUID Validation?
UUID validation is useful anywhere an application accepts UUIDs from an external or untrusted source.
- Backend engineers use validation libraries and request schemas to verify UUID fields before processing API requests.
- QA and penetration testers use invalid UUIDs to test whether APIs handle malformed input correctly without producing unexpected errors or leaking implementation details.
- Database administrators benefit from application-level validation that prevents obviously invalid identifiers from reaching database operations.
- Frontend developers can perform client-side validation for early feedback, although server-side validation should remain authoritative.
The important distinction is that validation should happen at the application boundary. A client-side check is useful for user experience, but it cannot be trusted as a security control.
How Is UUID Validation Used?
A typical API request might contain:
{
"user_id": "550e8400-e29b-41d4-a716-446655440000"
}
The server validates user_id before passing it to the application or database layer.
A simplified request flow looks like this:
Client
|
v
Request validation
|
+-- Invalid UUID --> 400 Bad Request
|
v
Application logic
|
v
Database
The goal is not to guarantee that the UUID identifies a real record. Validation only establishes that the value conforms to the UUID rules being checked.
For example, a syntactically valid UUID may still:
- Not exist in the database
- Refer to a deleted record
- Belong to another user
- Be unauthorized for the current request
Those are application-level concerns that must be handled separately.
How Does a UUID Validator Work?
For the canonical textual representation, a UUID is normally written as 36 characters:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
That consists of 32 hexadecimal characters and 4 hyphens.
A validator can perform several checks.
- Format check: It verifies the expected
8-4-4-4-12structure of the canonical textual representation. - Character check: It verifies that the hexadecimal positions contain valid hexadecimal characters (
0-9,a-f, orA-F). - Version check: It examines the version field when the application requires a specific UUID version, such as v4 or v7.
- Variant check: It verifies that the UUID uses the expected UUID variant defined by the applicable standard.
A simple format check can often be performed with a regular expression. A version-aware validator may additionally inspect the version and variant fields.
It is worth distinguishing format validation from UUID semantics. A regular expression can determine whether a string has the expected shape, but it does not necessarily provide all of the validation rules an application may require.
What Does the UUID Version Mean?
The UUID version is encoded in four bits of the UUID.
In the canonical string representation, the version appears as the first hexadecimal digit of the third group.
For example:
550e8400-e29b-41d4-a716-446655440000
^
version = 4
A UUID v4 therefore has 4 in the version position.
A UUID v7 has 7 in the same position:
0190c6c5-7b2a-7abc-8def-123456789abc
^
version = 7
The version tells software how the UUID's version-specific fields should be interpreted.
A general UUID validator can accept multiple defined versions. A version-specific validator can require one particular version.
What Is the UUID Variant?
The variant identifies how the UUID's fields are laid out and interpreted.
For the UUID variant used by the current RFC UUID formats, the relevant bits begin with 10 in binary. In the standard textual representation, this normally produces a hexadecimal value beginning with:
8
9
a
b
The variant is located in the first hexadecimal digit of the fourth group.
For example:
550e8400-e29b-41d4-a716-446655440000
^
variant = a
The terminology here is worth getting right: RFC 9562 is the current IETF UUID specification and obsoletes RFC 4122. RFC 4122 terminology is still widely encountered in software documentation, but new UUID documentation should generally refer to RFC 9562.
Relevant UUID Internals
A UUID always contains 128 bits, but the meaning of those bits changes according to the UUID version.
It is therefore misleading to describe every UUID as having the same fields such as "time-low," "time-mid," and "node."
A more useful view for validation is:
| Field | Size | Purpose |
|---|---|---|
| Version | 4 bits | Identifies the UUID version |
| Variant | 2 or more bits | Identifies the UUID layout/variant |
| Remaining bits | Version-dependent | Timestamp, randomness, counters, namespace data, or other version-specific information |
For example, UUID v4 uses 122 bits for random data after accounting for its version and variant bits.
UUID v7 uses a 48-bit Unix timestamp in milliseconds, followed by version-specific and generation fields.
UUID v1 uses a different time-based layout and historically includes a node identifier.
This is why a validator should not assume that every UUID has the same internal field meanings.
What Does a UUID Validator Not Check?
A valid UUID is not necessarily a valid application identifier.
For example:
550e8400-e29b-41d4-a716-446655440000
may be a structurally valid UUID but still have no corresponding record in the database.
A validator generally does not determine:
- Whether the UUID exists
- Whether it is unique within your database
- Who owns the resource
- Whether the caller is authorized to access it
- Whether the UUID was generated by your application
- Whether the UUID represents a valid business object
Those checks belong elsewhere in the application.
For example, after UUID validation, an API might still need to perform:
1. Validate UUID format
2. Authenticate the caller
3. Check authorization
4. Look up the resource
5. Perform the requested operation
This separation is important. UUID validation is input validation; it is not authentication or authorization.