
Zod Has a Free API: TypeScript-First Schema Validation That Actually Works
Why Zod Zod validates data AND gives you TypeScript types from schemas. Define once, validate at runtime, get types at compile time. No more duplicating types and validation logic. Install npm install zod Basic Schemas import { z } from ' zod ' ; const UserSchema = z . object ({ name : z . string (). min ( 2 ), email : z . string (). email (), age : z . number (). int (). min ( 18 ). max ( 120 ), role : z . enum ([ ' admin ' , ' user ' , ' moderator ' ]), bio : z . string (). optional (), }); type User = z . infer < typeof UserSchema > ; // { name: string; email: string; age: number; role: 'admin'|'user'|'moderator'; bio?: string } const user = UserSchema . parse ({ name : ' Alice ' , email : ' alice@example.com ' , age : 30 , role : ' admin ' , }); API Validation import express from ' express ' ; import { z } from ' zod ' ; const CreateOrderSchema = z . object ({ productId : z . string (). uuid (), quantity : z . number (). int (). positive (), coupon : z . string (). optional (), });
Continue reading on Dev.to JavaScript
Opens in a new tab

