Skip to content
Plugin
Zod
plugins: [pluginZod()]
Officialv5.4.8kubb >=5.8.3node >=22

Zod

@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.

The client plugins @kubb/plugin-fetch, @kubb/plugin-axios, and @kubb/plugin-client use these schemas when you set their validator option.

Installation

bun add -d @kubb/plugin-zod

Or run kubb add plugin-zod to install the version the Kubb registry pins and add pluginZod() to kubb.config.ts.

Setup

  • No other plugins are needed.
  • Install zod v4 in the app that uses the generated code. compile needs 4.5 or later, and typeGuards needs 4.6 or later.

Options worth knowing

  • mini (beta) generates Zod Mini schemas for smaller bundles, and switches importPath to zod/mini.
  • importPath sets the module the generated files import z from. The default is zod.
  • inferred adds a z.infer type next to each schema, so you can skip @kubb/plugin-ts.
  • coercion wraps schemas in z.coerce, so input is converted before validation. Pass an object to coerce only dates, strings, or numbers.
  • guidType picks z.uuid() (default) or z.guid() for format: uuid.
  • regexType writes a pattern as a regex literal (default) or a RegExp constructor.
  • typeGuards adds is* type guards and assert* assertions.
  • compile wraps schemas in z.compile for faster validation. Circular schemas are left as they are.

See Options for every option and its default.

Example

The config below generates the schemas with a z.infer type next to each one.

kubb.config.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 }),
  ],
})

Dependencies

No extension dependencies.

Used by

Extensions that list Zod as a dependency.