Zod

Zod is a TypeScript-first schema declaration and validation library

  • declare a validator once and infers static TypeScript type

  • libraries adapters like react-hook-form

npm install zod

Requires TypeScript 4.5+ and strict mode in your tsconfig.json

// tsconfig.json
{
  // ...
  "compilerOptions": {
    // ...
    "strict": true
  }
}

Usage

import { z } from "zod";

const User = z.object({
  username: z.string(),
});

User.parse({ username: "Ludwig" });

// extract the inferred type
type User = z.infer<typeof User>;
// { username: string }

Types

Primitatives

Optional

String Validation

Last updated