AK-21/Graphite-Industrial-Intelligence
0
1<p align="center">2 <img src="logo.svg" width="200px" align="center" alt="Zod logo" />3 <h1 align="center">Zod</h1>4 <p align="center">5 TypeScript-first schema validation with static type inference6 <br/>7 by <a href="https://x.com/colinhacks">@colinhacks</a>8 </p>9</p>10<br/>11 12<p align="center">13<a href="https://github.com/colinhacks/zod/actions?query=branch%3Amain"><img src="https://github.com/colinhacks/zod/actions/workflows/test.yml/badge.svg?event=push&branch=main" alt="Zod CI status" /></a>14<a href="https://opensource.org/licenses/MIT" rel="nofollow"><img src="https://img.shields.io/github/license/colinhacks/zod" alt="License"></a>15<a href="https://www.npmjs.com/package/zod" rel="nofollow"><img src="https://img.shields.io/npm/dw/zod.svg" alt="npm"></a>16<a href="https://discord.gg/KaSRdyX2vc" rel="nofollow"><img src="https://img.shields.io/discord/893487829802418277?label=Discord&logo=discord&logoColor=white" alt="discord server"></a>17<a href="https://github.com/colinhacks/zod" rel="nofollow"><img src="https://img.shields.io/github/stars/colinhacks/zod" alt="stars"></a>18</p>19 20<div align="center">21 <a href="https://zod.dev/api">Docs</a>22 <span> • </span>23 <a href="https://discord.gg/RcG33DQJdf">Discord</a>24 <span> • </span>25 <a href="https://twitter.com/colinhacks">𝕏</a>26 <span> • </span>27 <a href="https://bsky.app/profile/zod.dev">Bluesky</a>28 <br />29</div>30 31<br/>32<br/>33 34### [Read the docs →](https://zod.dev/api)35 36<br/>37<br/>38 39## What is Zod?40 41Zod is a TypeScript-first validation library. Define a schema and parse some data with it. You'll get back a strongly typed, validated result.42 43```ts44import * as z from "zod";45 46const User = z.object({47 name: z.string(),48});49 50// some untrusted data...51const input = {52 /* stuff */53};54 55// the parsed result is validated and type safe!56const data = User.parse(input);57 58// so you can use it with confidence :)59console.log(data.name);60```61 62<br/>63 64## Features65 66- Zero external dependencies67- Works in Node.js and all modern browsers68- Tiny: `2kb` core bundle (gzipped)69- Immutable API: methods return a new instance70- Concise interface71- Works with TypeScript and plain JS72- Built-in JSON Schema conversion73- Extensive ecosystem74 75<br/>76 77## Installation78 79```sh80npm install zod81```82 83<br/>84 85## Basic usage86 87Before you can do anything else, you need to define a schema. For the purposes of this guide, we'll use a simple object schema.88 89```ts90import * as z from "zod";91 92const Player = z.object({93 username: z.string(),94 xp: z.number(),95});96```97 98### Parsing data99 100Given any Zod schema, use `.parse` to validate an input. If it's valid, Zod returns a strongly-typed _deep clone_ of the input.101 102```ts103Player.parse({ username: "billie", xp: 100 });104// => returns { username: "billie", xp: 100 }105```106 107**Note** — If your schema uses certain asynchronous APIs like `async` [refinements](https://zod.dev/api#refinements) or [transforms](https://zod.dev/api#transforms), you'll need to use the `.parseAsync()` method instead.108 109```ts110const schema = z.string().refine(async (val) => val.length <= 8);111 112await schema.parseAsync("hello");113// => "hello"114```115 116### Handling errors117 118When validation fails, the `.parse()` method will throw a `ZodError` instance with granular information about the validation issues.119 120```ts121try {122 Player.parse({ username: 42, xp: "100" });123} catch (err) {124 if (err instanceof z.ZodError) {125 err.issues;126 /* [127 {128 expected: 'string',129 code: 'invalid_type',130 path: [ 'username' ],131 message: 'Invalid input: expected string'132 },133 {134 expected: 'number',135 code: 'invalid_type',136 path: [ 'xp' ],137 message: 'Invalid input: expected number'138 }139 ] */140 }141}142```143 144To 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](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions), so you can handle both cases conveniently.145 146```ts147const result = Player.safeParse({ username: 42, xp: "100" });148if (!result.success) {149 result.error; // ZodError instance150} else {151 result.data; // { username: string; xp: number }152}153```154 155**Note** — If your schema uses certain asynchronous APIs like `async` [refinements](https://zod.dev/api#refinements) or [transforms](https://zod.dev/api#transforms), you'll need to use the `.safeParseAsync()` method instead.156 157```ts158const schema = z.string().refine(async (val) => val.length <= 8);159 160await schema.safeParseAsync("hello");161// => { success: true; data: "hello" }162```163 164### Inferring types165 166Zod infers a static type from your schema definitions. You can extract this type with the `z.infer<>` utility and use it however you like.167 168```ts169const Player = z.object({170 username: z.string(),171 xp: z.number(),172});173 174// extract the inferred type175type Player = z.infer<typeof Player>;176 177// use it in your code178const player: Player = { username: "billie", xp: 100 };179```180 181In 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:182 183```ts184const mySchema = z.string().transform((val) => val.length);185 186type MySchemaIn = z.input<typeof mySchema>;187// => string188 189type MySchemaOut = z.output<typeof mySchema>; // equivalent to z.infer<typeof mySchema>190// number191```192 