Skip to content

@kubb/swagger-zod 🦙 ​

With the Swagger Zod plugin you can use Zod to validate your schema's based on a Swagger file.

Installation ​

shell
bun add @kubb/swagger-zod @kubb/swagger
shell
pnpm add @kubb/swagger-zod @kubb/swagger
shell
npm install @kubb/swagger-zod @kubb/swagger
shell
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'

typescript
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

typescript
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

typescript
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

typescript
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

typescript
export type Include = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
}

INFO

Type: Array<Include>

typescript
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

typescript
export type Exclude = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
}

INFO

Type: Array<Exclude>

typescript
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

typescript
export type Override = {
  type: 'tag' | 'operationId' | 'path' | 'method'
  pattern: string | RegExp
  options: PluginOptions
}

INFO

Type: Array<Override>

typescript
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

typescript
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

typescript
z.string()
typescript
z.string().datetime()
typescript
z.string().datetime({ offset: true })
typescript
z.string().datetime({ local: true })
typescript
z.date()

INFO

Type: false | 'string' | 'stringOffset' | 'stringLocal' | 'date'
Default: 'string'

typescript
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,
    }),
  ],
})
typescript
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',
    }),
  ],
})
typescript
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',
    }),
  ],
})
typescript
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',
    }),
  ],
})
typescript
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

typescript
any
typescript
unknown

INFO

Type: 'any' | 'unknown'
Default: 'any'

typescript
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',
    }),
  ],
})
typescript
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

typescript
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

typescript
import type { Operations } from '@kubb/swagger-zod/components'

export type Templates = {
  operations: typeof Operations.templates | false
}

INFO

Type: Templates

typescript
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,
      },
    ),
  ],
})

Depended ​

Released under the MIT License.