Zod
✨ https://zod.dev ✨
TypeScript-first schema validation with static type inference
> Zod 3.23 is out! View the [release notes](https://github.com/colinhacks/zod/releases/tag/v3.23.0).
> These docs have been translated into [Chinese](./README_ZH.md).
## Table of contents
- [Table of contents](#table-of-contents)
- [Introduction](#introduction)
- [Sponsors](#sponsors)
- [Gold](#gold)
- [Silver](#silver)
- [Bronze](#bronze)
- [Copper](#copper)
- [Ecosystem](#ecosystem)
- [Resources](#resources)
- [API libraries](#api-libraries)
- [Form integrations](#form-integrations)
- [Zod to X](#zod-to-x)
- [X to Zod](#x-to-zod)
- [Mocking](#mocking)
- [Powered by Zod](#powered-by-zod)
- [Utilities for Zod](#utilities-for-zod)
- [Installation](#installation)
- [Requirements](#requirements)
- [From `npm` (Node/Bun)](#from-npm-nodebun)
- [From `deno.land/x` (Deno)](#from-denolandx-deno)
- [Basic usage](#basic-usage)
- [Primitives](#primitives)
- [Coercion for primitives](#coercion-for-primitives)
- [Literals](#literals)
- [Strings](#strings)
- [Datetimes](#datetimes)
- [Dates](#dates)
- [Times](#times)
- [IP addresses](#ip-addresses)
- [Numbers](#numbers)
- [BigInts](#bigints)
- [NaNs](#nans)
- [Booleans](#booleans)
- [Dates](#dates)
- [Zod enums](#zod-enums)
- [Native enums](#native-enums)
- [Optionals](#optionals)
- [Nullables](#nullables)
- [Objects](#objects)
- [`.shape`](#shape)
- [`.keyof`](#keyof)
- [`.extend`](#extend)
- [`.merge`](#merge)
- [`.pick/.omit`](#pickomit)
- [`.partial`](#partial)
- [`.deepPartial`](#deeppartial)
- [`.required`](#required)
- [`.passthrough`](#passthrough)
- [`.strict`](#strict)
- [`.strip`](#strip)
- [`.catchall`](#catchall)
- [Arrays](#arrays)
- [`.element`](#element)
- [`.nonempty`](#nonempty)
- [`.min/.max/.length`](#minmaxlength)
- [Tuples](#tuples)
- [Unions](#unions)
- [Discriminated unions](#discriminated-unions)
- [Records](#records)
- [Record key type](#record-key-type)
- [Maps](#maps)
- [Sets](#sets)
- [Intersections](#intersections)
- [Recursive types](#recursive-types)
- [ZodType with ZodEffects](#zodtype-with-zodeffects)
- [JSON type](#json-type)
- [Cyclical objects](#cyclical-objects)
- [Promises](#promises)
- [Instanceof](#instanceof)
- [Functions](#functions)
- [Preprocess](#preprocess)
- [Custom schemas](#custom-schemas)
- [Schema methods](#schema-methods)
- [`.parse`](#parse)
- [`.parseAsync`](#parseasync)
- [`.safeParse`](#safeparse)
- [`.safeParseAsync`](#safeparseasync)
- [`.refine`](#refine)
- [Arguments](#arguments)
- [Customize error path](#customize-error-path)
- [Asynchronous refinements](#asynchronous-refinements)
- [Relationship to transforms](#relationship-to-transforms)
- [`.superRefine`](#superrefine)
- [Abort early](#abort-early)
- [Type refinements](#type-refinements)
- [`.transform`](#transform)
- [Chaining order](#chaining-order)
- [Validating during transform](#validating-during-transform)
- [Relationship to refinements](#relationship-to-refinements)
- [Async transforms](#async-transforms)
- [`.default`](#default)
- [`.describe`](#describe)
- [`.catch`](#catch)
- [`.optional`](#optional)
- [`.nullable`](#nullable)
- [`.nullish`](#nullish)
- [`.array`](#array)
- [`.promise`](#promise)
- [`.or`](#or)
- [`.and`](#and)
- [`.brand`](#brand)
- [`.readonly`](#readonly)
- [`.pipe`](#pipe)
- [You can use `.pipe()` to fix common issues with `z.coerce`.](#you-can-use-pipe-to-fix-common-issues-with-zcoerce)
- [Guides and concepts](#guides-and-concepts)
- [Type inference](#type-inference)
- [Writing generic functions](#writing-generic-functions)
- [Constraining allowable inputs](#constraining-allowable-inputs)
- [Error handling](#error-handling)
- [Error formatting](#error-formatting)
- [Comparison](#comparison)
- [Joi](#joi)
- [Yup](#yup)
- [io-ts](#io-ts)
- [Runtypes](#runtypes)
- [Ow](#ow)
- [Changelog](#changelog)
## Introduction
Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type, from a simple `string` to a complex nested object.
Zod is designed to be as developer-friendly as possible. The goal is to eliminate duplicative type declarations. With Zod, you declare a validator _once_ and Zod will automatically infer the static TypeScript type. It's easy to compose simpler types into complex data structures.
Some other great aspects:
- Zero dependencies
- Works in Node.js and all modern browsers
- Tiny: 8kb minified + zipped
- Immutable: methods (e.g. `.optional()`) return a new instance
- Concise, chainable interface
- Functional approach: [parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)
- Works with plain JavaScript too! You don't need to use TypeScript.
### Sponsors
Sponsorship at any level is appreciated and encouraged. For individual developers, consider the [Cup of Coffee tier](https://github.com/sponsors/colinhacks). If you built a paid product using Zod, consider one of the [podium tiers](https://github.com/sponsors/colinhacks).
#### Platinum
> [Email me](mailto:colin@colinhacks.com) to discuss sponsoring Zod at this level.
#### Gold
#### Silver
#### Bronze
Brandon Bayer |
Jiří Brabec |
Alex Johansson |
Fungible Systems |
Adaptable |
Avana Wallet |
Jason Lengstorf |
Global Illumination, Inc. |
MasterBorn |
Ryan Palmer |
Michael Sweeney |
Nextbase |
Remotion |
Connor Sinnott |
Mohammad-Ali A'râbi |
Supatool |
Social Crow |
### Ecosystem
There are a growing number of tools that are built atop or support Zod natively! If you've built a tool or library on top of Zod, tell me about it [on Twitter](https://twitter.com/colinhacks) or [start a Discussion](https://github.com/colinhacks/zod/discussions). I'll add it below and tweet it out.
#### Resources
- [Total TypeScript Zod Tutorial](https://www.totaltypescript.com/tutorials/zod) by [@mattpocockuk](https://twitter.com/mattpocockuk)
- [Fixing TypeScript's Blindspot: Runtime Typechecking](https://www.youtube.com/watch?v=rY_XqfSHock) by [@jherr](https://twitter.com/jherr)
#### API libraries
- [`tRPC`](https://github.com/trpc/trpc): Build end-to-end typesafe APIs without GraphQL.
- [`@anatine/zod-nestjs`](https://github.com/anatine/zod-plugins/tree/main/packages/zod-nestjs): Helper methods for using Zod in a NestJS project.
- [`zod-endpoints`](https://github.com/flock-community/zod-endpoints): Contract-first strictly typed endpoints with Zod. OpenAPI compatible.
- [`zhttp`](https://github.com/evertdespiegeleer/zhttp): An OpenAPI compatible, strictly typed http library with Zod input and response validation.
- [`domain-functions`](https://github.com/SeasonedSoftware/domain-functions/): Decouple your business logic from your framework using composable functions. With first-class type inference from end to end powered by Zod schemas.
- [`@zodios/core`](https://github.com/ecyrbe/zodios): A typescript API client with runtime and compile time validation backed by axios and zod.
- [`express-zod-api`](https://github.com/RobinTail/express-zod-api): Build Express-based APIs with I/O schema validation and custom middlewares.
- [`tapiduck`](https://github.com/sumukhbarve/monoduck/blob/main/src/tapiduck/README.md): End-to-end typesafe JSON APIs with Zod and Express; a bit like tRPC, but simpler.
- [`koa-zod-router`](https://github.com/JakeFenley/koa-zod-router): Create typesafe routes in Koa with I/O validation using Zod.
#### Form integrations
- [`react-hook-form`](https://github.com/react-hook-form/resolvers#zod): A first-party Zod resolver for React Hook Form.
- [`zod-validation-error`](https://github.com/causaly/zod-validation-error): Generate user-friendly error messages from `ZodError`s.
- [`zod-formik-adapter`](https://github.com/robertLichtnow/zod-formik-adapter): A community-maintained Formik adapter for Zod.
- [`react-zorm`](https://github.com/esamattis/react-zorm): Standalone `