Validation15M+/wkMIT

zod

Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defining data shapes and validating them at runtime. Zod's key in

Installation

npm
npm install zod
yarn
yarn add zod
pnpm
pnpm add zod

Import

ESM
import { z } from 'zod';

Quick Example

usage
import { z } from 'zod';

const UserSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  age: z.number().int().positive().optional(),
});

type User = z.infer<typeof UserSchema>;

const result = UserSchema.safeParse({ name: 'Alice', email: '[email protected]' });
if (result.success) console.log(result.data);

About zod

Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defining data shapes and validating them at runtime. Zod's key innovation is that schema definitions automatically infer TypeScript types — z.object({ name: z.string(), age: z.number() }) infers { name: string; age: number } — eliminating the duplication between type definitions and validation logic. Zod schemas are composable and support strings, numbers, booleans, dates, enums, arrays, objects, unions, intersections, tuples, records, maps, sets, promises, functions, and custom refinements. Each type includes built-in validators: z.string().email().min(1).max(100), z.number().int().positive().max(1000). The .transform() method converts validated data to a different shape, and .refine() adds custom validation logic with custom error messages. Zod supports schema composition through .merge(), .extend(), .pick(), .omit(), and .partial() methods that mirror TypeScript utility types. The library is used by tRPC for API input/output validation, React Hook Form for form validation, and many other tools that need runtime type checking. Zod's combination of type inference, composable schemas, and excellent error messages has made it the most popular validation library in the TypeScript ecosystem.

Quick Facts

Packagezod
CategoryValidation
Weekly Downloads15M+
LicenseMIT
Installnpm install zod

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.