Zod
Complete reference for Zod — the TypeScript-first schema validation library with static type inference
Installation & Setup
Install Zod and start defining schemas
Install & Import
Install Zod and import it into your project
Primitive Schemas
Built-in schemas for JavaScript primitive types
Primitive Types
Schemas for strings, numbers, booleans, dates, and special types
String Validation
String-specific validators and format checks
String Validators
Length, format, and pattern validators for strings
Number Validation
Number-specific validators for ranges and integer constraints
Number Validators
Range, integer, and sign validators for numbers
Object Schemas
Define and manipulate object shapes with z.object()
Defining Objects
Create object schemas with required and optional properties
Object Methods
Transform existing object schemas with extend, pick, omit, partial
Arrays, Tuples, Sets & Maps
Schemas for collection types
Collection Schemas
Define arrays, tuples, sets, maps, and records
Unions, Intersections & Discriminated Unions
Combine multiple schemas with unions and intersections
Combining Schemas
Union, intersection, and discriminated union patterns
Enums & Literals
Define schemas for fixed sets of values
Enum & Literal Schemas
Restrict values to a known set with z.enum() and z.literal()
Optional, Nullable, Default & Catch
Handle missing values, defaults, and fallbacks
Handling Missing Values
Optional, nullable, default values, and catch fallbacks
Parsing & Error Handling
Parse data and handle validation errors
parse() vs safeParse()
Throwing and non-throwing parse methods
Customizing Error Messages
Set custom error messages on schemas and validators
Refinements & Transforms
Custom validation logic and data transformations
refine() & superRefine()
Add custom validation logic to any schema
transform(), pipe() & coerce
Convert and transform data during validation
Type Inference
Extract TypeScript types from Zod schemas
z.infer, z.input & z.output
Use schemas as the single source of truth for runtime and types