MCPcopy Create free account
hub / github.com/colinhacks/zod

github.com/colinhacks/zod

Chat with this repo
repository ↗ · DeepWiki ↗ · release v4.3.6 ↗ · + Follow · compare 2 versions
2,205 symbols 6,027 edges 390 files 72 documented · 3% updated 1d agov4.4.3 · 2026-05-04★ 43,286111 open issues

Browse by type

Functions 1,424 Types & classes 781
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Zod logo

Zod

TypeScript-first schema validation with static type inference



by <a href="https://x.com/colinhacks">@colinhacks</a>

Zod CI status License npm discord server stars

Docs   •   Discord   •   𝕏   •   Bluesky

Featured sponsor: Jazz

  <img alt="jazz logo" src="https://raw.githubusercontent.com/garden-co/jazz/938f6767e46cdfded60e50d99bf3b533f4809c68/homepage/homepage/public/Zod%20sponsor%20message.png" width="85%">

Learn more about featured sponsorships

Read the docs →

What is Zod?

Zod is a TypeScript-first validation library. Define a schema and parse some data with it. You'll get back a strongly typed, validated result.

import * as z from "zod";

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

// some untrusted data...
const input = {
  /* stuff */
};

// the parsed result is validated and type safe!
const data = User.parse(input);

// so you can use it with confidence :)
console.log(data.name);

Features

  • Zero external dependencies
  • Works in Node.js and all modern browsers
  • Tiny: 2kb core bundle (gzipped)
  • Immutable API: methods return a new instance
  • Concise interface
  • Works with TypeScript and plain JS
  • Built-in JSON Schema conversion
  • Extensive ecosystem

Installation

npm install zod

Basic usage

Before you can do anything else, you need to define a schema. For the purposes of this guide, we'll use a simple object schema.

import * as z from "zod";

const Player = z.object({
  username: z.string(),
  xp: z.number(),
});

Parsing data

Given any Zod schema, use .parse to validate an input. If it's valid, Zod returns a strongly-typed deep clone of the input.

Player.parse({ username: "billie", xp: 100 });
// => returns { username: "billie", xp: 100 }

Note — If your schema uses certain asynchronous APIs like async refinements or transforms, you'll need to use the .parseAsync() method instead.

const schema = z.string().refine(async (val) => val.length <= 8);

await schema.parseAsync("hello");
// => "hello"

Handling errors

When validation fails, the .parse() method will throw a ZodError instance with granular information about the validation issues.

try {
  Player.parse({ username: 42, xp: "100" });
} catch (err) {
  if (err instanceof z.ZodError) {
    err.issues;
    /* [
      {
        expected: 'string',
        code: 'invalid_type',
        path: [ 'username' ],
        message: 'Invalid input: expected string'
      },
      {
        expected: 'number',
        code: 'invalid_type',
        path: [ 'xp' ],
        message: 'Invalid input: expected number'
      }
    ] */
  }
}

To avoid a try/catch block, you can use the .safeParse() method to get back a plain result object containing either the successfully parsed data or a ZodError. The result type is a discriminated union, so you can handle both cases conveniently.

const result = Player.safeParse({ username: 42, xp: "100" });
if (!result.success) {
  result.error; // ZodError instance
} else {
  result.data; // { username: string; xp: number }
}

Note — If your schema uses certain asynchronous APIs like async refinements or transforms, you'll need to use the .safeParseAsync() method instead.

const schema = z.string().refine(async (val) => val.length <= 8);

await schema.safeParseAsync("hello");
// => { success: true; data: "hello" }

Inferring types

Zod infers a static type from your schema definitions. You can extract this type with the z.infer<> utility and use it however you like.

const Player = z.object({
  username: z.string(),
  xp: z.number(),
});

// extract the inferred type
type Player = z.infer<typeof Player>;

// use it in your code
const player: Player = { username: "billie", xp: 100 };

In some cases, the input & output types of a schema can diverge. For instance, the .transform() API can convert the input from one type to another. In these cases, you can extract the input and output types independently:

const mySchema = z.string().transform((val) => val.length);

type MySchemaIn = z.input<typeof mySchema>;
// => string

type MySchemaOut = z.output<typeof mySchema>; // equivalent to z.infer<typeof mySchema>
// number

Extension points exported contracts — how you extend this code

browse all types & interfaces →

Core symbols most depended-on inside this repo

browse all functions →

Shape

Function 987
Interface 628
Method 437
Class 128
Enum 25

Languages

TypeScript100%

Modules by API surface

packages/zod/src/v3/types.ts365 symbols
packages/zod/src/v4/classic/schemas.ts331 symbols
packages/zod/src/v4/core/schemas.ts257 symbols
packages/zod/src/v4/mini/schemas.ts187 symbols
packages/zod/src/v4/core/api.ts120 symbols
packages/zod/src/v4/core/checks.ts69 symbols
packages/zod/src/v4/core/util.ts67 symbols
packages/zod/src/v4/core/json-schema-processors.ts41 symbols
packages/zod/src/v4/classic/tests/recursive-types.test.ts32 symbols
packages/zod/src/v4/core/errors.ts29 symbols
packages/zod/src/v3/ZodError.ts29 symbols
packages/zod/src/v4/core/zsf.ts17 symbols

Dependencies from manifests, versioned

@ai-sdk/openai3.0.2 · 1×
@arethetypeswrong/cli0.17.4 · 1×
@biomejs/biome1.9.4 · 1×
@inkeep/cxkit-react0.5.99 · 1×
@radix-ui/react-accordion1.2.4 · 1×
@rollup/plugin-commonjs28.0.3 · 1×
@rollup/plugin-node-resolve16.0.1 · 1×
@rollup/plugin-terser0.4.4 · 1×
@seriousme/openapi-schema-validator2.5.0 · 1×
@types/benchmark2.1.5 · 1×

Datastores touched

(mongodb)Database · 1 repos
defaultauthdbDatabase · 1 repos

For agents

$ claude mcp add zod \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page