Skip to main content

What is a Schema?

In Zod, a schema is an object that represents a data structure and its validation rules. Schemas serve two purposes:
  1. Runtime validation - Check if data matches expected structure
  2. Type inference - Automatically generate TypeScript types
Every schema is an instance of 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:
Zod v4 does NOT accept infinite values by default - all numbers must be finite.

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:
Each method call creates a new schema instance. The original schema remains unchanged.

Unknown Keys in Objects

Control how objects handle extra properties:

Next Steps