Zod
npm
v3.23.8 34.2k stars
12M/week
Updated 2024-12-15
TypeScript-first schema validation with static type inference. Zod eliminates duplicate type declarations, providing runtime validation while keeping your types in sync.
validation typescript schema type-safe
Installation
npm install zod Key Features
Zero dependencies
Works in Node.js and browsers
Tiny bundle size (8kb minified)
Immutable methods
Functional approach
Plain JavaScript support
Usage Example
import { z } from 'zod';
// Define a schema
const UserSchema = z.object({
name: z.string().min(2),
email: z.string().email(),
age: z.number().min(18).optional(),
});
// Parse and validate
const user = UserSchema.parse({
name: "John Doe",
email: "[email protected]",
age: 25,
});
// TypeScript type is automatically inferred
type User = z.infer<typeof UserSchema>;