Skip to content

Tree-shakeable schemas with Zod Mini

Set mini to true to generate Zod Mini schemas that use the functional API, so bundlers tree-shake the validators you never call. This also defaults the import to 'zod/mini'.

kubb.config.ts
typescript
import { 
defineConfig
} from 'kubb/config'
import {
pluginZod
} from '@kubb/plugin-zod'
export default
defineConfig
({
input
: './petStore.yaml',
output
: {
path
: './src/gen',
clean
: true },
plugins
: [
pluginZod
({
mini
: true,
}), ], })

Output example

src/gen/zod/petSchema.ts
typescript
import * as z from 'zod/mini'
import { categorySchema } from './categorySchema'
import { tagSchema } from './tagSchema'

export const petSchema = z.object({
  id: z.optional(z.bigint()),
  name: z.string(),
  get category() { return z.optional(categorySchema) },
  photoUrls: z.array(z.string()),
  tags: z.optional(z.array(tagSchema)),
  status: z.optional(z.enum(['available', 'pending', 'sold'])),
})
usage.ts
typescript
import { petSchema } from './src/gen/zod/petSchema'

// only the validators actually imported end up in the bundle
const pet = petSchema.parse({ name: 'Rex', photoUrls: [] })