DEV Community

Alex Spinov
Alex Spinov

Posted on

Zod Has a Free TypeScript Schema Validation — Validate Data with Full Type Inference

A developer had TypeScript types AND Joi validation schemas AND JSON Schema definitions. Three sources of truth for the same data shape. They always drifted apart.

Zod is a free TypeScript-first schema validation library. Define your schema ONCE, get runtime validation AND TypeScript types automatically.

What Zod Offers for Free

  • TypeScript First - Infer types directly from schemas
  • Runtime Validation - Validate data at boundaries (APIs, forms)
  • Composable - Build complex schemas from simple ones
  • Error Messages - Detailed, customizable error messages
  • Transforms - Parse and transform data in one step
  • Async Validation - Async refinements and transforms
  • Zero Dependencies - Tiny bundle, no dependencies
  • Ecosystem - Works with React Hook Form, tRPC, Astro

Quick Start

import { z } from 'zod'

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

type User = z.infer<typeof UserSchema>  // TypeScript type auto-generated

const result = UserSchema.safeParse(inputData)
if (result.success) {
  console.log(result.data)  // fully typed
}
Enter fullscreen mode Exit fullscreen mode

GitHub: colinhacks/zod - 35K+ stars


Need to monitor and scrape data from multiple web services automatically? I build custom scraping solutions. Check out my web scraping toolkit or email me at spinov001@gmail.com for a tailored solution.

Top comments (0)