Basic Usage
Create a schema that accepts any one of the provided options.Signature
ZodType[]
required
Array of schemas. Must contain at least two options. Input must match at least one.
string | ZodUnionParams
Optional error message (string) or configuration object.
Properties
readonly ZodType[]
Access the union’s option schemas.
Convenience Method
Use the.or() method as shorthand for unions:
Multiple Options
Unions can have more than two options:For literal unions, prefer
z.enum() or z.literal([...]) for better error messages.Complex Types
Object Unions
Nested Unions
Array Unions
Nullable and Optional
Nullable Types
Optional Types
Nullish Types
Type Inference
Parsing Behavior
Zod tries each option in order until one succeeds:Error Messages
When all options fail, Zod reports all validation errors:Common Patterns
API Response Types
Configuration Options
ID Types
XOR (Exclusive Union)
For exclusive unions where exactly one option must match:Transformations
Transform union values:Related
- z.discriminatedUnion() - For tagged unions (better performance)
- z.intersection() - For combining schemas (AND logic)
- z.enum() - For string literal unions
- z.xor() - For exclusive unions (exactly one match)