|
| 1 | +# Zod Integration |
| 2 | + |
| 3 | +[Zod](https://zod.dev/) implements [Standard Schema](/docs/integrations/standard-schema), so you can use it directly in your procedures without any extra setup. On top of that, `@orpc/zod` provides a dedicated JSON Schema converter and registries for customizing the generated JSON schemas. |
| 4 | + |
| 5 | +::: warning |
| 6 | +`@orpc/zod` requires Zod v4 or later. |
| 7 | +::: |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +::: code-group |
| 12 | + |
| 13 | +```sh [npm] |
| 14 | +npm install @orpc/zod@beta zod |
| 15 | +``` |
| 16 | + |
| 17 | +```sh [yarn] |
| 18 | +yarn add @orpc/zod@beta zod |
| 19 | +``` |
| 20 | + |
| 21 | +```sh [pnpm] |
| 22 | +pnpm add @orpc/zod@beta zod |
| 23 | +``` |
| 24 | + |
| 25 | +```sh [bun] |
| 26 | +bun add @orpc/zod@beta zod |
| 27 | +``` |
| 28 | + |
| 29 | +```sh [deno] |
| 30 | +deno add npm:@orpc/zod@beta npm:zod |
| 31 | +``` |
| 32 | + |
| 33 | +::: |
| 34 | + |
| 35 | +## JSON Schema Converter |
| 36 | + |
| 37 | +`ZodToJsonSchemaConverter` wraps [Zod's built-in toJSONSchema](https://zod.dev/json-schema?id=ztojsonschema#ztojsonschema) and adds support for additional types such as `z.bigint()`, `z.date()`, `z.set()`, and `z.map()`. Use it with tools such as the [OpenAPI Generator](/docs/openapi/specification#openapi-generator) and [Smart Coercion](/docs/plugins/smart-coercion). It accepts the same options as Zod's `toJSONSchema`, see the [source code](https://github.com/middleapi/orpc/blob/main/packages/zod/src/converter.ts) for implementation details. |
| 38 | + |
| 39 | +```ts |
| 40 | +import { OpenAPIGenerator } from '@orpc/openapi' |
| 41 | +import { ZodToJsonSchemaConverter } from '@orpc/zod' |
| 42 | + |
| 43 | +const generator = new OpenAPIGenerator({ |
| 44 | + converters: [new ZodToJsonSchemaConverter()], |
| 45 | +}) |
| 46 | +``` |
| 47 | + |
| 48 | +### Reusable Schemas |
| 49 | + |
| 50 | +A common pattern is defining reusable schemas with `id` metadata. The converter places them in `$defs`, which `OpenAPIGenerator` then [hoists](/docs/openapi/specification#hoisting-defs) into `components.schemas`. For more on `id` and `$ref` in Zod, see [Zod JSON Schema Registries](https://zod.dev/json-schema?id=registries#registries). |
| 51 | + |
| 52 | +```ts |
| 53 | +import * as z from 'zod' |
| 54 | + |
| 55 | +const PlanetSchema = z.object({ |
| 56 | + id: z.string(), |
| 57 | + name: z.string(), |
| 58 | +}).meta({ id: 'Planet' }) |
| 59 | +``` |
| 60 | + |
| 61 | +### Customizing Generated JSON Schemas |
| 62 | + |
| 63 | +`@orpc/zod` exposes registries for customizing the JSON schema generated for a given Zod schema. Registered entries are shallow merged over the generated JSON schema: `JSON_SCHEMA_REGISTRY` applies to both input and output, while `JSON_SCHEMA_INPUT_REGISTRY` and `JSON_SCHEMA_OUTPUT_REGISTRY` apply to a single direction and win on conflicting keys: |
| 64 | + |
| 65 | +```ts |
| 66 | +import { |
| 67 | + JSON_SCHEMA_INPUT_REGISTRY, |
| 68 | + JSON_SCHEMA_OUTPUT_REGISTRY, |
| 69 | + JSON_SCHEMA_REGISTRY, |
| 70 | +} from '@orpc/zod' |
| 71 | +import * as z from 'zod' |
| 72 | + |
| 73 | +const user = z.object({ |
| 74 | + name: z.string(), |
| 75 | + age: z.string().transform(v => Number(v)), |
| 76 | +}) |
| 77 | + |
| 78 | +JSON_SCHEMA_REGISTRY.add(user, { |
| 79 | + description: 'A user', |
| 80 | +}) |
| 81 | + |
| 82 | +JSON_SCHEMA_INPUT_REGISTRY.add(user, { |
| 83 | + examples: [{ name: 'John', age: '20' }], |
| 84 | +}) |
| 85 | + |
| 86 | +JSON_SCHEMA_OUTPUT_REGISTRY.add(user, { |
| 87 | + examples: [{ name: 'John', age: 20 }], |
| 88 | +}) |
| 89 | +``` |
0 commit comments