
Zod Is Not Just Validation — 5 Things You Did Not Know It Could Do
Zod Is a Swiss Army Knife Most developers use Zod for form validation. But it can do much more: generate types, transform data, build API schemas, and more. 1. Generate TypeScript Types Automatically import { z } from " zod " ; const UserSchema = z . object ({ name : z . string (). min ( 1 ), email : z . string (). email (), age : z . number (). min ( 0 ). max ( 150 ), role : z . enum ([ " admin " , " user " , " moderator " ]) }); // Auto-generate TypeScript type type User = z . infer < typeof UserSchema > ; // { name: string; email: string; age: number; role: "admin" | "user" | "moderator" } Define schema once. Get validation AND types. 2. Transform Data During Validation const FormInput = z . object ({ name : z . string (). transform ( s => s . trim ()), email : z . string (). email (). transform ( s => s . toLowerCase ()), age : z . string (). transform ( Number ). pipe ( z . number (). min ( 0 )), tags : z . string (). transform ( s => s . split ( " , " ). map ( t => t . trim ()))
Continue reading on Dev.to Tutorial
Opens in a new tab




