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.
ZodToOpenApi(onSchema?: SchemaNodeHook,channels?: { readonly onDefinitionClaim?: (hint: string) => string; readonly onDefinition?: (name: string,schema: OpenApiSchemaObject) => 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,schema: OpenApiSchemaObject) => 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.
transform(schema: unknown,io?: SchemaIo): OpenApiSchemaObject
Transforms a Zod schema into an OpenAPI schema object.