Skip to content
Plugin
Zod logo
plugins: [pluginZod()]
Officialv5.10.0kubb >=5.19.0node >=22.22

OpenAPI to Zod schema generator

@kubb/plugin-zod

Generates Zod v4 schemas from your OpenAPI spec so you validate API responses, form input, and query params at runtime.

kubb add plugin-zod
zodvalidationschemaruntime-validationcodegenopenapi

The Zod plugin generates a Zod v4 schema for every schema in your OpenAPI spec, plus the parameter, request, and response schemas of each operation.

@kubb/plugin-fetch, @kubb/plugin-axios, and @kubb/plugin-client validate with these schemas when you set their validator option.

Installation

kubb add plugin-zod

kubb add installs the version the Kubb registry pins and adds pluginZod() to kubb.config.ts. It needs a kubb.config.ts, so run kubb init first in a new project.

Setup

  • No other plugins are needed.
  • Install zod v4 in the app that uses the generated code. Some options need a newer minor:
OptionNeeds
oneOfType: 'xor'Zod 4.2 or later
compileZod 4.5 or later
typeGuardsZod 4.6 or later

Options worth knowing

OptionDefaultWhat it does
inferredfalseAdds a z.infer type next to each schema, so you can skip @kubb/plugin-ts.
minifalseBeta. Generates Zod Mini schemas for smaller bundles.
coercionfalseCoerces input before validation, such as query strings.
oneOfType'union'Validates a oneOf with z.union or z.xor.
typeGuardsfalseAdds is* type guards and assert* assertions.
compilefalseWraps schemas in z.compile for faster validation.

Example

This config generates the schemas with a z.infer type next to each one. For a Pet schema, the plugin writes petSchema.ts:

import { defineConfig } from 'kubb/config'
import { pluginZod } from '@kubb/plugin-zod'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  plugins: [pluginZod({ inferred: true })],
})

Next, customize the generated schemas.

Dependencies

No extension dependencies.

Used by

Extensions that list Zod as a dependency.