DEV Community

Alex Spinov
Alex Spinov

Posted on

Zod Has a Free API You Should Know About

Zod is a TypeScript-first schema validation library. Define a schema once and get type inference, runtime validation, and error messages — all from a single source of truth.

Why Zod Dominates

A backend developer was writing TypeScript interfaces AND Joi validation schemas for every API endpoint — duplicating work on every change. With Zod, they define the schema once and TypeScript types are inferred automatically.

Key Features:

  • TypeScript-First — Types are inferred from schemas automatically
  • Zero Dependencies — Tiny bundle, no baggage
  • Composable — Build complex schemas from simple ones
  • Rich Error Messages — Detailed validation errors out of the box
  • Transforms — Parse and transform data in one step

Quick Start

npm install zod
Enter fullscreen mode Exit fullscreen mode
import { z } from "zod"

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

// Type is automatically inferred!
type User = z.infer<typeof UserSchema>

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

API Validation

const CreatePostSchema = z.object({
  title: z.string().max(200),
  content: z.string().min(10),
  tags: z.array(z.string()).max(5)
})

app.post("/posts", (req, res) => {
  const result = CreatePostSchema.safeParse(req.body)
  if (!result.success) return res.status(400).json(result.error)
  // result.data is fully typed
  createPost(result.data)
})
Enter fullscreen mode Exit fullscreen mode

Works With Everything

React Hook Form, tRPC, Next.js Server Actions, Express, Fastify, and any TypeScript project.

Why Choose Zod

  1. Single source of truth — schema = types = validation
  2. Excellent DX — autocomplete, type inference everywhere
  3. Battle-tested — used by Vercel, Stripe, and thousands of companies

Check out Zod docs to get started.


Need data validation at scale? Check out my Apify actors or email spinov001@gmail.com for custom solutions.

Top comments (0)