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

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,968 @@
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
+ tip: |
505
+ 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.
506
+ examples:
507
+ - name: '`coercion: true`'
508
+ files:
509
+ - lang: typescript
510
+ twoslash: false
511
+ code: |
512
+ z.coerce.string()
513
+ z.coerce.date()
514
+ z.coerce.number()
515
+ - name: '`coercion: false` (default)'
516
+ files:
517
+ - lang: typescript
518
+ twoslash: false
519
+ code: |
520
+ z.string()
521
+ z.date()
522
+ z.number()
523
+ - name: Coerce numbers only
524
+ files:
525
+ - lang: typescript
526
+ twoslash: false
527
+ code: |
528
+ // { numbers: true, strings: false, dates: false }
529
+ z.string()
530
+ z.date()
531
+ z.coerce.number()
532
+ - name: operations
533
+ type: boolean
534
+ required: false
535
+ default: 'false'
536
+ description: |
537
+ Emits an `operations.ts` file that groups schemas per operation: request body, query params, path params, and each response status.
538
+
539
+ 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.
540
+ - name: paramsCasing
541
+ type: "'camelcase'"
542
+ required: false
543
+ description: |
544
+ Renames properties inside the path/query/header schemas to the chosen casing. Body schemas are unaffected.
545
+
546
+ Must match the value of `paramsCasing` on `@kubb/plugin-ts` so the generated Zod schemas stay assignable to the generated types.
547
+ codeBlock:
548
+ lang: typescript
549
+ title: "`paramsCasing: 'camelcase'`"
550
+ code: |
551
+ // OpenAPI spec uses: pet_id, X-Api-Key
552
+ export const getPetPathParamsSchema = z.object({
553
+ petId: z.string(),
554
+ })
555
+
556
+ export const getPetHeaderParamsSchema = z.object({
557
+ xApiKey: z.string().optional(),
558
+ })
559
+ - name: guidType
560
+ type: "'uuid' | 'guid'"
561
+ required: false
562
+ default: "'uuid'"
563
+ description: |
564
+ Validator used for OpenAPI properties with `format: uuid`.
565
+
566
+ - `'uuid'` (default) — `z.uuid()`. Standard RFC 4122 UUID.
567
+ - `'guid'` — `z.guid()`. Looser; accepts Microsoft-style GUIDs (allows lowercase, mixed brace styles).
568
+ examples:
569
+ - name: "'uuid' (default)"
570
+ files:
571
+ - lang: typescript
572
+ twoslash: false
573
+ code: |
574
+ z.uuid()
575
+ - name: "'guid'"
576
+ files:
577
+ - lang: typescript
578
+ twoslash: false
579
+ code: |
580
+ z.guid()
581
+ - name: mini
582
+ type: boolean
583
+ required: false
584
+ default: 'false'
585
+ badge:
586
+ type: tip
587
+ text: beta
588
+ description: |
589
+ 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.
590
+
591
+ Setting `mini: true` also defaults `importPath` to `'zod/mini'`.
592
+ warning: |
593
+ Zod Mini is currently in beta. Its API may change in a future release.
594
+ tip: |
595
+ 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.
596
+ examples:
597
+ - name: '`mini: true`'
598
+ files:
599
+ - lang: typescript
600
+ twoslash: false
601
+ code: |
602
+ import { z } from 'zod/mini'
603
+
604
+ z.optional(z.string())
605
+ z.nullable(z.number())
606
+ z.array(z.string()).check(z.minLength(1), z.maxLength(10))
607
+ - name: '`mini: false` (default)'
608
+ files:
609
+ - lang: typescript
610
+ twoslash: false
611
+ code: |
612
+ import { z } from 'zod'
613
+
614
+ z.string().optional()
615
+ z.number().nullable()
616
+ z.array(z.string()).min(1).max(10)
617
+ - name: include
618
+ type: Array<Include>
619
+ required: false
620
+ description: |
621
+ Restricts generation to operations that match at least one entry in the list. Anything not matched is skipped.
622
+
623
+ Each entry filters by one of:
624
+
625
+ - `tag` — the operation's first tag in the OpenAPI spec.
626
+ - `operationId` — the operation's `operationId`.
627
+ - `path` — the URL pattern (`'/pet/{petId}'`).
628
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
629
+ - `contentType` — the media type of the request body.
630
+
631
+ `pattern` accepts either a string (exact match) or a `RegExp` for fuzzy matches.
632
+ codeBlock:
633
+ lang: typescript
634
+ title: Type definition
635
+ code: |
636
+ export type Include = {
637
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
638
+ pattern: string | RegExp
639
+ }
640
+ examples:
641
+ - name: Only the pet tag
642
+ files:
643
+ - name: kubb.config.ts
644
+ lang: typescript
645
+ twoslash: false
646
+ code: |
647
+ import { defineConfig } from 'kubb'
648
+ import { pluginTs } from '@kubb/plugin-ts'
649
+
650
+ export default defineConfig({
651
+ input: { path: './petStore.yaml' },
652
+ output: { path: './src/gen' },
653
+ plugins: [
654
+ pluginTs({
655
+ include: [
656
+ { type: 'tag', pattern: 'pet' },
657
+ ],
658
+ }),
659
+ ],
660
+ })
661
+ - name: Only GET operations under /pet
662
+ files:
663
+ - name: kubb.config.ts
664
+ lang: typescript
665
+ twoslash: false
666
+ code: |
667
+ import { defineConfig } from 'kubb'
668
+ import { pluginTs } from '@kubb/plugin-ts'
669
+
670
+ export default defineConfig({
671
+ input: { path: './petStore.yaml' },
672
+ output: { path: './src/gen' },
673
+ plugins: [
674
+ pluginTs({
675
+ include: [
676
+ { type: 'method', pattern: 'get' },
677
+ { type: 'path', pattern: /^\/pet/ },
678
+ ],
679
+ }),
680
+ ],
681
+ })
682
+ - name: exclude
683
+ type: Array<Exclude>
684
+ required: false
685
+ description: |
686
+ Skips any operation that matches at least one entry in the list. The opposite of `include`.
687
+
688
+ Each entry filters by one of:
689
+
690
+ - `tag` — the operation's first tag.
691
+ - `operationId` — the operation's `operationId`.
692
+ - `path` — the URL pattern (`'/pet/{petId}'`).
693
+ - `method` — HTTP method (`'get'`, `'post'`, ...).
694
+ - `contentType` — the media type of the request body.
695
+
696
+ `pattern` accepts a plain string or a `RegExp`. When both `include` and `exclude` are set, `exclude` wins.
697
+ codeBlock:
698
+ lang: typescript
699
+ title: Type definition
700
+ code: |
701
+ export type Exclude = {
702
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
703
+ pattern: string | RegExp
704
+ }
705
+ examples:
706
+ - name: Skip everything under the store tag
707
+ files:
708
+ - name: kubb.config.ts
709
+ lang: typescript
710
+ twoslash: false
711
+ code: |
712
+ import { defineConfig } from 'kubb'
713
+ import { pluginTs } from '@kubb/plugin-ts'
714
+
715
+ export default defineConfig({
716
+ input: { path: './petStore.yaml' },
717
+ output: { path: './src/gen' },
718
+ plugins: [
719
+ pluginTs({
720
+ exclude: [
721
+ { type: 'tag', pattern: 'store' },
722
+ ],
723
+ }),
724
+ ],
725
+ })
726
+ - name: Skip a specific operation and all delete methods
727
+ files:
728
+ - name: kubb.config.ts
729
+ lang: typescript
730
+ twoslash: false
731
+ code: |
732
+ import { defineConfig } from 'kubb'
733
+ import { pluginTs } from '@kubb/plugin-ts'
734
+
735
+ export default defineConfig({
736
+ input: { path: './petStore.yaml' },
737
+ output: { path: './src/gen' },
738
+ plugins: [
739
+ pluginTs({
740
+ exclude: [
741
+ { type: 'operationId', pattern: 'deletePet' },
742
+ { type: 'method', pattern: 'delete' },
743
+ ],
744
+ }),
745
+ ],
746
+ })
747
+ - name: override
748
+ type: Array<Override>
749
+ required: false
750
+ description: |
751
+ 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.
752
+
753
+ 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.
754
+
755
+ Entries are evaluated top to bottom. The first matching entry's `options` is merged onto the plugin defaults; later entries do not stack.
756
+ codeBlock:
757
+ lang: typescript
758
+ title: Type definition
759
+ code: |
760
+ export type Override = {
761
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
762
+ pattern: string | RegExp
763
+ options: PluginOptions
764
+ }
765
+ examples:
766
+ - name: Use a different enum style for the user tag
767
+ files:
768
+ - name: kubb.config.ts
769
+ lang: typescript
770
+ twoslash: false
771
+ code: |
772
+ import { defineConfig } from 'kubb'
773
+ import { pluginTs } from '@kubb/plugin-ts'
774
+
775
+ export default defineConfig({
776
+ input: { path: './petStore.yaml' },
777
+ output: { path: './src/gen' },
778
+ plugins: [
779
+ pluginTs({
780
+ enumType: 'asConst',
781
+ override: [
782
+ {
783
+ type: 'tag',
784
+ pattern: 'user',
785
+ options: { enumType: 'literal' },
786
+ },
787
+ ],
788
+ }),
789
+ ],
790
+ })
791
+ - name: generators
792
+ type: Array<Generator<PluginZod>>
793
+ required: false
794
+ experimental: true
795
+ description: |
796
+ 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.
797
+
798
+ 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).
799
+ warning: |
800
+ Generators are an experimental, low-level API. The signature may change between minor releases.
801
+ - name: transformer
802
+ type: Visitor
803
+ required: false
804
+ description: |
805
+ 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.
806
+
807
+ 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.
808
+ examples:
809
+ - name: Strip descriptions before printing
810
+ files:
811
+ - name: kubb.config.ts
812
+ lang: typescript
813
+ twoslash: false
814
+ code: |
815
+ import { defineConfig } from 'kubb'
816
+ import { pluginTs } from '@kubb/plugin-ts'
817
+
818
+ export default defineConfig({
819
+ input: { path: './petStore.yaml' },
820
+ output: { path: './src/gen' },
821
+ plugins: [
822
+ pluginTs({
823
+ transformer: {
824
+ schema(node) {
825
+ return { ...node, description: undefined }
826
+ },
827
+ },
828
+ }),
829
+ ],
830
+ })
831
+ - name: Prefix every operationId
832
+ files:
833
+ - name: kubb.config.ts
834
+ lang: typescript
835
+ twoslash: false
836
+ code: |
837
+ import { defineConfig } from 'kubb'
838
+ import { pluginTs } from '@kubb/plugin-ts'
839
+
840
+ export default defineConfig({
841
+ input: { path: './petStore.yaml' },
842
+ output: { path: './src/gen' },
843
+ plugins: [
844
+ pluginTs({
845
+ transformer: {
846
+ operation(node) {
847
+ return { ...node, operationId: `api_${node.operationId}` }
848
+ },
849
+ },
850
+ }),
851
+ ],
852
+ })
853
+ tip: |
854
+ Use `transformer` to rewrite node properties before printing. For changing the names of generated symbols and files, use `resolver` instead.
855
+ - name: printer
856
+ type: '{ nodes?: PrinterZodNodes | PrinterZodMiniNodes }'
857
+ required: false
858
+ description: |
859
+ Replaces the Zod handler for a specific schema type (e.g. `'integer'`, `'date'`, `'string'`). Each handler returns the Zod expression as a string.
860
+
861
+ When `mini: true`, overrides target the Zod Mini printer; otherwise they target the standard Zod printer.
862
+ examples:
863
+ - name: Use z.number() for integers
864
+ files:
865
+ - lang: typescript
866
+ twoslash: false
867
+ code: |
868
+ import { defineConfig } from 'kubb'
869
+ import { pluginZod } from '@kubb/plugin-zod'
870
+
871
+ export default defineConfig({
872
+ input: { path: './petStore.yaml' },
873
+ output: { path: './src/gen' },
874
+ plugins: [
875
+ pluginZod({
876
+ printer: {
877
+ nodes: {
878
+ integer() {
879
+ return 'z.number()'
880
+ },
881
+ },
882
+ },
883
+ }),
884
+ ],
885
+ })
886
+ - name: Use z.string().date() for date schemas
887
+ files:
888
+ - lang: typescript
889
+ twoslash: false
890
+ code: |
891
+ import { defineConfig } from 'kubb'
892
+ import { pluginZod } from '@kubb/plugin-zod'
893
+
894
+ export default defineConfig({
895
+ input: { path: './petStore.yaml' },
896
+ output: { path: './src/gen' },
897
+ plugins: [
898
+ pluginZod({
899
+ printer: {
900
+ nodes: {
901
+ date() {
902
+ return 'z.string().date()'
903
+ },
904
+ },
905
+ },
906
+ }),
907
+ ],
908
+ })
909
+ - name: wrapOutput
910
+ type: '(arg: { output: string; schema: SchemaNode }) => string | undefined'
911
+ required: false
912
+ description: |
913
+ 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`.
914
+
915
+ Return a new string to replace the output, or return `undefined` to leave it untouched.
916
+ tip: |
917
+ 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.
918
+ codeBlock:
919
+ lang: typescript
920
+ title: Append .openapi() with metadata
921
+ code: |
922
+ import { defineConfig } from 'kubb'
923
+ import { pluginZod } from '@kubb/plugin-zod'
924
+
925
+ export default defineConfig({
926
+ input: { path: './petStore.yaml' },
927
+ output: { path: './src/gen' },
928
+ plugins: [
929
+ pluginZod({
930
+ wrapOutput: ({ output, schema }) => {
931
+ const metadata: Record<string, unknown> = {}
932
+
933
+ if (schema.keywords?.includes('example')) {
934
+ // Pull keyword metadata off the SchemaNode here
935
+ }
936
+
937
+ if (Object.keys(metadata).length > 0) {
938
+ return `${output}.openapi(${JSON.stringify(metadata)})`
939
+ }
940
+
941
+ return undefined
942
+ },
943
+ }),
944
+ ],
945
+ })
946
+ examples:
947
+ - name: kubb.config.ts
948
+ files:
949
+ - lang: typescript
950
+ twoslash: false
951
+ code: |
952
+ import { defineConfig } from 'kubb'
953
+ import { pluginTs } from '@kubb/plugin-ts'
954
+ import { pluginZod } from '@kubb/plugin-zod'
955
+
956
+ export default defineConfig({
957
+ input: { path: './petStore.yaml' },
958
+ output: { path: './src/gen' },
959
+ plugins: [
960
+ pluginTs(),
961
+ pluginZod({
962
+ output: { path: './zod' },
963
+ group: { type: 'tag', name: ({ group }) => `${group}Schemas` },
964
+ typed: true,
965
+ importPath: 'zod',
966
+ }),
967
+ ],
968
+ })