Skip to main content

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

For object unions with a discriminator field, use z.discriminatedUnion() for better performance and error messages.

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:
Order matters! Place more specific schemas before more general ones.

Error Messages

When all options fail, Zod reports all validation errors:
Customize the error message:

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: