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.
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.
Frequently Asked Questions
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.
Citations (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
Related on TokRepo
Source & Thanks
Created by Colin McDonnell. Licensed under MIT.
zod — stars 35,000+
Discussion
Related Assets
doctest — The Fastest Feature-Rich C++ Testing Framework
doctest is a single-header C++ testing framework designed for minimal compile-time overhead and maximum speed.
Chai — BDD/TDD Assertion Library for Node.js
Chai is a flexible assertion library for Node.js and browsers that supports expect, should, and assert styles.
Supertest — HTTP Assertion Library for Node.js APIs
Supertest provides a high-level API for testing HTTP servers in Node.js with fluent assertion chaining.