Skip to content
Zod

Zod

Officialv5.4.7

@kubb/plugin-zod

kubb add plugin-zod

Options

Pass these options to pluginZod(). Shared options link to Shared plugin options for details.

Options overview

OptionPurposeDefault
outputWhere files are written: path, mode, barrel, banner, footer.{ path: 'zod', barrel: { type: 'named' } }
groupSplit output by tag or path: type, name.None
importPathModule the generated files import z from.'zod', or 'zod/mini' with mini
inferredEmit a z.infer alias next to each schema.false
coercionCoerce input before validation.false
guidTypeValidator for format: uuid properties.'uuid'
regexTypeHow an OpenAPI pattern is written.'literal'
compileWrap schemas in z.compile for fast-path validation.false
miniGenerate Zod Mini schemas.false
typeGuardsGenerate is* type guards and assert* assertions.false
includeKeep only operations and schemas that match.None
excludeSkip operations and schemas that match.[]
overrideApply different options per pattern.[]
resolverCustomize generated names and file paths.resolverZod
macrosRewrite AST nodes before printing.[]
printerReplace 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.

Typestring
Requiredfalse
Defaultmini ? '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.

Typeboolean
Requiredfalse
Defaultfalse
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.

Typeboolean | { dates?: boolean, strings?: boolean, numbers?: boolean }
Requiredfalse
Defaultfalse
false
Coerces nothing and validates strictly, except bigint fields (format: int64), which always use z.coerce.bigint().
true
Coerces strings, numbers, and dates with z.coerce.string(), z.coerce.number() and z.coerce.date().
{ dates?, strings?, numbers? }
Chooses which primitives to coerce.
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'
Requiredfalse
Default'uuid'
'uuid'
Generates z.uuid(), a standard RFC 4122 UUID.
'guid'
Generates 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'
Requiredfalse
Default'literal'
'literal'
Emits a regex literal, such as .regex(/^[a-z]+$/).
'constructor'
Emits the 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.

Typeboolean | { strict?: boolean }
Requiredfalse
Defaultfalse
false
Leaves schemas uncompiled.
true
Compiles schemas using z.compile(...).
{ strict: true }
Passes { 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'.

Typeboolean
Requiredfalse
Defaultfalse

typeGuards

Generates TypeScript type guards (is*) and assertion functions (assert*) with Zod's validate API. Needs Zod v4.6.0 or higher.

Typeboolean | { is?: boolean, assert?: boolean }
Requiredfalse
Defaultfalse
false
Generates only the Zod schemas.
true
Generates both is<Schema> type guards and assert<Schema> assertion functions.
{ is?: boolean; assert?: boolean }
Enables type guards or assertions separately.

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.

src/gen/zod/petSchema.ts
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.

TypeResolverPatch<ResolverZod>
Requiredfalse
Partial override
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 }
Requiredfalse
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.