@kubb/swagger-zod 🦙 ​
With the Swagger Zod plugin you can use Zod to validate your schema's based on a Swagger file.
Installation ​
bun add @kubb/swagger-zod @kubb/swagger
pnpm add @kubb/swagger-zod @kubb/swagger
npm install @kubb/swagger-zod @kubb/swagger
yarn add @kubb/swagger-zod @kubb/swagger
Options ​
output ​
Relative path to save the Zod schemas. When output is a file it will save all models inside that file else it will create a file per schema item.
INFO
Type: string
Default: 'zod'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
output: {
path: './zod',
},
},
),
],
})
output.exportAs ​
Name to be used for the export * as from './'
INFO
Type: string
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
output: {
exortAs: 'schemas',
},
},
),
],
})
output.extName ​
Add an extension to the generated imports and exports, default it will not use an extension
INFO
Type: string
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
output: {
extName: '.js',
},
},
),
],
})
output.exportType ​
Define what needs to exported, here you can also disable the export of barrel files
INFO
Type: 'barrel' | 'barrelNamed' | false
group ​
Group the Zod schemas based on the provided name.
group.type ​
Tag will group based on the operation tag inside the Swagger file.
Type: 'tag'
Required: true
group.output ​
Relative path to save the grouped Zod schemas. {{tag}}
will be replaced by the current tagName.
Type: string
Example: zod/{{tag}}Controller
=> zod/PetController
Default: '${output}/{{tag}}Controller'
group.exportAs ​
Name to be used for the export * as {{exportAs}} from './
Type: string
Default: '{{tag}}Schemas'
INFO
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
output: {
path: './schemas',
},
group: { type: 'tag', output: './schemas/{{tag}}Schemas' },
},
),
],
})
include ​
Array containing include parameters to include tags/operations/methods/paths.
type
export type Include = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
INFO
Type: Array<Include>
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
include: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
exclude ​
Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
type
export type Exclude = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
}
INFO
Type: Array<Exclude>
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
exclude: [
{
type: 'tag',
pattern: 'store',
},
],
},
),
],
})
override ​
Array containing override parameters to override options
based on tags/operations/methods/paths.
type
export type Override = {
type: 'tag' | 'operationId' | 'path' | 'method'
pattern: string | RegExp
options: PluginOptions
}
INFO
Type: Array<Override>
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerTS({}),
createSwaggerZod(
{
override: [
{
type: 'tag',
pattern: 'pet',
options: {
output: {
path: './custom',
},
},
},
],
},
),
],
})
typed ​
Use TypeScript(@kubb/swagger-ts
) to add type annotation.
INFO
Type: boolean
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
import { definePlugin as createSwaggerTS } from '@kubb/swagger-ts'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
typed: true,
},
),
createSwaggerTs(
{},
),
],
})
dateType ​
Choose to use date
or datetime
as JavaScript Date
instead of string
.
See datetimes.
type
z.string()
z.string().datetime()
z.string().datetime({ offset: true })
z.string().datetime({ local: true })
z.date()
INFO
Type: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
Default: 'string'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: false,
}),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: 'string',
}),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: 'stringOffset',
}),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: 'stringLocal',
}),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
dateType: 'date',
}),
],
})
unknownType ​
Which type to use when the Swagger/OpenAPI file is not providing more information.
type
any
unknown
INFO
Type: 'any' | 'unknown'
Default: 'any'
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
unknownType: 'any',
}),
],
})
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod({
unknownType: 'unknown',
}),
],
})
transformers ​
transformers.name ​
Override the name of the Zod schema that is getting generated, this will also override the name of the file.
INFO
Type: (name: string, type?: "function" | "type" | "file" ) => string
import { defineConfig } from '@kubb/core'
import { definePlugin as createSwagger } from '@kubb/swagger'
import { definePlugin as createSwaggerZod } from '@kubb/swagger-zod'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
transformers: {
name: (name) => {
return `${name}Client`
},
},
},
),
],
})
templates ​
Make it possible to override one of the templates.
TIP
See templates for more information about creating templates.
Set false
to disable a template.
type
import type { Operations } from '@kubb/swagger-zod/components'
export type Templates = {
operations: typeof Operations.templates | false
}
INFO
Type: Templates
import { defineConfig } from '@kubb/core'
import createSwagger from '@kubb/swagger'
import createSwaggerZod from '@kubb/swagger-zod'
import { templates } from './CustomTemplate'
export default defineConfig({
input: {
path: './petStore.yaml',
},
output: {
path: './src/gen',
},
plugins: [
createSwagger({ output: false }),
createSwaggerZod(
{
templates,
},
),
],
})