What is a Schema?
In Zod, a schema is an object that represents a data structure and its validation rules. Schemas serve two purposes:- Runtime validation - Check if data matches expected structure
- Type inference - Automatically generate TypeScript types
ZodType and has methods for parsing, validation, and transformation.
Creating Basic Schemas
Zod provides factory functions for all primitive types:String Schemas
String schemas support validation and transformation methods:From the source code at
packages/zod/src/v4/classic/schemas.ts:269-318, string schemas have properties like format, minLength, and maxLength that can be inspected.Number Schemas
Number validation with numeric constraints:Object Schemas
Define complex object structures:Array Schemas
Validate arrays with element constraints:Union and Intersection
Combine multiple schemas:Enum Schemas
Optional and Nullable
Schema Metadata
Add descriptions and custom metadata:Common Patterns
Schema Chaining
All schema methods return a new schema instance, allowing method chaining:Unknown Keys in Objects
Control how objects handle extra properties:Next Steps
- Learn about Parsing to validate data with schemas
- Explore Type Inference to extract TypeScript types
- Master Refinements for custom validation logic