---
name: integrate-zod-schemas
description: >
  Use the generated zod-mode output: a single TypeScript file holding only
  Zod 4 schemas and their inferred types for an OpenAPI spec's component
  schemas, with no client and no helper types. Covers generating it with
  -m zod, narrowing the output to chosen schemas plus everything they
  reference with --schemas, how polymorphic anyOf/oneOf + discriminator
  (including .NET's), const, enums, patterns and optional properties map
  to Zod, and validating data with schema.parse / safeParse. Load when
  importing or generating a zod-mode file, e.g. to validate a JSON document
  format shared with a .NET API.
type: framework
library: openapi-client-generator
library_version: "3.0.0"
requires:
  - generate-client
sources:
  - "bazo/openapi-client-generator:src/modes/zod/index.ts"
  - "bazo/openapi-client-generator:src/converter/composition.ts"
---

This skill builds on generate-client. Read it first for CLI usage.

# Integrate Zod Schemas

`zod` mode emits only `import { z } from "zod"` followed by one `export const X = z...` and one
`export type X = z.infer<typeof X>` per component schema, in dependency order. There is no HTTP
client, no helper type and no dead-code removal. Choose it when you need to validate or type a
payload (a stored document, a webhook body) rather than call an API.

## Setup

```bash
npx openapi-client-generator openapi.json src/schemas/ -n contentSchemas -m zod
npm install zod   # >= 4
```

Pick a subset with `--schemas`; the output holds those schemas plus every schema they reference,
transitively:

```bash
npx openapi-client-generator openapi.json src/schemas/ -n contentSchemas -m zod --schemas ContentDocument,Block
```

An unknown name fails the run and writes nothing. `--schemas` is ignored (with a warning) in every
other mode.

## Use

```ts
import { ContentDocument, type ContentDocument as Doc } from "./schemas/contentSchemas";

const result = ContentDocument.safeParse(JSON.parse(raw));
if (!result.success) throw new Error(result.error.message);
const doc: Doc = result.data;
```

## Mapping

| Spec | Zod |
|---|---|
| `anyOf`/`oneOf` + `discriminator`, each branch's tag a `const` or one-value `enum` | `z.discriminatedUnion("type", [...])` |
| `additionalProperties: false` | `z.strictObject` |
| `pattern` | `.regex(/.../)`, escapes kept verbatim |
| `const` | `z.literal(...)` |
| `enum: [2, 3]` | `z.literal([2, 3])` (Zod 4's literal union) |
| `{}` | `z.unknown()` |
| not in `required`, not nullable | `.optional()` |
| `format: uuid` | `z.uuid()` |
| `min/maxItems`, `min/maxLength`, `minimum/maximum` | `.min/.max`, `.gte/.lte` |

A discriminated union is only emitted when every branch is an object whose tag property is
literal-like; otherwise the generator falls back to a tag-constrained `z.union`.

## Common mistakes

- Regenerating by hand-editing the output: re-run the CLI instead; the file is generated.
- Using zod 3: the output uses Zod 4 idioms (`z.uuid()`, `z.int()`, `z.strictObject`).
- Expecting `--schemas` to match operation ids: it takes component schema names.
