Zod
Officialv5.4.7@kubb/plugin-zod
Options
Pass these options to pluginZod(). Shared options link to Shared plugin options for details.
Options overview
| Option | Purpose | Default |
|---|---|---|
output | Where files are written: path, mode, barrel, banner, footer. | { path: 'zod', barrel: { type: 'named' } } |
group | Split output by tag or path: type, name. | None |
importPath | Module the generated files import z from. | 'zod', or 'zod/mini' with mini |
inferred | Emit a z.infer alias next to each schema. | false |
coercion | Coerce input before validation. | false |
guidType | Validator for format: uuid properties. | 'uuid' |
regexType | How an OpenAPI pattern is written. | 'literal' |
compile | Wrap schemas in z.compile for fast-path validation. | false |
mini | Generate Zod Mini schemas. | false |
typeGuards | Generate is* type guards and assert* assertions. | false |
include | Keep only operations and schemas that match. | None |
exclude | Skip operations and schemas that match. | [] |
override | Apply different options per pattern. | [] |
resolver | Customize generated names and file paths. | resolverZod |
macros | Rewrite AST nodes before printing. | [] |
printer | Replace the handler for a schema type. | None |
Option details
importPath
Module the generated files import z from. Point it at your own module to re-export Zod.
| Type | string |
| Required | false |
| Default | mini ? 'zod/mini' : 'zod' |
'zod' and 'zod/mini' are imported as import * as z. A custom module is imported as import { z }, so export z by name.
inferred
Exports a z.infer<typeof schema> type next to each schema, so you do not need @kubb/plugin-ts. The type takes the PascalCase schema name plus SchemaType, so petSchema gets PetSchemaType.
| Type | boolean |
| Required | false |
| Default | false |
export type PetSchemaType = z.infer<typeof petSchema>It also writes a ResponsesSchema for each operation (one schema per status code) and its inferred type. @kubb/plugin-fetch and @kubb/plugin-axios key their RequestResult on it when @kubb/plugin-ts is absent.
coercion
Wraps schemas in z.coerce so input is coerced before validation, for form data, query params, and similar string sources. See Coercion for primitives.
| Type | boolean | { dates?: boolean, strings?: boolean, numbers?: boolean } |
| Required | false |
| Default | false |
bigint fields (format: int64), which always use z.coerce.bigint().z.coerce.string(), z.coerce.number() and z.coerce.date().dates coerces only Date-typed fields (from dateType: 'date'). ISO-string fields are never coerced, and neither are format: time fields, because new Date() cannot parse a bare HH:mm:ss. For a time-of-day type such as Temporal.PlainTime, see Encode custom types.guidType
Validator used for OpenAPI properties with format: uuid.
| Type | 'uuid' | 'guid' |
| Required | false |
| Default | 'uuid' |
z.uuid(), a standard RFC 4122 UUID.z.guid(), which is looser and accepts Microsoft-style GUIDs.regexType
Controls how an OpenAPI pattern is written inside .regex(...). Use 'constructor' when a regex literal breaks your build or you need a string pattern.
| Type | 'literal' | 'constructor' |
| Required | false |
| Default | 'literal' |
.regex(/^[a-z]+$/).RegExp constructor, such as .regex(new RegExp('^[a-z]+$')).compile
Wraps schemas in z.compile(...) to generate validation code instead of interpreting the schema on each call. Needs Zod v4.5.0 or higher.
| Type | boolean | { strict?: boolean } |
| Required | false |
| Default | false |
z.compile(...).{ strict: true } to z.compile(...), which throws when any part of the schema cannot be compiled into flat JavaScript instead of falling back to the interpreter.Schemas with circular references (z.lazy) and bare $ref response aliases stay uncompiled to prevent runtime errors.
mini
Generates Zod Mini schemas. They use functions (z.optional(z.string())) instead of method chains (z.string().optional()), so bundlers can drop unused validators. Also sets the importPath default to 'zod/mini'.
| Type | boolean |
| Required | false |
| Default | false |
typeGuards
Generates TypeScript type guards (is*) and assertion functions (assert*) with Zod's validate API. Needs Zod v4.6.0 or higher.
| Type | boolean | { is?: boolean, assert?: boolean } |
| Required | false |
| Default | false |
is<Schema> type guards and assert<Schema> assertion functions.When inferred is true, the guards narrow to the generated type alias such as PetSchemaType. When mini is true, they route through z.validate and z.parse.
export const isPet = (data: unknown): data is z.infer<typeof petSchema> => petSchema.validate(data)
export function assertPet(data: unknown): asserts data is z.infer<typeof petSchema> {
if (!petSchema.validate(data)) {
petSchema.parse(data)
}
}resolver
Overrides generated file and symbol names. Omitted members keep resolverZod. The shared members (name, file, imports) and the this context are described under resolver.
| Type | ResolverPatch<ResolverZod> |
| Required | false |
type ResolverZodPatch = {
// plus the shared name, file, and imports members
schema?: {
typeName?(name: string): string
type?(name: string): string
inputName?(name: string): string
inputTypeName?(name: string): string
isName?(name: string): string
assertName?(name: string): string
}
param?: {
name?(node: OperationNode, param: ParameterNode): string
path?(node: OperationNode, param: ParameterNode): string
query?(node: OperationNode, param: ParameterNode): string
headers?(node: OperationNode, param: ParameterNode): string
}
response?: {
status?(node: OperationNode, statusCode: StatusCode): string
body?(node: OperationNode): string
responses?(node: OperationNode): string
response?(node: OperationNode): string
error?(node: OperationNode): string
options?(node: OperationNode): string
}
}printer
Replaces the handler for a schema type such as 'integer' or 'string'. Each handler returns the Zod expression as a string, in Zod Mini syntax when mini: true. Inside a handler, this.base(node) returns the built-in output to wrap and this.transform(node) recurses into nested nodes. See the printer guide.
| Type | { nodes?: PrinterZodNodes | PrinterZodMiniNodes } |
| Required | false |
import { pluginZod } from '@kubb/plugin-zod'
pluginZod({
printer: {
nodes: {
integer() {
return 'z.number()'
},
},
},
})A handler can read this.options.direction, which is 'decode' for responses and 'encode' for request bodies and parameters. If it returns a different expression per direction, Kubb also writes an ${name}InputSchema. Request bodies use it, including bodies that reach the schema through a $ref. See Encode custom types.