class ZodToOpenApi
Since 0.1.0

Converts a Zod schema to an OpenAPI 3.1 schema object.

Zod v3 and v4 are both supported, detected per schema by duck typing (toJSONSchema presence marks v4). An unrecognized schema degrades to {} — never a throw — and, when a diagnostics channel is attached, reports what it could not represent.

Constructors

ZodToOpenApi(
onSchema?: SchemaNodeHook,
channels?: { readonly onDefinitionClaim?: (hint: string) => string; readonly onDefinition?: (
name: string,
) => void
; readonly onUnrepresentable?: (diagnostic: { readonly reason: string; }) => void; }
)

Creates a transformer.

Parameters
optional
onSchema: SchemaNodeHook

Optional hook consulted for every schema node, so a caller can substitute a $ref for a reused schema at ANY depth rather than only at the root. Omit it for a plain transform.

optional
channels: { readonly onDefinitionClaim?: (hint: string) => string; readonly onDefinition?: (
name: string,
) => void
; readonly onUnrepresentable?: (diagnostic: { readonly reason: string; }) => void; }

Optional outbound channels for zod v4 $defs and unrepresentable-node diagnostics. Entirely optional: without them the zod v4 path still produces a complete, self-contained schema.

Methods

Transforms a Zod schema into an OpenAPI schema object.

Usage

import { ZodToOpenApi } from "openapi-plugin/src/index.ts";