@kubb/plugin-zod 5.0.0-beta.3 → 5.0.0-beta.30

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 ADDED
@@ -0,0 +1,966 @@
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-cypress` | `resolverCypress` |
334
+ | `@kubb/plugin-mcp` | `resolverMcp` |
335
+ | `@kubb/plugin-client` | `resolverClient` |
336
+ codeBlock:
337
+ lang: typescript
338
+ title: Add an Api prefix to every name
339
+ code: |
340
+ import { defineConfig } from 'kubb'
341
+ import { pluginTs } from '@kubb/plugin-ts'
342
+
343
+ export default defineConfig({
344
+ input: { path: './petStore.yaml' },
345
+ output: { path: './src/gen' },
346
+ plugins: [
347
+ pluginTs({
348
+ resolver: {
349
+ resolveName(name) {
350
+ return `Api${this.default(name, 'function')}`
351
+ },
352
+ },
353
+ }),
354
+ ],
355
+ })
356
+ tip: |
357
+ Use `resolver` for naming and file-location tweaks. For changing the AST nodes themselves (e.g. stripping descriptions), use `transformer` instead.
358
+ - name: group
359
+ type: Group
360
+ required: false
361
+ description: |
362
+ Splits generated files into subfolders based on the operation's tag, so each tag in your OpenAPI spec gets its own directory.
363
+
364
+ 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.
365
+ tip: |
366
+ 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.
367
+ examples:
368
+ - name: kubb.config.ts
369
+ files:
370
+ - lang: typescript
371
+ twoslash: false
372
+ code: |
373
+ import { defineConfig } from 'kubb'
374
+ import { pluginTs } from '@kubb/plugin-ts'
375
+
376
+ export default defineConfig({
377
+ input: { path: './petStore.yaml' },
378
+ output: { path: './src/gen' },
379
+ plugins: [
380
+ pluginTs({
381
+ group: {
382
+ type: 'tag',
383
+ name: ({ group }) => `${group}Controller`,
384
+ },
385
+ }),
386
+ ],
387
+ })
388
+ body: |
389
+ With the configuration above, the generator emits:
390
+
391
+ ```text
392
+ src/gen/
393
+ ├── petController/
394
+ │ ├── AddPet.ts
395
+ │ └── GetPet.ts
396
+ └── storeController/
397
+ ├── CreateStore.ts
398
+ └── GetStoreById.ts
399
+ ```
400
+ properties:
401
+ - name: type
402
+ type: "'tag'"
403
+ required: true
404
+ description: |
405
+ Property used to assign each operation to a group. Required whenever `group` is set.
406
+
407
+ 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.
408
+ note: |
409
+ `Required: true*` is conditional — only required when the parent `group` option is used. `group` itself stays optional.
410
+ - name: name
411
+ type: '(context: GroupContext) => string'
412
+ required: false
413
+ default: (ctx) => `${ctx.group}Controller`
414
+ description: Function that builds the folder/identifier name from a group key (the operation's first tag).
415
+ - name: importPath
416
+ type: string
417
+ required: false
418
+ default: "'zod'"
419
+ description: |
420
+ Module specifier used in the `import { z } from '...'` statement at the top of generated files.
421
+
422
+ Use `'zod/mini'` to import from the tree-shakeable Mini bundle, or a custom path when re-exporting Zod from your own module.
423
+ examples:
424
+ - name: Use Zod's mini bundle
425
+ files:
426
+ - lang: typescript
427
+ twoslash: false
428
+ code: |
429
+ import { defineConfig } from 'kubb'
430
+ import { pluginZod } from '@kubb/plugin-zod'
431
+
432
+ export default defineConfig({
433
+ input: { path: './petStore.yaml' },
434
+ output: { path: './src/gen' },
435
+ plugins: [
436
+ pluginZod({ importPath: 'zod/mini' }),
437
+ ],
438
+ })
439
+ - name: typed
440
+ type: boolean
441
+ required: false
442
+ default: 'false'
443
+ description: |
444
+ Adds a type annotation that ties each Zod schema to its TypeScript counterpart from `@kubb/plugin-ts`.
445
+
446
+ 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.
447
+ important: |
448
+ The mapping uses a [ToZod-style](https://github.com/colinhacks/tozod) helper (vendored in Kubb) to derive a Zod shape from a TypeScript type.
449
+ examples:
450
+ - name: Schema linked to its TS type
451
+ files:
452
+ - lang: typescript
453
+ twoslash: false
454
+ code: |
455
+ import { z } from 'zod'
456
+ import type { ToZod } from '@kubb/plugin-zod'
457
+ import type { Pet } from '../ts/Pet'
458
+
459
+ export const petSchema: ToZod<Pet> = z.object({
460
+ name: z.string(),
461
+ status: z.enum(['available', 'pending', 'sold']).optional(),
462
+ })
463
+ - name: inferred
464
+ type: boolean
465
+ required: false
466
+ default: 'false'
467
+ description: |
468
+ Exports a `z.infer<typeof schema>` type alias next to every generated schema.
469
+
470
+ 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`.
471
+ codeBlock:
472
+ lang: typescript
473
+ title: With inferred enabled
474
+ code: |
475
+ import { z } from 'zod'
476
+
477
+ export const petSchema = z.object({
478
+ name: z.string(),
479
+ status: z.enum(['available', 'pending', 'sold']).optional(),
480
+ })
481
+
482
+ export type Pet = z.infer<typeof petSchema>
483
+ - name: integerType
484
+ warning: |
485
+ Moved to [`adapterOas`](/adapters/adapter-oas#integerType). Use `adapterOas({ integerType })` instead.
486
+ - name: unknownType
487
+ warning: |
488
+ Moved to [`adapterOas`](/adapters/adapter-oas#unknownType). Use `adapterOas({ unknownType })` instead.
489
+ - name: emptySchemaType
490
+ warning: |
491
+ Moved to [`adapterOas`](/adapters/adapter-oas#emptySchemaType). Use `adapterOas({ emptySchemaType })` instead.
492
+ - name: coercion
493
+ type: 'boolean | { dates?: boolean, strings?: boolean, numbers?: boolean }'
494
+ required: false
495
+ default: 'false'
496
+ description: |
497
+ 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.
498
+
499
+ - `true` — coerce strings, numbers, and dates.
500
+ - `false` (default) — no coercion. Strict validation.
501
+ - Object — pick which primitives to coerce.
502
+
503
+ See [Coercion for primitives](https://zod.dev/?id=coercion-for-primitives).
504
+ examples:
505
+ - name: '`coercion: true`'
506
+ files:
507
+ - lang: typescript
508
+ twoslash: false
509
+ code: |
510
+ z.coerce.string()
511
+ z.coerce.date()
512
+ z.coerce.number()
513
+ - name: '`coercion: false` (default)'
514
+ files:
515
+ - lang: typescript
516
+ twoslash: false
517
+ code: |
518
+ z.string()
519
+ z.date()
520
+ z.number()
521
+ - name: Coerce numbers only
522
+ files:
523
+ - lang: typescript
524
+ twoslash: false
525
+ code: |
526
+ // { numbers: true, strings: false, dates: false }
527
+ z.string()
528
+ z.date()
529
+ z.coerce.number()
530
+ - name: operations
531
+ type: boolean
532
+ required: false
533
+ default: 'false'
534
+ description: |
535
+ Emits an `operations.ts` file that groups schemas per operation: request body, query params, path params, and each response status.
536
+
537
+ 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.
538
+ - name: paramsCasing
539
+ type: "'camelcase'"
540
+ required: false
541
+ description: |
542
+ Renames properties inside the path/query/header schemas to the chosen casing. Body schemas are unaffected.
543
+
544
+ Must match the value of `paramsCasing` on `@kubb/plugin-ts` so the generated Zod schemas stay assignable to the generated types.
545
+ codeBlock:
546
+ lang: typescript
547
+ title: "`paramsCasing: 'camelcase'`"
548
+ code: |
549
+ // OpenAPI spec uses: pet_id, X-Api-Key
550
+ export const getPetPathParamsSchema = z.object({
551
+ petId: z.string(),
552
+ })
553
+
554
+ export const getPetHeaderParamsSchema = z.object({
555
+ xApiKey: z.string().optional(),
556
+ })
557
+ - name: guidType
558
+ type: "'uuid' | 'guid'"
559
+ required: false
560
+ default: "'uuid'"
561
+ description: |
562
+ Validator used for OpenAPI properties with `format: uuid`.
563
+
564
+ - `'uuid'` (default) — `z.uuid()`. Standard RFC 4122 UUID.
565
+ - `'guid'` — `z.guid()`. Looser; accepts Microsoft-style GUIDs (allows lowercase, mixed brace styles).
566
+ examples:
567
+ - name: "'uuid' (default)"
568
+ files:
569
+ - lang: typescript
570
+ twoslash: false
571
+ code: |
572
+ z.uuid()
573
+ - name: "'guid'"
574
+ files:
575
+ - lang: typescript
576
+ twoslash: false
577
+ code: |
578
+ z.guid()
579
+ - name: mini
580
+ type: boolean
581
+ required: false
582
+ default: 'false'
583
+ badge:
584
+ type: tip
585
+ text: beta
586
+ description: |
587
+ 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.
588
+
589
+ Setting `mini: true` also defaults `importPath` to `'zod/mini'`.
590
+ warning: |
591
+ Zod Mini is currently in beta. Its API may change in a future release.
592
+ tip: |
593
+ 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.
594
+ examples:
595
+ - name: '`mini: true`'
596
+ files:
597
+ - lang: typescript
598
+ twoslash: false
599
+ code: |
600
+ import { z } from 'zod/mini'
601
+
602
+ z.optional(z.string())
603
+ z.nullable(z.number())
604
+ z.array(z.string()).check(z.minLength(1), z.maxLength(10))
605
+ - name: '`mini: false` (default)'
606
+ files:
607
+ - lang: typescript
608
+ twoslash: false
609
+ code: |
610
+ import { z } from 'zod'
611
+
612
+ z.string().optional()
613
+ z.number().nullable()
614
+ z.array(z.string()).min(1).max(10)
615
+ - name: include
616
+ type: Array<Include>
617
+ required: false
618
+ description: |
619
+ Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
620
+
621
+ Each entry filters by one of:
622
+
623
+ - `tag` — the operation's first tag in the OpenAPI spec.
624
+ - `operationId` — the operation's `operationId`.
625
+ - `path` — the URL pattern (`'/pet/{petId}'`).
626
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
627
+ - `contentType` — the media type of the request body.
628
+
629
+ `pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
630
+ codeBlock:
631
+ lang: typescript
632
+ title: Type definition
633
+ code: |
634
+ export type Include = {
635
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
636
+ pattern: string | RegExp
637
+ }
638
+ examples:
639
+ - name: Only the pet tag
640
+ files:
641
+ - name: kubb.config.ts
642
+ lang: typescript
643
+ twoslash: false
644
+ code: |
645
+ import { defineConfig } from 'kubb'
646
+ import { pluginTs } from '@kubb/plugin-ts'
647
+
648
+ export default defineConfig({
649
+ input: { path: './petStore.yaml' },
650
+ output: { path: './src/gen' },
651
+ plugins: [
652
+ pluginTs({
653
+ include: [
654
+ { type: 'tag', pattern: 'pet' },
655
+ ],
656
+ }),
657
+ ],
658
+ })
659
+ - name: Only GET operations under /pet
660
+ files:
661
+ - name: kubb.config.ts
662
+ lang: typescript
663
+ twoslash: false
664
+ code: |
665
+ import { defineConfig } from 'kubb'
666
+ import { pluginTs } from '@kubb/plugin-ts'
667
+
668
+ export default defineConfig({
669
+ input: { path: './petStore.yaml' },
670
+ output: { path: './src/gen' },
671
+ plugins: [
672
+ pluginTs({
673
+ include: [
674
+ { type: 'method', pattern: 'get' },
675
+ { type: 'path', pattern: /^\/pet/ },
676
+ ],
677
+ }),
678
+ ],
679
+ })
680
+ - name: exclude
681
+ type: Array<Exclude>
682
+ required: false
683
+ description: |
684
+ Skips any operation that matches at least one entry in the list. The opposite of `include`.
685
+
686
+ Each entry filters by one of:
687
+
688
+ - `tag` — the operation's first tag.
689
+ - `operationId` — the operation's `operationId`.
690
+ - `path` — the URL pattern (`'/pet/{petId}'`).
691
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
692
+ - `contentType` — the media type of the request body.
693
+
694
+ `pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
695
+ codeBlock:
696
+ lang: typescript
697
+ title: Type definition
698
+ code: |
699
+ export type Exclude = {
700
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
701
+ pattern: string | RegExp
702
+ }
703
+ examples:
704
+ - name: Skip everything under the store tag
705
+ files:
706
+ - name: kubb.config.ts
707
+ lang: typescript
708
+ twoslash: false
709
+ code: |
710
+ import { defineConfig } from 'kubb'
711
+ import { pluginTs } from '@kubb/plugin-ts'
712
+
713
+ export default defineConfig({
714
+ input: { path: './petStore.yaml' },
715
+ output: { path: './src/gen' },
716
+ plugins: [
717
+ pluginTs({
718
+ exclude: [
719
+ { type: 'tag', pattern: 'store' },
720
+ ],
721
+ }),
722
+ ],
723
+ })
724
+ - name: Skip a specific operation and all delete methods
725
+ files:
726
+ - name: kubb.config.ts
727
+ lang: typescript
728
+ twoslash: false
729
+ code: |
730
+ import { defineConfig } from 'kubb'
731
+ import { pluginTs } from '@kubb/plugin-ts'
732
+
733
+ export default defineConfig({
734
+ input: { path: './petStore.yaml' },
735
+ output: { path: './src/gen' },
736
+ plugins: [
737
+ pluginTs({
738
+ exclude: [
739
+ { type: 'operationId', pattern: 'deletePet' },
740
+ { type: 'method', pattern: 'delete' },
741
+ ],
742
+ }),
743
+ ],
744
+ })
745
+ - name: override
746
+ type: Array<Override>
747
+ required: false
748
+ description: |
749
+ 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.
750
+
751
+ 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.
752
+
753
+ Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
754
+ codeBlock:
755
+ lang: typescript
756
+ title: Type definition
757
+ code: |
758
+ export type Override = {
759
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
760
+ pattern: string | RegExp
761
+ options: PluginOptions
762
+ }
763
+ examples:
764
+ - name: Use a different enum style for the user tag
765
+ files:
766
+ - name: kubb.config.ts
767
+ lang: typescript
768
+ twoslash: false
769
+ code: |
770
+ import { defineConfig } from 'kubb'
771
+ import { pluginTs } from '@kubb/plugin-ts'
772
+
773
+ export default defineConfig({
774
+ input: { path: './petStore.yaml' },
775
+ output: { path: './src/gen' },
776
+ plugins: [
777
+ pluginTs({
778
+ enumType: 'asConst',
779
+ override: [
780
+ {
781
+ type: 'tag',
782
+ pattern: 'user',
783
+ options: { enumType: 'literal' },
784
+ },
785
+ ],
786
+ }),
787
+ ],
788
+ })
789
+ - name: generators
790
+ type: Array<Generator<PluginZod>>
791
+ required: false
792
+ experimental: true
793
+ description: |
794
+ 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.
795
+
796
+ 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).
797
+ warning: |
798
+ Generators are an experimental, low-level API. The signature may change between minor releases.
799
+ - name: transformer
800
+ type: Visitor
801
+ required: false
802
+ description: |
803
+ 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.
804
+
805
+ 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.
806
+ examples:
807
+ - name: Strip descriptions before printing
808
+ files:
809
+ - name: kubb.config.ts
810
+ lang: typescript
811
+ twoslash: false
812
+ code: |
813
+ import { defineConfig } from 'kubb'
814
+ import { pluginTs } from '@kubb/plugin-ts'
815
+
816
+ export default defineConfig({
817
+ input: { path: './petStore.yaml' },
818
+ output: { path: './src/gen' },
819
+ plugins: [
820
+ pluginTs({
821
+ transformer: {
822
+ schema(node) {
823
+ return { ...node, description: undefined }
824
+ },
825
+ },
826
+ }),
827
+ ],
828
+ })
829
+ - name: Prefix every operationId
830
+ files:
831
+ - name: kubb.config.ts
832
+ lang: typescript
833
+ twoslash: false
834
+ code: |
835
+ import { defineConfig } from 'kubb'
836
+ import { pluginTs } from '@kubb/plugin-ts'
837
+
838
+ export default defineConfig({
839
+ input: { path: './petStore.yaml' },
840
+ output: { path: './src/gen' },
841
+ plugins: [
842
+ pluginTs({
843
+ transformer: {
844
+ operation(node) {
845
+ return { ...node, operationId: `api_${node.operationId}` }
846
+ },
847
+ },
848
+ }),
849
+ ],
850
+ })
851
+ tip: |
852
+ Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
853
+ - name: printer
854
+ type: '{ nodes?: PrinterZodNodes | PrinterZodMiniNodes }'
855
+ required: false
856
+ description: |
857
+ Replaces the Zod handler for a specific schema type (e.g. `'integer'`, `'date'`, `'string'`). Each handler returns the Zod expression as a string.
858
+
859
+ When `mini: true`, overrides target the Zod Mini printer; otherwise they target the standard Zod printer.
860
+ examples:
861
+ - name: Use z.number() for integers
862
+ files:
863
+ - lang: typescript
864
+ twoslash: false
865
+ code: |
866
+ import { defineConfig } from 'kubb'
867
+ import { pluginZod } from '@kubb/plugin-zod'
868
+
869
+ export default defineConfig({
870
+ input: { path: './petStore.yaml' },
871
+ output: { path: './src/gen' },
872
+ plugins: [
873
+ pluginZod({
874
+ printer: {
875
+ nodes: {
876
+ integer() {
877
+ return 'z.number()'
878
+ },
879
+ },
880
+ },
881
+ }),
882
+ ],
883
+ })
884
+ - name: Use z.string().date() for date schemas
885
+ files:
886
+ - lang: typescript
887
+ twoslash: false
888
+ code: |
889
+ import { defineConfig } from 'kubb'
890
+ import { pluginZod } from '@kubb/plugin-zod'
891
+
892
+ export default defineConfig({
893
+ input: { path: './petStore.yaml' },
894
+ output: { path: './src/gen' },
895
+ plugins: [
896
+ pluginZod({
897
+ printer: {
898
+ nodes: {
899
+ date() {
900
+ return 'z.string().date()'
901
+ },
902
+ },
903
+ },
904
+ }),
905
+ ],
906
+ })
907
+ - name: wrapOutput
908
+ type: '(arg: { output: string; schema: SchemaNode }) => string | undefined'
909
+ required: false
910
+ description: |
911
+ 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`.
912
+
913
+ Return a new string to replace the output, or return `undefined` to leave it untouched.
914
+ tip: |
915
+ 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.
916
+ codeBlock:
917
+ lang: typescript
918
+ title: Append .openapi() with metadata
919
+ code: |
920
+ import { defineConfig } from 'kubb'
921
+ import { pluginZod } from '@kubb/plugin-zod'
922
+
923
+ export default defineConfig({
924
+ input: { path: './petStore.yaml' },
925
+ output: { path: './src/gen' },
926
+ plugins: [
927
+ pluginZod({
928
+ wrapOutput: ({ output, schema }) => {
929
+ const metadata: Record<string, unknown> = {}
930
+
931
+ if (schema.keywords?.includes('example')) {
932
+ // Pull keyword metadata off the SchemaNode here
933
+ }
934
+
935
+ if (Object.keys(metadata).length > 0) {
936
+ return `${output}.openapi(${JSON.stringify(metadata)})`
937
+ }
938
+
939
+ return undefined
940
+ },
941
+ }),
942
+ ],
943
+ })
944
+ examples:
945
+ - name: kubb.config.ts
946
+ files:
947
+ - lang: typescript
948
+ twoslash: false
949
+ code: |
950
+ import { defineConfig } from 'kubb'
951
+ import { pluginTs } from '@kubb/plugin-ts'
952
+ import { pluginZod } from '@kubb/plugin-zod'
953
+
954
+ export default defineConfig({
955
+ input: { path: './petStore.yaml' },
956
+ output: { path: './src/gen' },
957
+ plugins: [
958
+ pluginTs(),
959
+ pluginZod({
960
+ output: { path: './zod' },
961
+ group: { type: 'tag', name: ({ group }) => `${group}Schemas` },
962
+ typed: true,
963
+ importPath: 'zod',
964
+ }),
965
+ ],
966
+ })