Skip to main content
⚡ OpenDevDocs
packagesvalidationReferenceBeginner

Zod: TypeScript-First Schema Declaration & Validation

Complete developer reference for Zod, TypeScript-first schema declaration, runtime type inference, parsing, object schemas, transforms, and error handling.

Verified: 2026-09-30
Tested on:zod 3.23.x, typescript 5.6.x
Platforms:
nodejsbrowserall
#zod#typescript#validation#schema

What It Is

Zod is a TypeScript-first schema declaration and runtime data validation library. It allows developers to define a single schema once and automatically infer the corresponding static TypeScript type, eliminating duplicate type declarations and runtime parsing bugs.


Why Use It

  1. Zero Runtime Dependencies: Ultra-lean, framework-agnostic, and runs identically in Node.js, Next.js, and browser environments.
  2. Automatic Type Inference (z.infer<typeof schema>): Eliminates manual synchronization between TypeScript interfaces and runtime validation logic.
  3. Parse, Don't Validate: Zod safely transforms, coerces, and normalizes input data during the parsing phase.
  4. Developer Experience: Concise, chainable, composable API with deep error reporting and custom error messages.

Installation

[!NOTE] Ensure "strict": true is enabled in your tsconfig.json for proper type inference.


Quick Start

import { z } from "zod";

// 1. Define schema
const UserSchema = z.object({
  id: z.string().uuid(),
  name: z.string().min(2, "Name must be at least 2 characters"),
  email: z.string().email(),
  age: z.number().int().min(18).optional(),
  role: z.enum(["admin", "user", "guest"]).default("user"),
});

// 2. Infer static TypeScript type
export type User = z.infer<typeof UserSchema>;

// 3. Parse untrusted data
const rawData = {
  id: "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d",
  name: "Alice",
  email: "alice@example.com",
};

// Returns parsed User object or throws ZodError
const user = UserSchema.parse(rawData);

// Safe parsing (Does not throw)
const result = UserSchema.safeParse(rawData);
if (!result.success) {
  console.error(result.error.format());
} else {
  console.log("Validated user:", result.data);
}

Common APIs

APIDescriptionExample
z.string()Validates string primitives with helpers (email, uuid, url, regex)z.string().min(3).max(50)
z.number()Validates numbers (int, positive, gt, gte)z.number().positive()
z.boolean()Validates boolean true/falsez.boolean()
z.array(schema)Validates array elementsz.array(z.string()).nonempty()
z.object(shape)Validates nested objectsz.object({ a: z.string() })
z.enum([...])Validates union of string literalsz.enum(["pending", "done"])
z.union([...])Validates OR unionsz.union([z.string(), z.number()])
z.coerce.*Coerces strings to primitivesz.coerce.number(), z.coerce.date()
.optional()Allows undefinedz.string().optional()
.nullable()Allows nullz.string().nullable()
.nullish()Allows null or undefinedz.string().nullish()
.transform(fn)Transforms output during parsez.string().transform(s => s.trim())
.refine(fn)Custom validation predicatesschema.refine(v => v % 2 === 0)

Examples

1. Validating Environment Variables (Next.js / Node.js)

// env.ts
import { z } from "zod";

const envSchema = z.object({
  DATABASE_URL: z.string().url(),
  PORT: z.coerce.number().default(3000),
  NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
});

export const env = envSchema.parse(process.env);

2. Next.js Server Action / API Route Validation

import { z } from "zod";

const CreatePostSchema = z.object({
  title: z.string().min(5),
  content: z.string().min(20),
});

export async function createPostAction(formData: FormData) {
  const result = CreatePostSchema.safeParse({
    title: formData.get("title"),
    content: formData.get("content"),
  });

  if (!result.success) {
    return { errors: result.error.flatten().fieldErrors };
  }

  // Safe data is fully typed
  const { title, content } = result.data;
  // await db.post.create(...)
}

Best Practices

  1. Use safeParse for User-Facing Forms: Avoid try/catch overhead and handle error states gracefully.
  2. Coerce Primitives from FormData / Query Strings: Query strings and FormData entries are always strings; use z.coerce.number() or z.coerce.boolean() to parse them accurately.
  3. Keep Schemas Modular: Reuse small atomic sub-schemas with .merge() and .extend().
  4. Use Custom Error Messages: Provide human-friendly error descriptions directly in the schema definition.

Common Mistakes

  • Using parse() without try/catch: Unhandled ZodError will crash Node.js processes or trigger 500 error pages. Use safeParse() instead.
  • Forgetting strict: true in tsconfig: Leads to inaccurate static type inferences.
  • Confusing .nullish() with .optional(): .optional() permits undefined, whereas .nullable() permits null.

Alternatives

  • Valibot: Highly modular alternative with a smaller bundle size (~1KB) through tree-shaking.
  • Yup: Popular schema validator, historically paired with Formik, but with weaker TypeScript inference.
  • TypeBox: High-performance JSON Schema generator optimized for Fastify.
  • ArkType: Ultra-fast TypeScript syntax validator with zero compilation overhead.

When Not to Use It

  • For simple primitive checks inside performance-critical hot loops where microsecond overhead matters.
  • In projects where pure JSON Schema standard compliance without TypeScript is strictly required.

Official Resources

On this page