Zod
A TypeScript library for declaring validation schemas that check data at run time and also produce matching static types.
In more detail
You build a schema from pieces such as z.object, z.string().email() and z.number().int(), then parse data with it. A successful parse returns data you can trust and type; a failure returns a list of issues saying exactly which fields were wrong.
Examples
const UserSchema = z.object({ email: z.string().email(), age: z.number().int().min(18) })const Charge = z.object({ amount: z.number().positive(), currency: z.enum(['USD', 'EUR']) });A Next.js Server Action can return Zod's field errors to useActionState to show them in the form.Related terms
- validation schema — A description of what valid data looks like, checked by code while the program runs so bad data is rejected at the door.
- safeParse — The Zod method that checks data against a schema and returns a success flag with either the parsed data or the errors, instead of throwing.
- type inference — The compiler working out a type from the code itself, so you do not have to write it out by hand.
- type coercion — Converting a value from one type to another, such as the text "8080" into the number 8080.
- untrusted input — Any data a program did not create itself, including arguments, form fields, request bodies, file contents, file names and environment variables.
- Server Action security — Treating every Server Action as a public HTTP endpoint that anyone can call with any arguments.
Taught in
- End-to-End Type Safety: Shared Schemas & DTOs — Production Full-Stack Engineering
- Environment Configuration: Secrets & Multi-Stage — Production Full-Stack Engineering
- API Contract Architecture: OpenAPI 3.1 & Schema Truth — Production Full-Stack Engineering
- Type-Safe RPC Architectures: tRPC & Procedures — Production Full-Stack Engineering
- Optimistic UI: useOptimistic Rollbacks & Transitions — Next.js 15 & Modern App Router Architecture
- Server Action Security: Zod Validation & Auth Gates — Next.js 15 & Modern App Router Architecture