Skip to main content

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 specifically
  • z.uuidv6() - Validates UUID v6 specifically
  • z.uuidv7() - Validates UUID v7 specifically
Location in source: ~/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 a ZodUUID that validates and returns strings.

Validation Rules

UUID Format

All UUID validators check for:
  1. Correct format: 8-4-4-4-12 hexadecimal digits
  2. Valid version bits in the 7th byte
  3. 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).
The UUID validator strictly enforces RFC 9562/4122 standards. UUIDs with variant 0 (NCS) or variant 2 (Microsoft) are rejected by z.uuid() but may be accepted by the less strict z.guid().