Skip to main content

Zod

What is Zodโ€‹

Zod is a TypeScript-first schema declaration and validation library. It is MIT licensed, currently at version 4.x, and has ~30M+ weekly npm downloads with 36k+ GitHub stars. It has zero dependencies and works across Node.js, browsers, Deno, Bun, and Cloudflare Workers. Bundle size is ~11 kB gzipped, with Zod Mini offering a ~2 kB core for bundle-sensitive contexts.

Why we recommend itโ€‹

  • TypeScript type inference โ€” schemas automatically infer static types via z.infer<typeof schema>, eliminating the need to maintain types and validation logic separately.
  • Zero dependencies โ€” nothing extra enters your dependency tree.
  • Composable API โ€” schemas are chainable and composable with .extend(), .merge(), .pick(), .omit(), .partial(), and more.
  • Rich type support โ€” strings, numbers, objects, arrays, unions, discriminated unions, recursive types, tuples, records, and custom refinements/transforms.
  • Structured errors โ€” ZodError objects include path information, making it straightforward to map errors to form fields or API response shapes.
  • Broad ecosystem integration โ€” first-class support in React Hook Form, tRPC, TanStack Router, Conform, and Next.js Server Actions.

Why over alternativesโ€‹

  • vs Yup โ€” Yup's TypeScript inference was added retroactively and is less reliable. It coerces values by default โ€” a frequent source of subtle bugs.
  • vs Joi โ€” Joi offers no TypeScript type inference, has a large bundle (~150 kB), and is primarily designed for Node.js server environments.
  • vs io-ts โ€” io-ts has a steep learning curve, requires the fp-ts library, and is less ergonomic for common validation tasks.
  • vs class-validator โ€” Decorator-based validation requires classes and reflect-metadata, which conflicts with functional React patterns and tree-shaking.

When to useโ€‹

  • Form validation (paired with React Hook Form or Conform).
  • API response validation โ€” verify that external data matches expected shapes at runtime.
  • Environment variable parsing โ€” fail fast on startup if required config is missing.
  • tRPC input/output schemas.
  • Shared client/server contracts โ€” define a schema once, use it for both runtime validation and TypeScript types.
  • Config file parsing.
  • LLM structured output โ€” constrain model responses to typed schemas using Zod 4's built-in z.toJSONSchema() with OpenAI, Anthropic, and Google Gemini. See Structured Output with Zod for provider-specific patterns and the Vercel AI SDK Output.object() abstraction.

When NOT to useโ€‹

  • Performance-critical bulk data processing โ€” if you're validating thousands of JSON objects per second (e.g., streaming pipelines), consider Ajv which compiles schemas to optimized code.
  • Projects not using TypeScript โ€” Zod's primary value proposition is type inference. In plain JavaScript projects, the ergonomic advantage shrinks considerably.
  • JSON Schema as source of truth โ€” if your validation schemas must be serializable JSON Schema (e.g., for OpenAPI specs), consider TypeBox or Ajv directly.
  • Extremely bundle-sensitive apps โ€” if every kilobyte counts and you need minimal validation, consider Valibot which offers a tree-shakeable, function-based API.
tip

Zod pairs naturally with TanStack Query โ€” validate API responses at the boundary before they enter your cache, catching contract drift early.

Resourcesโ€‹