@kubb/plugin-zod 5.0.0-beta.42 → 5.0.0-beta.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/extension.yaml DELETED
@@ -1,970 +0,0 @@
1
- $schema: https://kubb.dev/schemas/extension.json
2
- kind: plugin
3
- id: plugin-zod
4
- name: Zod
5
- description: Generate Zod v4 schemas from OpenAPI for runtime validation that stays in sync with your TypeScript types.
6
- category: validation
7
- type: official
8
- npmPackage: '@kubb/plugin-zod'
9
- docsPath: /plugins/plugin-zod
10
- repo: https://github.com/kubb-labs/plugins
11
- maintainers:
12
- - name: Stijn Van Hulle
13
- github: stijnvanhulle
14
- compatibility:
15
- kubb: '>=5.0.0'
16
- node: '>=22'
17
- tags:
18
- - zod
19
- - validation
20
- - schema
21
- - runtime-validation
22
- - codegen
23
- - openapi
24
- dependencies: []
25
- resources:
26
- documentation: https://kubb.dev/plugins/plugin-zod
27
- repository: https://github.com/kubb-labs/plugins
28
- issues: https://github.com/kubb-labs/plugins/issues
29
- changelog: https://github.com/kubb-labs/plugins/blob/main/packages/plugin-zod/CHANGELOG.md
30
- codesandbox: https://codesandbox.io/p/github/kubb-labs/plugins/main/examples/zod
31
- featured: true
32
- icon:
33
- light: https://kubb.dev/feature/zod.svg
34
- intro: |
35
- # @kubb/plugin-zod
36
-
37
- Generate [Zod](https://zod.dev/) v4 schemas from your OpenAPI spec. Use them to validate API responses at runtime, build form schemas, or feed back into router libraries that consume Zod (`tRPC`, `Hono`, `Elysia`).
38
-
39
- Pair with `@kubb/plugin-client` and set the client's `parser: 'zod'` to validate every response automatically.
40
- options:
41
- - name: output
42
- type: Output
43
- required: false
44
- default: "{ path: 'zod', barrel: { type: 'named' } }"
45
- description: Where the generated Zod schemas are written and how they are exported.
46
- properties:
47
- - name: path
48
- type: string
49
- required: true
50
- description: |
51
- Folder (or single file) where the plugin writes its generated code. The path is resolved against the global `output.path` set on `defineConfig`.
52
-
53
- Use a folder to keep each generator's output isolated (`'types'`, `'clients'`, `'hooks'`). Use a single file when you want everything in one place, for example `'api.ts'`.
54
- tip: |
55
- When `output.path` points to a single file, the `group` option cannot be used because every operation ends up in the same file.
56
- examples:
57
- - name: kubb.config.ts
58
- files:
59
- - lang: typescript
60
- twoslash: false
61
- code: |
62
- import { defineConfig } from 'kubb'
63
- import { pluginTs } from '@kubb/plugin-ts'
64
-
65
- export default defineConfig({
66
- input: { path: './petStore.yaml' },
67
- output: { path: './src/gen' },
68
- plugins: [
69
- pluginTs({
70
- output: { path: './types' },
71
- }),
72
- ],
73
- })
74
- - name: Resulting tree
75
- files:
76
- - lang: text
77
- twoslash: false
78
- code: |
79
- src/
80
- └── gen/
81
- └── types/
82
- ├── Pet.ts
83
- └── Store.ts
84
- default: "'zod'"
85
- - name: barrel
86
- type: "{ type: 'named' | 'all', nested?: boolean } | false"
87
- required: false
88
- default: "{ type: 'named' }"
89
- description: |
90
- Controls how the generated `index.ts` (barrel) file re-exports the plugin's output.
91
-
92
- - `{ type: 'named' }` re-exports each symbol by name. Best for tree-shaking and explicit imports.
93
- - `{ type: 'all' }` uses `export *`. Smaller barrel file, but exports everything.
94
- - `{ nested: true }` creates a barrel in every subdirectory, so callers can import from any depth.
95
- - `false` skips the barrel entirely. The plugin's files are also excluded from the root `index.ts`.
96
- tip: |
97
- Pick `'named'` when consumers care about which symbols they import (better tree-shaking, friendlier auto-import). Pick `'all'` when the file count is small and you want a one-line barrel.
98
- examples:
99
- - name: "'named' (default)"
100
- files:
101
- - name: kubb.config.ts
102
- lang: typescript
103
- twoslash: false
104
- code: |
105
- import { defineConfig } from 'kubb'
106
- import { pluginTs } from '@kubb/plugin-ts'
107
-
108
- export default defineConfig({
109
- input: { path: './petStore.yaml' },
110
- output: { path: './src/gen' },
111
- plugins: [
112
- pluginTs({
113
- output: { barrel: { type: 'named' } },
114
- }),
115
- ],
116
- })
117
- - name: src/gen/types/index.ts
118
- lang: typescript
119
- twoslash: false
120
- code: |
121
- export { Pet, PetStatus } from './Pet'
122
- export { Store } from './Store'
123
- - name: "'all'"
124
- files:
125
- - name: kubb.config.ts
126
- lang: typescript
127
- twoslash: false
128
- code: |
129
- import { defineConfig } from 'kubb'
130
- import { pluginTs } from '@kubb/plugin-ts'
131
-
132
- export default defineConfig({
133
- input: { path: './petStore.yaml' },
134
- output: { path: './src/gen' },
135
- plugins: [
136
- pluginTs({
137
- output: { barrel: { type: 'all' } },
138
- }),
139
- ],
140
- })
141
- - name: src/gen/types/index.ts
142
- lang: typescript
143
- twoslash: false
144
- code: |
145
- export * from './Pet'
146
- export * from './Store'
147
- - name: nested
148
- files:
149
- - name: kubb.config.ts
150
- lang: typescript
151
- twoslash: false
152
- code: |
153
- import { defineConfig } from 'kubb'
154
- import { pluginTs } from '@kubb/plugin-ts'
155
-
156
- export default defineConfig({
157
- input: { path: './petStore.yaml' },
158
- output: { path: './src/gen' },
159
- plugins: [
160
- pluginTs({
161
- output: { barrel: { type: 'named', nested: true } },
162
- }),
163
- ],
164
- })
165
- - name: Generated tree
166
- lang: text
167
- twoslash: false
168
- code: |
169
- src/gen/types/
170
- ├── index.ts # re-exports ./petController and ./storeController
171
- ├── petController/
172
- │ ├── index.ts # re-exports Pet, Store, ...
173
- │ └── Pet.ts
174
- └── storeController/
175
- ├── index.ts
176
- └── Store.ts
177
- - name: 'false'
178
- files:
179
- - name: kubb.config.ts
180
- lang: typescript
181
- twoslash: false
182
- code: |
183
- import { defineConfig } from 'kubb'
184
- import { pluginTs } from '@kubb/plugin-ts'
185
-
186
- export default defineConfig({
187
- input: { path: './petStore.yaml' },
188
- output: { path: './src/gen' },
189
- plugins: [
190
- pluginTs({
191
- output: { barrel: false },
192
- }),
193
- ],
194
- })
195
- - name: Result
196
- lang: text
197
- twoslash: false
198
- code: |
199
- # No index.ts is generated for this plugin.
200
- # Its files are also excluded from the root index.ts.
201
- - name: banner
202
- type: 'string | ((node: RootNode) => string)'
203
- required: false
204
- description: |
205
- Text prepended to every generated file. Useful for license headers, lint disables, or `@ts-nocheck` directives.
206
-
207
- Pass a string for a static banner. Pass a function to compute the banner from each file's `RootNode` (the AST root containing path, schema, and operation context).
208
- examples:
209
- - name: Static banner
210
- files:
211
- - name: kubb.config.ts
212
- lang: typescript
213
- twoslash: false
214
- code: |
215
- import { defineConfig } from 'kubb'
216
- import { pluginTs } from '@kubb/plugin-ts'
217
-
218
- export default defineConfig({
219
- input: { path: './petStore.yaml' },
220
- output: { path: './src/gen' },
221
- plugins: [
222
- pluginTs({
223
- output: {
224
- banner: '/* eslint-disable */\n// @ts-nocheck',
225
- },
226
- }),
227
- ],
228
- })
229
- - name: Generated file
230
- lang: typescript
231
- twoslash: false
232
- code: |
233
- /* eslint-disable */
234
- // @ts-nocheck
235
- export type Pet = {
236
- id: number
237
- name: string
238
- }
239
- - name: Dynamic banner
240
- files:
241
- - name: kubb.config.ts
242
- lang: typescript
243
- twoslash: false
244
- code: |
245
- import { defineConfig } from 'kubb'
246
- import { pluginTs } from '@kubb/plugin-ts'
247
-
248
- export default defineConfig({
249
- input: { path: './petStore.yaml' },
250
- output: { path: './src/gen' },
251
- plugins: [
252
- pluginTs({
253
- output: {
254
- banner: (node) => `// Source: ${node.path}\n// Generated at ${new Date().toISOString()}`,
255
- },
256
- }),
257
- ],
258
- })
259
- - name: footer
260
- type: 'string | ((node: RootNode) => string)'
261
- required: false
262
- description: |
263
- Text appended at the end of every generated file. The mirror of `banner` — use it for closing comments, re-enabling lint rules, or marker lines.
264
-
265
- Pass a string for a static footer, or a function that receives the file's `RootNode` and returns the footer text.
266
- examples:
267
- - name: Re-enable lint after a banner disable
268
- files:
269
- - name: kubb.config.ts
270
- lang: typescript
271
- twoslash: false
272
- code: |
273
- import { defineConfig } from 'kubb'
274
- import { pluginTs } from '@kubb/plugin-ts'
275
-
276
- export default defineConfig({
277
- input: { path: './petStore.yaml' },
278
- output: { path: './src/gen' },
279
- plugins: [
280
- pluginTs({
281
- output: {
282
- banner: '/* eslint-disable */',
283
- footer: '/* eslint-enable */',
284
- },
285
- }),
286
- ],
287
- })
288
- - name: override
289
- type: boolean
290
- required: false
291
- default: 'false'
292
- description: |
293
- Allows the plugin to overwrite hand-written files that share a name with a generated file.
294
-
295
- - `false` (default): Kubb skips a file if it already exists and is not marked as generated. This protects manual edits.
296
- - `true`: Kubb overwrites any file at the target path, including hand-written ones.
297
- warning: |
298
- Enable this only when you are sure the target folder contains nothing you need to keep. Local edits are lost on the next generation.
299
- examples:
300
- - name: kubb.config.ts
301
- files:
302
- - lang: typescript
303
- twoslash: false
304
- code: |
305
- import { defineConfig } from 'kubb'
306
- import { pluginTs } from '@kubb/plugin-ts'
307
-
308
- export default defineConfig({
309
- input: { path: './petStore.yaml' },
310
- output: { path: './src/gen' },
311
- plugins: [
312
- pluginTs({
313
- output: { override: true },
314
- }),
315
- ],
316
- })
317
- - name: resolver
318
- type: Partial<ResolverZod> & ThisType<ResolverZod>
319
- required: false
320
- description: |
321
- Overrides how the plugin builds names and paths for generated files and symbols. Use this to add prefixes, suffixes, or to swap the casing strategy without forking the plugin.
322
-
323
- Only override the methods you want to change. Anything you omit falls back to the plugin's default resolver. A method that returns `null` or `undefined` also falls back.
324
-
325
- Inside each method, `this` is bound to the full resolver, so you can call `this.default(name, 'function')` to delegate to the built-in implementation.
326
- body: |
327
- Each plugin ships with a default resolver:
328
-
329
- | Plugin | Default resolver |
330
- | --- | --- |
331
- | `@kubb/plugin-ts` | `resolverTs` |
332
- | `@kubb/plugin-zod` | `resolverZod` |
333
- | `@kubb/plugin-faker` | `resolverFaker` |
334
- | `@kubb/plugin-cypress` | `resolverCypress` |
335
- | `@kubb/plugin-msw` | `resolverMsw` |
336
- | `@kubb/plugin-mcp` | `resolverMcp` |
337
- | `@kubb/plugin-client` | `resolverClient` |
338
- codeBlock:
339
- lang: typescript
340
- title: Add an Api prefix to every name
341
- code: |
342
- import { defineConfig } from 'kubb'
343
- import { pluginTs } from '@kubb/plugin-ts'
344
-
345
- export default defineConfig({
346
- input: { path: './petStore.yaml' },
347
- output: { path: './src/gen' },
348
- plugins: [
349
- pluginTs({
350
- resolver: {
351
- resolveName(name) {
352
- return `Api${this.default(name, 'function')}`
353
- },
354
- },
355
- }),
356
- ],
357
- })
358
- tip: |
359
- Use `resolver` for naming and file-location tweaks. For changing the AST nodes themselves (e.g. stripping descriptions), use `transformer` instead.
360
- - name: group
361
- type: Group
362
- required: false
363
- description: |
364
- Splits generated files into subfolders based on the operation's tag, so each tag in your OpenAPI spec gets its own directory.
365
-
366
- Without `group`, every file lands in the plugin's `output.path` folder. With `group`, files are bucketed under `{output.path}/{groupName}/`, where `groupName` is derived from the operation's first tag.
367
- tip: |
368
- Use `group` to mirror your API's domain structure (pet, store, user) in the generated code. Combine it with `output.barrel: { type: 'named', nested: true }` to get per-tag barrel files.
369
- examples:
370
- - name: kubb.config.ts
371
- files:
372
- - lang: typescript
373
- twoslash: false
374
- code: |
375
- import { defineConfig } from 'kubb'
376
- import { pluginTs } from '@kubb/plugin-ts'
377
-
378
- export default defineConfig({
379
- input: { path: './petStore.yaml' },
380
- output: { path: './src/gen' },
381
- plugins: [
382
- pluginTs({
383
- group: {
384
- type: 'tag',
385
- name: ({ group }) => `${group}Controller`,
386
- },
387
- }),
388
- ],
389
- })
390
- body: |
391
- With the configuration above, the generator emits:
392
-
393
- ```text
394
- src/gen/
395
- ├── petController/
396
- │ ├── AddPet.ts
397
- │ └── GetPet.ts
398
- └── storeController/
399
- ├── CreateStore.ts
400
- └── GetStoreById.ts
401
- ```
402
- properties:
403
- - name: type
404
- type: "'tag'"
405
- required: true
406
- description: |
407
- Property used to assign each operation to a group. Required whenever `group` is set.
408
-
409
- Today only `'tag'` is supported: Kubb reads the first tag on the operation (`operation.getTags().at(0)?.name`) and uses it as the group key. Operations without a tag are placed in a default group.
410
- note: |
411
- `Required: true*` is conditional — only required when the parent `group` option is used. `group` itself stays optional.
412
- - name: name
413
- type: '(context: GroupContext) => string'
414
- required: false
415
- default: (ctx) => `${ctx.group}Controller`
416
- description: Function that builds the folder/identifier name from a group key (the operation's first tag).
417
- - name: importPath
418
- type: string
419
- required: false
420
- default: "'zod'"
421
- description: |
422
- Module specifier used in the `import { z } from '...'` statement at the top of generated files.
423
-
424
- Use `'zod/mini'` to import from the tree-shakeable Mini bundle, or a custom path when re-exporting Zod from your own module.
425
- examples:
426
- - name: Use Zod's mini bundle
427
- files:
428
- - lang: typescript
429
- twoslash: false
430
- code: |
431
- import { defineConfig } from 'kubb'
432
- import { pluginZod } from '@kubb/plugin-zod'
433
-
434
- export default defineConfig({
435
- input: { path: './petStore.yaml' },
436
- output: { path: './src/gen' },
437
- plugins: [
438
- pluginZod({ importPath: 'zod/mini' }),
439
- ],
440
- })
441
- - name: typed
442
- type: boolean
443
- required: false
444
- default: 'false'
445
- description: |
446
- Adds a type annotation that ties each Zod schema to its TypeScript counterpart from `@kubb/plugin-ts`.
447
-
448
- With `typed: true`, the generated `petSchema` is typed as `ToZod<Pet>` — TypeScript will fail compilation when the schema drifts from the type. Requires `@kubb/plugin-ts` in the plugins list.
449
- important: |
450
- The mapping uses a [ToZod-style](https://github.com/colinhacks/tozod) helper (vendored in Kubb) to derive a Zod shape from a TypeScript type.
451
- examples:
452
- - name: Schema linked to its TS type
453
- files:
454
- - lang: typescript
455
- twoslash: false
456
- code: |
457
- import { z } from 'zod'
458
- import type { ToZod } from '@kubb/plugin-zod'
459
- import type { Pet } from '../ts/Pet'
460
-
461
- export const petSchema: ToZod<Pet> = z.object({
462
- name: z.string(),
463
- status: z.enum(['available', 'pending', 'sold']).optional(),
464
- })
465
- - name: inferred
466
- type: boolean
467
- required: false
468
- default: 'false'
469
- description: |
470
- Exports a `z.infer<typeof schema>` type alias next to every generated schema.
471
-
472
- Use this when you want one source of truth (the Zod schema) and a TypeScript type derived from it, instead of importing types separately from `@kubb/plugin-ts`.
473
- codeBlock:
474
- lang: typescript
475
- title: With inferred enabled
476
- code: |
477
- import { z } from 'zod'
478
-
479
- export const petSchema = z.object({
480
- name: z.string(),
481
- status: z.enum(['available', 'pending', 'sold']).optional(),
482
- })
483
-
484
- export type Pet = z.infer<typeof petSchema>
485
- - name: integerType
486
- warning: |
487
- Moved to [`adapterOas`](/adapters/adapter-oas#integerType). Use `adapterOas({ integerType })` instead.
488
- - name: unknownType
489
- warning: |
490
- Moved to [`adapterOas`](/adapters/adapter-oas#unknownType). Use `adapterOas({ unknownType })` instead.
491
- - name: emptySchemaType
492
- warning: |
493
- Moved to [`adapterOas`](/adapters/adapter-oas#emptySchemaType). Use `adapterOas({ emptySchemaType })` instead.
494
- - name: coercion
495
- type: 'boolean | { dates?: boolean, strings?: boolean, numbers?: boolean }'
496
- required: false
497
- default: 'false'
498
- description: |
499
- Wraps schemas in `z.coerce` so input is coerced to the expected type before validation. Useful for form data, query params, and any source where everything arrives as a string.
500
-
501
- - `true` — coerce strings, numbers, and dates.
502
- - `false` (default) — no coercion. Strict validation.
503
- - Object — pick which primitives to coerce.
504
-
505
- See [Coercion for primitives](https://zod.dev/?id=coercion-for-primitives).
506
- tip: |
507
- When `@kubb/adapter-oas` runs with `dateType: 'date'` (date fields typed as `Date`), the generated schemas round-trip dates at the validation boundary rather than coercing: response schemas decode the ISO `string` into a `Date` (`z.iso.datetime().transform(...)`), and an `${name}InputSchema` variant encodes `Date` back into an ISO `string` (`z.date().transform(...)`) for request bodies. `coercion.dates` has no effect on these fields.
508
- examples:
509
- - name: '`coercion: true`'
510
- files:
511
- - lang: typescript
512
- twoslash: false
513
- code: |
514
- z.coerce.string()
515
- z.coerce.date()
516
- z.coerce.number()
517
- - name: '`coercion: false` (default)'
518
- files:
519
- - lang: typescript
520
- twoslash: false
521
- code: |
522
- z.string()
523
- z.date()
524
- z.number()
525
- - name: Coerce numbers only
526
- files:
527
- - lang: typescript
528
- twoslash: false
529
- code: |
530
- // { numbers: true, strings: false, dates: false }
531
- z.string()
532
- z.date()
533
- z.coerce.number()
534
- - name: operations
535
- type: boolean
536
- required: false
537
- default: 'false'
538
- description: |
539
- Emits an `operations.ts` file that groups schemas per operation: request body, query params, path params, and each response status.
540
-
541
- Use this to validate or describe whole operations in one place — handy when wiring Kubb output into a server framework that takes Zod schemas per route.
542
- - name: paramsCasing
543
- type: "'camelcase'"
544
- required: false
545
- description: |
546
- Renames properties inside the path/query/header schemas to the chosen casing. Body schemas are unaffected.
547
-
548
- Must match the value of `paramsCasing` on `@kubb/plugin-ts` so the generated Zod schemas stay assignable to the generated types.
549
- codeBlock:
550
- lang: typescript
551
- title: "`paramsCasing: 'camelcase'`"
552
- code: |
553
- // OpenAPI spec uses: pet_id, X-Api-Key
554
- export const getPetPathParamsSchema = z.object({
555
- petId: z.string(),
556
- })
557
-
558
- export const getPetHeaderParamsSchema = z.object({
559
- xApiKey: z.string().optional(),
560
- })
561
- - name: guidType
562
- type: "'uuid' | 'guid'"
563
- required: false
564
- default: "'uuid'"
565
- description: |
566
- Validator used for OpenAPI properties with `format: uuid`.
567
-
568
- - `'uuid'` (default) — `z.uuid()`. Standard RFC 4122 UUID.
569
- - `'guid'` — `z.guid()`. Looser; accepts Microsoft-style GUIDs (allows lowercase, mixed brace styles).
570
- examples:
571
- - name: "'uuid' (default)"
572
- files:
573
- - lang: typescript
574
- twoslash: false
575
- code: |
576
- z.uuid()
577
- - name: "'guid'"
578
- files:
579
- - lang: typescript
580
- twoslash: false
581
- code: |
582
- z.guid()
583
- - name: mini
584
- type: boolean
585
- required: false
586
- default: 'false'
587
- badge:
588
- type: tip
589
- text: beta
590
- description: |
591
- Switches code generation to [Zod Mini](https://zod.dev/packages/mini). Schemas use the functional API (`z.optional(z.string())`) instead of the chainable one (`z.string().optional()`), which lets bundlers tree-shake unused validators.
592
-
593
- Setting `mini: true` also defaults `importPath` to `'zod/mini'`.
594
- warning: |
595
- Zod Mini is currently in beta. Its API may change in a future release.
596
- tip: |
597
- Use Zod Mini in code that ships to the browser. The functional API drops several kilobytes from the bundle compared to the standard Zod build.
598
- examples:
599
- - name: '`mini: true`'
600
- files:
601
- - lang: typescript
602
- twoslash: false
603
- code: |
604
- import { z } from 'zod/mini'
605
-
606
- z.optional(z.string())
607
- z.nullable(z.number())
608
- z.array(z.string()).check(z.minLength(1), z.maxLength(10))
609
- - name: '`mini: false` (default)'
610
- files:
611
- - lang: typescript
612
- twoslash: false
613
- code: |
614
- import { z } from 'zod'
615
-
616
- z.string().optional()
617
- z.number().nullable()
618
- z.array(z.string()).min(1).max(10)
619
- - name: include
620
- type: Array<Include>
621
- required: false
622
- description: |
623
- Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
624
-
625
- Each entry filters by one of:
626
-
627
- - `tag` — the operation's first tag in the OpenAPI spec.
628
- - `operationId` — the operation's `operationId`.
629
- - `path` — the URL pattern (`'/pet/{petId}'`).
630
- - `method` — HTTP method (`'get'`, `'post'`, ...).
631
- - `contentType` — the media type of the request body.
632
-
633
- `pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
634
- codeBlock:
635
- lang: typescript
636
- title: Type definition
637
- code: |
638
- export type Include = {
639
- type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
640
- pattern: string | RegExp
641
- }
642
- examples:
643
- - name: Only the pet tag
644
- files:
645
- - name: kubb.config.ts
646
- lang: typescript
647
- twoslash: false
648
- code: |
649
- import { defineConfig } from 'kubb'
650
- import { pluginTs } from '@kubb/plugin-ts'
651
-
652
- export default defineConfig({
653
- input: { path: './petStore.yaml' },
654
- output: { path: './src/gen' },
655
- plugins: [
656
- pluginTs({
657
- include: [
658
- { type: 'tag', pattern: 'pet' },
659
- ],
660
- }),
661
- ],
662
- })
663
- - name: Only GET operations under /pet
664
- files:
665
- - name: kubb.config.ts
666
- lang: typescript
667
- twoslash: false
668
- code: |
669
- import { defineConfig } from 'kubb'
670
- import { pluginTs } from '@kubb/plugin-ts'
671
-
672
- export default defineConfig({
673
- input: { path: './petStore.yaml' },
674
- output: { path: './src/gen' },
675
- plugins: [
676
- pluginTs({
677
- include: [
678
- { type: 'method', pattern: 'get' },
679
- { type: 'path', pattern: /^\/pet/ },
680
- ],
681
- }),
682
- ],
683
- })
684
- - name: exclude
685
- type: Array<Exclude>
686
- required: false
687
- description: |
688
- Skips any operation that matches at least one entry in the list. The opposite of `include`.
689
-
690
- Each entry filters by one of:
691
-
692
- - `tag` — the operation's first tag.
693
- - `operationId` — the operation's `operationId`.
694
- - `path` — the URL pattern (`'/pet/{petId}'`).
695
- - `method` — HTTP method (`'get'`, `'post'`, ...).
696
- - `contentType` — the media type of the request body.
697
-
698
- `pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
699
- codeBlock:
700
- lang: typescript
701
- title: Type definition
702
- code: |
703
- export type Exclude = {
704
- type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
705
- pattern: string | RegExp
706
- }
707
- examples:
708
- - name: Skip everything under the store tag
709
- files:
710
- - name: kubb.config.ts
711
- lang: typescript
712
- twoslash: false
713
- code: |
714
- import { defineConfig } from 'kubb'
715
- import { pluginTs } from '@kubb/plugin-ts'
716
-
717
- export default defineConfig({
718
- input: { path: './petStore.yaml' },
719
- output: { path: './src/gen' },
720
- plugins: [
721
- pluginTs({
722
- exclude: [
723
- { type: 'tag', pattern: 'store' },
724
- ],
725
- }),
726
- ],
727
- })
728
- - name: Skip a specific operation and all delete methods
729
- files:
730
- - name: kubb.config.ts
731
- lang: typescript
732
- twoslash: false
733
- code: |
734
- import { defineConfig } from 'kubb'
735
- import { pluginTs } from '@kubb/plugin-ts'
736
-
737
- export default defineConfig({
738
- input: { path: './petStore.yaml' },
739
- output: { path: './src/gen' },
740
- plugins: [
741
- pluginTs({
742
- exclude: [
743
- { type: 'operationId', pattern: 'deletePet' },
744
- { type: 'method', pattern: 'delete' },
745
- ],
746
- }),
747
- ],
748
- })
749
- - name: override
750
- type: Array<Override>
751
- required: false
752
- description: |
753
- Applies a different set of plugin options to operations that match a pattern. Use this when most of your API should follow the global config, but a handful of endpoints need different treatment.
754
-
755
- Each entry has the same `type` and `pattern` shape as `include`/`exclude`, plus an `options` object that overrides the plugin's options for matched operations.
756
-
757
- Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
758
- codeBlock:
759
- lang: typescript
760
- title: Type definition
761
- code: |
762
- export type Override = {
763
- type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
764
- pattern: string | RegExp
765
- options: PluginOptions
766
- }
767
- examples:
768
- - name: Use a different enum style for the user tag
769
- files:
770
- - name: kubb.config.ts
771
- lang: typescript
772
- twoslash: false
773
- code: |
774
- import { defineConfig } from 'kubb'
775
- import { pluginTs } from '@kubb/plugin-ts'
776
-
777
- export default defineConfig({
778
- input: { path: './petStore.yaml' },
779
- output: { path: './src/gen' },
780
- plugins: [
781
- pluginTs({
782
- enumType: 'asConst',
783
- override: [
784
- {
785
- type: 'tag',
786
- pattern: 'user',
787
- options: { enumType: 'literal' },
788
- },
789
- ],
790
- }),
791
- ],
792
- })
793
- - name: generators
794
- type: Array<Generator<PluginZod>>
795
- required: false
796
- experimental: true
797
- description: |
798
- Adds custom generators that run alongside the plugin's built-in generators. Each generator can emit additional files or post-process existing ones using the plugin's AST and options.
799
-
800
- Use this when you need output the plugin does not produce out of the box (a custom client wrapper, an extra index, a metadata file). For end-to-end guidance, see [Creating plugins](https://kubb.dev/docs/5.x/guides/creating-plugins).
801
- warning: |
802
- Generators are an experimental, low-level API. The signature may change between minor releases.
803
- - name: transformer
804
- type: Visitor
805
- required: false
806
- description: |
807
- Modifies AST nodes before they are printed to source code. Use this when you need to rewrite operation IDs, drop descriptions, or change schema metadata without forking the generator.
808
-
809
- Each visitor method (e.g. `schema`, `operation`) receives the node and a context object. Return a new node to replace it, or return `undefined` to leave it untouched. Methods you omit keep the plugin's default behavior.
810
- examples:
811
- - name: Strip descriptions before printing
812
- files:
813
- - name: kubb.config.ts
814
- lang: typescript
815
- twoslash: false
816
- code: |
817
- import { defineConfig } from 'kubb'
818
- import { pluginTs } from '@kubb/plugin-ts'
819
-
820
- export default defineConfig({
821
- input: { path: './petStore.yaml' },
822
- output: { path: './src/gen' },
823
- plugins: [
824
- pluginTs({
825
- transformer: {
826
- schema(node) {
827
- return { ...node, description: undefined }
828
- },
829
- },
830
- }),
831
- ],
832
- })
833
- - name: Prefix every operationId
834
- files:
835
- - name: kubb.config.ts
836
- lang: typescript
837
- twoslash: false
838
- code: |
839
- import { defineConfig } from 'kubb'
840
- import { pluginTs } from '@kubb/plugin-ts'
841
-
842
- export default defineConfig({
843
- input: { path: './petStore.yaml' },
844
- output: { path: './src/gen' },
845
- plugins: [
846
- pluginTs({
847
- transformer: {
848
- operation(node) {
849
- return { ...node, operationId: `api_${node.operationId}` }
850
- },
851
- },
852
- }),
853
- ],
854
- })
855
- tip: |
856
- Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
857
- - name: printer
858
- type: '{ nodes?: PrinterZodNodes | PrinterZodMiniNodes }'
859
- required: false
860
- description: |
861
- Replaces the Zod handler for a specific schema type (e.g. `'integer'`, `'date'`, `'string'`). Each handler returns the Zod expression as a string.
862
-
863
- When `mini: true`, overrides target the Zod Mini printer; otherwise they target the standard Zod printer.
864
- examples:
865
- - name: Use z.number() for integers
866
- files:
867
- - lang: typescript
868
- twoslash: false
869
- code: |
870
- import { defineConfig } from 'kubb'
871
- import { pluginZod } from '@kubb/plugin-zod'
872
-
873
- export default defineConfig({
874
- input: { path: './petStore.yaml' },
875
- output: { path: './src/gen' },
876
- plugins: [
877
- pluginZod({
878
- printer: {
879
- nodes: {
880
- integer() {
881
- return 'z.number()'
882
- },
883
- },
884
- },
885
- }),
886
- ],
887
- })
888
- - name: Use z.string().date() for date schemas
889
- files:
890
- - lang: typescript
891
- twoslash: false
892
- code: |
893
- import { defineConfig } from 'kubb'
894
- import { pluginZod } from '@kubb/plugin-zod'
895
-
896
- export default defineConfig({
897
- input: { path: './petStore.yaml' },
898
- output: { path: './src/gen' },
899
- plugins: [
900
- pluginZod({
901
- printer: {
902
- nodes: {
903
- date() {
904
- return 'z.string().date()'
905
- },
906
- },
907
- },
908
- }),
909
- ],
910
- })
911
- - name: wrapOutput
912
- type: '(arg: { output: string; schema: SchemaNode }) => string | undefined'
913
- required: false
914
- description: |
915
- Lets you wrap the generated Zod schema string with extra calls before it is written to disk. The callback receives the raw schema output and the originating `SchemaNode`.
916
-
917
- Return a new string to replace the output, or return `undefined` to leave it untouched.
918
- tip: |
919
- Use this to round-trip metadata from OpenAPI back into Zod — examples, descriptions, or `.openapi()` annotations for libraries that re-emit OpenAPI from Zod schemas.
920
- codeBlock:
921
- lang: typescript
922
- title: Append .openapi() with metadata
923
- code: |
924
- import { defineConfig } from 'kubb'
925
- import { pluginZod } from '@kubb/plugin-zod'
926
-
927
- export default defineConfig({
928
- input: { path: './petStore.yaml' },
929
- output: { path: './src/gen' },
930
- plugins: [
931
- pluginZod({
932
- wrapOutput: ({ output, schema }) => {
933
- const metadata: Record<string, unknown> = {}
934
-
935
- if (schema.keywords?.includes('example')) {
936
- // Pull keyword metadata off the SchemaNode here
937
- }
938
-
939
- if (Object.keys(metadata).length > 0) {
940
- return `${output}.openapi(${JSON.stringify(metadata)})`
941
- }
942
-
943
- return undefined
944
- },
945
- }),
946
- ],
947
- })
948
- examples:
949
- - name: kubb.config.ts
950
- files:
951
- - lang: typescript
952
- twoslash: false
953
- code: |
954
- import { defineConfig } from 'kubb'
955
- import { pluginTs } from '@kubb/plugin-ts'
956
- import { pluginZod } from '@kubb/plugin-zod'
957
-
958
- export default defineConfig({
959
- input: { path: './petStore.yaml' },
960
- output: { path: './src/gen' },
961
- plugins: [
962
- pluginTs(),
963
- pluginZod({
964
- output: { path: './zod' },
965
- group: { type: 'tag', name: ({ group }) => `${group}Schemas` },
966
- typed: true,
967
- importPath: 'zod',
968
- }),
969
- ],
970
- })