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 install zod
yarn add zod
pnpm add zod
Import
import { z } from 'zod';Quick Example
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
| Package | zod |
| Category | Validation |
| Weekly Downloads | 15M+ |
| License | MIT |
| Install | npm install zod |
Related Packages
Yup is a schema validation library for JavaScript that provides a declarative, chainable API for def…
Joi is the most powerful schema description language and data validator for JavaScript, originally c…
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScri…
Valibot is a modular schema validation library for TypeScript that achieves exceptionally small bund…
TypeBox is a JSON Schema type builder that creates in-memory JSON Schema objects that can be statica…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.