Overview
Zod provides functions for validating UUIDs (Universally Unique Identifiers) according to RFC 9562/4122:z.uuid()- Validates any UUID (versions 1-8)z.uuidv4()- Validates UUID v4 specificallyz.uuidv6()- Validates UUID v6 specificallyz.uuidv7()- Validates UUID v7 specifically
~/workspace/source/packages/zod/src/v4/classic/schemas.ts:483-501
z.uuid()
Validates UUIDs of any version (1-8) with RFC-compliant variant bits.Basic Usage
Valid UUID Examples
Invalid UUID Examples
Custom Error Message
z.uuidv4()
Validates UUID version 4 specifically (random UUIDs).Basic Usage
Example
z.uuidv6()
Validates UUID version 6 (time-ordered UUIDs).Basic Usage
UUID v6 is designed to be time-ordered and sortable, making it useful for database primary keys and distributed systems.
z.uuidv7()
Validates UUID version 7 (Unix timestamp-based UUIDs).Basic Usage
UUID v7 embeds a Unix timestamp in the first 48 bits, making it both time-ordered and compatible with existing UUID infrastructure. It’s recommended for new applications requiring sortable identifiers.
Parameters
All UUID functions accept an optional parameter:string: Custom error message- Params object:
message?: string: Custom error message
Examples
Database ID Validation
API Request Validation
Migration from Legacy IDs
Version-Specific Validation
Return Type
All UUID schemas return aZodUUID that validates and returns strings.
Validation Rules
UUID Format
All UUID validators check for:- Correct format:
8-4-4-4-12hexadecimal digits - Valid version bits in the 7th byte
- Valid variant bits in the 9th byte (RFC 9562/4122 compliant)
Version-Specific Rules
- v4: Version bits =
0100(4) - v6: Version bits =
0110(6) - v7: Version bits =
0111(7)
Variant Bits
All versions require RFC-compliant variant bits:10xx in binary (8, 9, A, or B in hex).
Related Methods
- z.guid() - Less strict GUID validation
- z.cuid() - Collision-resistant unique IDs
- z.nanoid() - Compact unique IDs