talira

TypeScript-first schema validation with static type inference

open sourceMITself-hostablefree

Zod is a TypeScript-first validation library: you define a schema once, parse untrusted data with it, and get back a typed result, with z.infer giving you the static type so types and runtime checks don't drift apart. Schemas cover objects, unions, discriminated unions, refinements, transforms and async checks, and failures come back as structured issues with a path for each field. Zod 4 adds built-in JSON Schema conversion, ahead-of-time compilation for hot paths, and Zod Mini, a tree-shakable functional API for small frontend bundles. It implements Standard Schema, so form libraries such as React Hook Form and TanStack Form and HTTP clients such as Ky accept its schemas. It has no dependencies, runs in Node.js and browsers, and is MIT licensed.

Problems it solves

  • I write a TypeScript interface and a separate validation function for the same data and they drift apart
  • parse JSON from an API or a request body and get a typed object instead of any
  • validate request bodies in my Node.js API and reject bad input with field-by-field errors
  • check my environment variables at startup and fail fast with a clear message
  • generate JSON Schema from my TypeScript validation schemas for OpenAPI docs or LLM structured output
  • turn query string values into numbers, booleans and dates while validating them
  • share one validation schema between the browser form and the backend
  • validate webhook payloads that come in several shapes, picked by a type field
  • trim, normalise or convert values as part of validation and get the converted type back
  • run an async check, like whether a username is taken, inside a validation schema
  • my validation library adds too much to the frontend bundle
  • validation is a bottleneck when I parse large arrays of objects on a hot path
  • build a validator at runtime from a JSON Schema someone gives me
  • show users readable messages for each invalid field instead of one generic error

Features

  • schemas for primitives, objects, arrays, tuples, unions, discriminated unions, records, maps, sets, enums
  • parse / safeParse and async variants returning a typed deep clone
  • z.infer, z.input and z.output to extract static types
  • refinements, transforms, coercion and async validation
  • ZodError with structured issues and paths
  • z.toJSONSchema(): Draft 2020-12 by default, Draft-07, Draft-04 and OpenAPI 3.0 targets
  • z.fromJSONSchema() (experimental)
  • z.compile() ahead-of-time compiled fast path
  • Zod Mini (zod/mini): tree-shakable functional API
  • implements Standard Schema
View 3 more
  • zero dependencies; 2kb core bundle (gzipped)
  • zod/v3 and zod/v4 permalink subpaths; zod/v4/core for library authors
  • also published on JSR as @zod/zod

Limitations

  • Doesn't support TypeScript versions before 5.5: it's tested on TypeScript 5.5 and later and needs strict mode1
  • Doesn't convert every schema to JSON Schema: bigint, date, map, set, transform and custom types have no equivalent and throw by default2

Alternatives

The same job

These do what Zod does.

  1. Pydantic Data validation using Python type hints

Compare Zod with the top 3