ScriptsApr 7, 2026·1 min read

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.

TL;DR
Zod validates data at runtime while automatically inferring TypeScript types from your schema.
§01

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.

§02

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.

§03

How to use

  1. Install Zod via npm.
  2. Define schemas using Zod's chainable API.
  3. Use .parse() or .safeParse() to validate data at runtime.
§04

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);
}
§05

Related on TokRepo

§06

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

How does Zod compare to Yup?+

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.

Can Zod validate API request bodies?+

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.

Does Zod work with React forms?+

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.

What is z.infer?+

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.

Is Zod compatible with JSON Schema?+

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)
🙏

Source & Thanks

Created by Colin McDonnell. Licensed under MIT.

zod — stars 35,000+

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets