Zod — TypeScript-First Schema Validation
TypeScript-first schema declaration and validation library. Define a schema once, get type inference, runtime validation, and serialization. The standard for TypeScript data validation. 35,000+ stars.
Installation avec revue préalable
Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.
npx -y tokrepo@latest install 5328f7ec-37d8-47bc-afb5-034b66d1537d --target codexDry-run d'abord, confirmez les écritures, puis lancez cette commande.
What it is
Zod is a TypeScript-first schema declaration and validation library. You define a schema once and get static type inference, runtime validation, and data transformation in one place. Zod eliminates the gap between your TypeScript types and your runtime validation logic.
Zod is for TypeScript developers who need runtime data validation for API inputs, form data, configuration files, or any boundary where untyped data enters a typed system.
The project is actively maintained with regular releases and a growing user community. Documentation covers common use cases, and the open-source nature means you can inspect the source code, contribute fixes, and adapt the tool to your specific requirements.
How it saves time or tokens
Without Zod, you write TypeScript interfaces for types and separate validation logic (often with Joi, Yup, or manual checks) that can drift out of sync. Zod merges both: the schema is the type. Change the schema, and TypeScript catches every callsite that breaks. This eliminates an entire class of bugs where validation and types disagree.
How to use
- Install Zod via npm.
- Define schemas using Zod's chainable API.
- Use
.parse()or.safeParse()to validate data at runtime.
Example
import { z } from 'zod';
// Define a schema
const UserSchema = z.object({
name: z.string().min(2),
email: z.string().email(),
age: z.number().int().positive().optional(),
});
// Infer the TypeScript type
type User = z.infer<typeof UserSchema>;
// { name: string; email: string; age?: number }
// Validate data
const result = UserSchema.safeParse({
name: 'Alice',
email: 'alice@example.com',
});
if (result.success) {
console.log(result.data); // typed as User
} else {
console.log(result.error.issues);
}
Related on TokRepo
- AI Tools for Coding -- Developer tools and validation libraries
- Featured Workflows -- Top workflows curated on TokRepo
Common pitfalls
- Zod schemas are runtime objects, not just types. Each
.parse()call runs validation logic. For high-throughput hot paths, benchmark against simpler validation approaches. - Deeply nested schemas with many
.refine()calls produce error messages that are hard to map to specific fields. Use.superRefine()with path context for complex validations. - Zod v3 and v4 have breaking API changes. Check the migration guide before upgrading, especially for custom error maps and transform chains.
Before adopting this tool, evaluate whether it fits your team's existing workflow. Read the official documentation thoroughly, and start with a small proof-of-concept rather than a full migration. Community forums, GitHub issues, and Stack Overflow are valuable resources when you encounter edge cases not covered in the documentation.
Questions fréquentes
Zod is TypeScript-first with full static type inference from schemas. Yup was designed for JavaScript and added TypeScript support later. Zod's type inference is more precise, and it does not use class-based schemas like Yup does.
Yes. Zod is commonly used in Express, Fastify, and Next.js API routes to validate incoming request bodies. Libraries like tRPC and Hono use Zod as their default validation layer.
Yes. Zod integrates with React Hook Form via the @hookform/resolvers package and with Formik via custom adapters. Define your form schema in Zod and get both type safety and runtime validation.
z.infer is a utility type that extracts the TypeScript type from a Zod schema. It means you define the schema once and derive the type automatically, keeping validation and types permanently in sync.
Zod schemas can be converted to JSON Schema using the zod-to-json-schema package. This is useful for generating OpenAPI specs or sharing validation rules with non-TypeScript systems.
Sources citées (3)
- Zod GitHub— Zod is a TypeScript-first schema validation library
- Zod Documentation— Schema declaration with static type inference
- tRPC Documentation— tRPC uses Zod as its default validation layer
En lien sur TokRepo
Source et remerciements
Created by Colin McDonnell. Licensed under MIT.
zod — stars 35,000+
Fil de discussion
Actifs similaires
TypeBox — JSON Schema Type Builder for TypeScript
Build JSON Schema types with static TypeScript inference, giving you runtime validation and compile-time safety from a single definition.
io-ts — Runtime Type Validation with Static TypeScript Inference
Codec-based validation library that encodes and decodes data at runtime while inferring TypeScript types from schemas.
ArkType — TypeScript's 1:1 Validator with Instant Inference
Runtime validation library that mirrors TypeScript syntax for schemas with zero-overhead type inference.
class-validator — Decorator-Based Validation for TypeScript Classes
A validation library that uses TypeScript decorators to define validation rules directly on class properties, deeply integrated with NestJS and TypeORM.