DEV Community

Alex Spinov
Alex Spinov

Posted on

Zod Has a Free TypeScript Schema Validation Library With Zero Dependencies

Zod is a TypeScript-first schema declaration and validation library. Define a schema once — get runtime validation AND static TypeScript types.

What You Get for Free

  • TypeScript-first — types inferred from schemas
  • Zero dependencies — lightweight
  • Composable — build complex schemas from simple ones
  • Transforms — parse and transform data
  • Error messages — detailed, customizable errors
  • Works everywhere — Node, Deno, Bun, browsers

Define and Validate

import { z } from 'zod';

const UserSchema = z.object({
  name: z.string().min(2),
  email: z.string().email(),
  age: z.number().int().positive().optional(),
});

type User = z.infer<typeof UserSchema>;
// { name: string; email: string; age?: number }

const result = UserSchema.safeParse({ name: 'Alice', email: 'alice@example.com' });
if (result.success) console.log(result.data); // typed as User
Enter fullscreen mode Exit fullscreen mode

API Validation (Express)

app.post('/api/users', (req, res) => {
  const result = UserSchema.safeParse(req.body);
  if (!result.success) return res.status(400).json(result.error);
  createUser(result.data); // data is typed!
});
Enter fullscreen mode Exit fullscreen mode

Zod vs Joi vs Yup

Feature Zod Joi Yup
TypeScript Native Manual Manual
Bundle size 13KB 150KB 40KB
Inference Yes No Limited

Need TypeScript help? Check my work on GitHub or email spinov001@gmail.com for consulting.

Top comments (0)