@kubb/plugin-zod 5.0.0-alpha.9 → 5.0.0-beta.100

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.
Files changed (41) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +38 -23
  3. package/dist/index.cjs +1964 -133
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +482 -6
  6. package/dist/index.js +1950 -132
  7. package/dist/index.js.map +1 -1
  8. package/package.json +36 -81
  9. package/dist/components-B7zUFnAm.cjs +0 -890
  10. package/dist/components-B7zUFnAm.cjs.map +0 -1
  11. package/dist/components-eECfXVou.js +0 -842
  12. package/dist/components-eECfXVou.js.map +0 -1
  13. package/dist/components.cjs +0 -4
  14. package/dist/components.d.ts +0 -56
  15. package/dist/components.js +0 -2
  16. package/dist/generators-BjPDdJUz.cjs +0 -301
  17. package/dist/generators-BjPDdJUz.cjs.map +0 -1
  18. package/dist/generators-lTWPS6oN.js +0 -290
  19. package/dist/generators-lTWPS6oN.js.map +0 -1
  20. package/dist/generators.cjs +0 -4
  21. package/dist/generators.d.ts +0 -508
  22. package/dist/generators.js +0 -2
  23. package/dist/templates/ToZod.source.cjs +0 -7
  24. package/dist/templates/ToZod.source.cjs.map +0 -1
  25. package/dist/templates/ToZod.source.d.ts +0 -7
  26. package/dist/templates/ToZod.source.js +0 -6
  27. package/dist/templates/ToZod.source.js.map +0 -1
  28. package/dist/types-CoCoOc2u.d.ts +0 -172
  29. package/src/components/Operations.tsx +0 -70
  30. package/src/components/Zod.tsx +0 -142
  31. package/src/components/index.ts +0 -2
  32. package/src/generators/index.ts +0 -2
  33. package/src/generators/operationsGenerator.tsx +0 -50
  34. package/src/generators/zodGenerator.tsx +0 -202
  35. package/src/index.ts +0 -2
  36. package/src/parser.ts +0 -909
  37. package/src/plugin.ts +0 -183
  38. package/src/templates/ToZod.source.ts +0 -4
  39. package/src/types.ts +0 -172
  40. package/templates/ToZod.ts +0 -61
  41. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/dist/index.d.ts CHANGED
@@ -1,10 +1,486 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { n as PluginZod, t as Options } from "./types-CoCoOc2u.js";
3
- import * as _kubb_core0 from "@kubb/core";
4
-
1
+ import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
2
+ import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ResolverPatch, ast } from "kubb/kit";
3
+ import { AdapterOas } from "@kubb/adapter-oas";
4
+ //#region src/printers/printerZod.d.ts
5
+ /**
6
+ * Partial map of node-type overrides for the Zod printer.
7
+ *
8
+ * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`). The function
9
+ * replaces the built-in handler for that node type. Use `this.transform` to
10
+ * recurse into nested schema nodes, `this.base` to reuse the output of the
11
+ * handler being replaced, and `this.options` to read printer options.
12
+ *
13
+ * @example Override the `date` handler
14
+ * ```ts
15
+ * pluginZod({
16
+ * printer: {
17
+ * nodes: {
18
+ * date(node) {
19
+ * return 'z.iso.date()'
20
+ * },
21
+ * },
22
+ * },
23
+ * })
24
+ * ```
25
+ *
26
+ * @example Wrap the built-in output
27
+ * ```ts
28
+ * pluginZod({
29
+ * printer: {
30
+ * nodes: {
31
+ * object(node) {
32
+ * return `${this.base(node)}.openapi(${JSON.stringify({ description: node.description })})`
33
+ * },
34
+ * },
35
+ * },
36
+ * })
37
+ * ```
38
+ */
39
+ type PrinterZodNodes = ast.PrinterPartial<string, PrinterZodOptions>;
40
+ type PrinterZodOptions = {
41
+ /**
42
+ * Enable automatic type coercion for strings, numbers, and dates.
43
+ */
44
+ coercion?: PluginZod['resolvedOptions']['coercion'];
45
+ /**
46
+ * Use `z.guid()` or `z.uuid()` for UUID/GUID validation.
47
+ *
48
+ * @default 'uuid'
49
+ */
50
+ guidType?: PluginZod['resolvedOptions']['guidType'];
51
+ /**
52
+ * Output form for an OpenAPI `pattern` inside `.regex(...)`: a regex literal
53
+ * (`'literal'`) or the `RegExp` constructor (`'constructor'`).
54
+ *
55
+ * @default 'literal'
56
+ */
57
+ regexType?: PluginZod['resolvedOptions']['regexType'];
58
+ /**
59
+ * Date format in the OpenAPI spec (`'date'` or `'date-time'`).
60
+ */
61
+ dateType?: AdapterOas['resolvedOptions']['dateType'];
62
+ /**
63
+ * Transforms raw schema names into valid JavaScript identifiers.
64
+ */
65
+ resolver?: ResolverZod;
66
+ /**
67
+ * Properties to exclude using `.omit({ key: true })`.
68
+ */
69
+ keysToOmit?: Array<string> | null;
70
+ /**
71
+ * Schema names that form circular dependency chains.
72
+ * Properties referencing these emit lazy getters wrapping refs in `z.lazy(() => …)`.
73
+ */
74
+ cyclicSchemas?: ReadonlySet<string>;
75
+ /**
76
+ * Print direction for `dateType: 'date'` fields (`Date` in TypeScript):
77
+ * - `'output'` (default): decode the wire `string` into a `Date` (response bodies).
78
+ * - `'input'`: encode a `Date` back into the wire `string` (request bodies/params).
79
+ *
80
+ * Diverging the directions requires the generator to emit an `${name}InputSchema`
81
+ * variant for each date-bearing component.
82
+ */
83
+ direction?: 'input' | 'output';
84
+ /**
85
+ * Custom handler map for node type overrides.
86
+ */
87
+ nodes?: PrinterZodNodes;
88
+ };
89
+ /**
90
+ * Factory options for the Zod printer, defining input/output types and configuration.
91
+ */
92
+ type PrinterZodFactory = ast.PrinterFactoryOptions<'zod', PrinterZodOptions, string, string>;
93
+ /**
94
+ * Zod v4 printer built with `definePrinter`.
95
+ *
96
+ * Converts a `SchemaNode` AST into a Zod v4 code string using the chainable API
97
+ * (`.optional()`, `.nullable()`, `.omit()`, etc.). For improved tree-shaking, see {@link printerZodMini}.
98
+ *
99
+ * @example Chainable API
100
+ * ```ts
101
+ * const printer = printerZod({ coercion: false })
102
+ * const code = printer.print(stringNode) // "z.string()"
103
+ * ```
104
+ */
105
+ declare const printerZod: (options?: PrinterZodOptions | undefined) => ast.Printer<PrinterZodFactory>;
106
+ //#endregion
107
+ //#region src/printers/printerZodMini.d.ts
108
+ /**
109
+ * Partial map of node-type overrides for the Zod Mini printer.
110
+ *
111
+ * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`). The function
112
+ * replaces the built-in handler for that node type. Use `this.transform` to
113
+ * recurse into nested schema nodes, `this.base` to reuse the output of the
114
+ * handler being replaced, and `this.options` to read printer options.
115
+ *
116
+ * @example Override the `date` handler
117
+ * ```ts
118
+ * pluginZod({
119
+ * mini: true,
120
+ * printer: {
121
+ * nodes: {
122
+ * date(node) {
123
+ * return 'z.iso.date()'
124
+ * },
125
+ * },
126
+ * },
127
+ * })
128
+ * ```
129
+ */
130
+ type PrinterZodMiniNodes = ast.PrinterPartial<string, PrinterZodMiniOptions>;
131
+ type PrinterZodMiniOptions = {
132
+ /**
133
+ * Use `z.guid()` or `z.uuid()` for UUID/GUID validation.
134
+ *
135
+ * @default 'uuid'
136
+ */
137
+ guidType?: PluginZod['resolvedOptions']['guidType'];
138
+ /**
139
+ * Output form for an OpenAPI `pattern` inside `z.regex(...)`: a regex literal
140
+ * (`'literal'`) or the `RegExp` constructor (`'constructor'`).
141
+ *
142
+ * @default 'literal'
143
+ */
144
+ regexType?: PluginZod['resolvedOptions']['regexType'];
145
+ /**
146
+ * Transforms raw schema names into valid JavaScript identifiers.
147
+ */
148
+ resolver?: ResolverZod;
149
+ /**
150
+ * Properties to exclude using `.omit({ key: true })`.
151
+ */
152
+ keysToOmit?: Array<string> | null;
153
+ /**
154
+ * Schema names that form circular dependency chains.
155
+ * Properties referencing these emit lazy getters wrapping refs in `z.lazy(() => …)`.
156
+ */
157
+ cyclicSchemas?: ReadonlySet<string>;
158
+ /**
159
+ * Custom handler map for node type overrides.
160
+ */
161
+ nodes?: PrinterZodMiniNodes;
162
+ };
163
+ /**
164
+ * Factory options for the Zod Mini printer, defining input/output types and configuration.
165
+ */
166
+ type PrinterZodMiniFactory = ast.PrinterFactoryOptions<'zod-mini', PrinterZodMiniOptions, string, string>;
167
+ /**
168
+ * Zod v4 Mini printer built with `definePrinter`.
169
+ *
170
+ * Converts a `SchemaNode` AST into a Zod v4 code string using the functional API
171
+ * (`z.optional(z.string())`) for improved tree-shaking. See {@link printerZod} for the chainable API.
172
+ *
173
+ * @example Functional Mini API
174
+ * ```ts
175
+ * const printer = printerZodMini({})
176
+ * const code = printer.print(optionalStringNode) // "z.optional(z.string())"
177
+ * ```
178
+ */
179
+ declare const printerZodMini: (options?: PrinterZodMiniOptions | undefined) => ast.Printer<PrinterZodMiniFactory>;
180
+ //#endregion
181
+ //#region src/types.d.ts
182
+ /**
183
+ * Resolver for Zod that provides naming methods for schema types.
184
+ *
185
+ * The top-level `name` resolves a camelCase schema function name with a `Schema` suffix
186
+ * (`listPetsSchema`), and `file` casts generated file names the same way. Composite naming lives in
187
+ * the `schema`, `param`, and `response` namespaces.
188
+ */
189
+ type ResolverZod = Resolver & {
190
+ /**
191
+ * Names derived from a component schema: its inferred types and the request (input) direction
192
+ * variant, where a date-bearing component encodes `Date` back to a wire `string`.
193
+ */
194
+ schema: {
195
+ /**
196
+ * Resolves the schema type name (inferred type from schema).
197
+ *
198
+ * @example Schema type names
199
+ * `resolver.schema.typeName('pet') // → 'PetSchemaType'`
200
+ */
201
+ typeName(name: string): string;
202
+ /**
203
+ * Resolves the generated type name from the schema.
204
+ *
205
+ * @example Type names
206
+ * `resolver.schema.type('petSchema') // → 'PetSchemaType'`
207
+ */
208
+ type(name: string): string;
209
+ /**
210
+ * Resolves the schema function name for the request (input) direction of a
211
+ * date-bearing component, where `Date` is encoded back to a wire `string`.
212
+ *
213
+ * @example Input schema names
214
+ * `resolver.schema.inputName('order') // → 'orderInputSchema'`
215
+ */
216
+ inputName(name: string): string;
217
+ /**
218
+ * Resolves the inferred type name for the request (input) direction variant.
219
+ *
220
+ * @example Input schema type names
221
+ * `resolver.schema.inputTypeName('order') // → 'OrderInputSchemaType'`
222
+ */
223
+ inputTypeName(name: string): string;
224
+ };
225
+ /**
226
+ * Names for an operation's parameters: an individual parameter and the grouped path, query, and
227
+ * header schemas.
228
+ */
229
+ param: {
230
+ /**
231
+ * Resolves the schema name for an individual parameter.
232
+ *
233
+ * @example Individual parameter name
234
+ * `resolver.param.name(node, param) // → 'deletePetPathPetIdSchema'`
235
+ */
236
+ name(node: ast.OperationNode, param: ast.ParameterNode): string;
237
+ /**
238
+ * Resolves the name for an operation's grouped path parameters schema.
239
+ *
240
+ * @example Path parameters names
241
+ * `resolver.param.path(node, param) // → 'deletePetPathSchema'`
242
+ */
243
+ path(node: ast.OperationNode, param: ast.ParameterNode): string;
244
+ /**
245
+ * Resolves the name for an operation's grouped query parameters schema.
246
+ *
247
+ * @example Query parameters names
248
+ * `resolver.param.query(node, param) // → 'findPetsByStatusQuerySchema'`
249
+ */
250
+ query(node: ast.OperationNode, param: ast.ParameterNode): string;
251
+ /**
252
+ * Resolves the name for an operation's grouped header parameters schema.
253
+ *
254
+ * @example Header parameters names
255
+ * `resolver.param.headers(node, param) // → 'deletePetHeadersSchema'`
256
+ */
257
+ headers(node: ast.OperationNode, param: ast.ParameterNode): string;
258
+ };
259
+ /**
260
+ * Names for an operation's responses: per-status schemas, the request body, and the combined,
261
+ * union, and error names.
262
+ */
263
+ response: {
264
+ /**
265
+ * Resolves the name for an operation response by status code.
266
+ *
267
+ * @example Response status names
268
+ * `resolver.response.status(node, 200) // → 'listPetsStatus200Schema'`
269
+ */
270
+ status(node: ast.OperationNode, statusCode: ast.StatusCode): string;
271
+ /**
272
+ * Resolves the request body schema name.
273
+ *
274
+ * @example Request body names
275
+ * `resolver.response.body(node) // → 'createPetBodySchema'`
276
+ */
277
+ body(node: ast.OperationNode): string;
278
+ /**
279
+ * Resolves the name for the collection of all operation responses.
280
+ *
281
+ * @example Responses collection names
282
+ * `resolver.response.responses(node) // → 'listPetsResponsesSchema'`
283
+ */
284
+ responses(node: ast.OperationNode): string;
285
+ /**
286
+ * Resolves the name for the union of all operation responses.
287
+ *
288
+ * @example Response union names
289
+ * `resolver.response.response(node) // → 'listPetsResponseSchema'`
290
+ */
291
+ response(node: ast.OperationNode): string;
292
+ /**
293
+ * Resolves the name for the union of an operation's error (non-2xx) responses. Generated clients
294
+ * validate the error body against this on the non-throw path.
295
+ *
296
+ * @example Error union names
297
+ * `resolver.response.error(node) // → 'listPetsErrorSchema'`
298
+ */
299
+ error(node: ast.OperationNode): string;
300
+ /**
301
+ * Resolves the inferred type name for an operation's combined `{ body, path, query, headers }`
302
+ * options object. Only meaningful when `inferred: true`, since the schema and type this name
303
+ * points to are generated only in that case.
304
+ *
305
+ * @example Options type names
306
+ * `resolver.response.options(node) // → 'ListPetsOptionsSchemaType'`
307
+ */
308
+ options(node: ast.OperationNode): string;
309
+ };
310
+ };
311
+ /**
312
+ * Where the generated Zod schemas are written and how they are exported, plus the optional
313
+ * `group` strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
314
+ *
315
+ * @default { path: 'zod', barrel: { type: 'named' } }
316
+ */
317
+ type Options = OutputOptions & {
318
+ /**
319
+ * Skip operations matching at least one entry in the list.
320
+ */
321
+ exclude?: Array<Exclude>;
322
+ /**
323
+ * Restrict generation to operations matching at least one entry in the list.
324
+ */
325
+ include?: Array<Include>;
326
+ /**
327
+ * Apply a different options object to operations matching a pattern.
328
+ */
329
+ override?: Array<Override<ResolvedOptions>>;
330
+ /**
331
+ * Module specifier used in the `import { z } from '...'` statement.
332
+ * Use `'zod/mini'` for the tree-shakeable bundle.
333
+ *
334
+ * @default mini ? 'zod/mini' : 'zod'
335
+ */
336
+ importPath?: 'zod' | 'zod/mini' | (string & {});
337
+ /**
338
+ * Export a `z.infer<typeof schema>` type alias next to every generated schema.
339
+ * Lets the Zod schema act as the single source of truth.
340
+ */
341
+ inferred?: boolean;
342
+ /**
343
+ * Wrap schemas in `z.coerce` so input is coerced before validation. Useful for
344
+ * form data and query params where everything arrives as a string.
345
+ * - `true` coerces strings, numbers, and dates.
346
+ * - Object form picks per-primitive coercion.
347
+ *
348
+ * `dates` applies to fields typed as `Date` (adapter `dateType: 'date'`): they
349
+ * validate with `z.coerce.date()` instead of the string-to-Date codec. Fields
350
+ * kept as ISO strings (`z.iso.date()`, `z.iso.datetime()`) are never coerced.
351
+ *
352
+ * @default false
353
+ * @see https://zod.dev/?id=coercion-for-primitives
354
+ */
355
+ coercion?: boolean | {
356
+ dates?: boolean;
357
+ strings?: boolean;
358
+ numbers?: boolean;
359
+ };
360
+ /**
361
+ * Validator for `format: uuid` properties.
362
+ * - `'uuid'`: `z.uuid()`. Standard RFC 4122.
363
+ * - `'guid'`: `z.guid()`. Accepts Microsoft-style GUIDs.
364
+ *
365
+ * @default 'uuid'
366
+ */
367
+ guidType?: 'uuid' | 'guid';
368
+ /**
369
+ * Output form for an OpenAPI `pattern` inside `.regex(...)`.
370
+ * - `'literal'`: a regex literal, `.regex(/^[a-z]+$/)`.
371
+ * - `'constructor'`: the `RegExp` constructor, `.regex(new RegExp("^[a-z]+$"))`.
372
+ *
373
+ * @default 'literal'
374
+ */
375
+ regexType?: 'literal' | 'constructor';
376
+ /**
377
+ * Switch to Zod Mini's functional API for better tree-shaking. Also defaults
378
+ * `importPath` to `'zod/mini'`.
379
+ *
380
+ * @default false
381
+ * @beta
382
+ */
383
+ mini?: boolean;
384
+ /**
385
+ * Override how schema and operation names are built. Methods you omit fall back
386
+ * to the default `resolverZod`.
387
+ */
388
+ resolver?: ResolverPatch<ResolverZod>;
389
+ /**
390
+ * Replace the Zod handler for a specific schema type (`'integer'`, `'date'`, ...).
391
+ * When `mini: true`, overrides target the Zod Mini printer instead.
392
+ */
393
+ printer?: {
394
+ nodes?: PrinterZodNodes | PrinterZodMiniNodes;
395
+ };
396
+ /**
397
+ * Macros applied to each schema or operation node before printing.
398
+ */
399
+ macros?: Array<ast.Macro>;
400
+ };
401
+ type ResolvedOptions = {
402
+ output: Output;
403
+ exclude: Array<Exclude>;
404
+ include: Array<Include> | undefined;
405
+ override: Array<Override<ResolvedOptions>>;
406
+ group: Group | null;
407
+ inferred: NonNullable<Options['inferred']>;
408
+ importPath: NonNullable<Options['importPath']>;
409
+ coercion: NonNullable<Options['coercion']>;
410
+ guidType: NonNullable<Options['guidType']>;
411
+ regexType: NonNullable<Options['regexType']>;
412
+ mini: NonNullable<Options['mini']>;
413
+ printer: Options['printer'];
414
+ };
415
+ type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, ResolverZod>;
416
+ declare global {
417
+ namespace Kubb {
418
+ interface PluginRegistry {
419
+ 'plugin-zod': PluginZod;
420
+ }
421
+ }
422
+ }
423
+ //#endregion
424
+ //#region src/generators/zodGenerator.d.ts
425
+ /**
426
+ * Built-in generator for `@kubb/plugin-zod`. Emits one Zod schema per
427
+ * schema in the spec plus per-operation request/response/parameter schemas.
428
+ * When `mini: true`, schemas use the Zod Mini functional API instead of
429
+ * chainable methods.
430
+ */
431
+ declare const zodGenerator: import("kubb/kit").Generator<PluginZod, unknown>;
432
+ //#endregion
5
433
  //#region src/plugin.d.ts
434
+ /**
435
+ * Canonical plugin name for `@kubb/plugin-zod`. Used for driver lookups and
436
+ * cross-plugin dependency references.
437
+ */
6
438
  declare const pluginZodName = "plugin-zod";
7
- declare const pluginZod: (options?: Options | undefined) => _kubb_core0.UserPluginWithLifeCycle<PluginZod>;
439
+ /**
440
+ * Generates Zod v4 schemas from an OpenAPI spec. Use them to validate API
441
+ * responses at runtime, build form schemas, or feed back into router libraries
442
+ * that consume Zod (tRPC, Hono, Elysia). Pair with `@kubb/plugin-axios` or
443
+ * `@kubb/plugin-fetch` and set the client's `validator: 'zod'` to validate every response
444
+ * automatically.
445
+ *
446
+ * @example
447
+ * ```ts
448
+ * import { defineConfig } from 'kubb/config'
449
+ * import { pluginTs } from '@kubb/plugin-ts'
450
+ * import { pluginZod } from '@kubb/plugin-zod'
451
+ *
452
+ * export default defineConfig({
453
+ * input: './petStore.yaml',
454
+ * output: { path: './src/gen' },
455
+ * plugins: [
456
+ * pluginTs(),
457
+ * pluginZod({
458
+ * output: { path: './zod' },
459
+ * inferred: true,
460
+ * }),
461
+ * ],
462
+ * })
463
+ * ```
464
+ */
465
+ declare const pluginZod: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginZod>;
466
+ //#endregion
467
+ //#region src/resolvers/resolverZod.d.ts
468
+ /**
469
+ * Default resolver used by `@kubb/plugin-zod`. Decides the names and file
470
+ * paths for every generated Zod schema. Schemas use camelCase with a
471
+ * `Schema` suffix (`listPetsSchema`); their inferred types use PascalCase
472
+ * with a `SchemaType` suffix (`PetSchemaType`), so the value and the type
473
+ * never share an identifier even when the schema name is all-uppercase.
474
+ *
475
+ * @example Resolve schema and type names
476
+ * ```ts
477
+ * import { resolverZod } from '@kubb/plugin-zod'
478
+ *
479
+ * resolverZod.name('list pets') // 'listPetsSchema'
480
+ * resolverZod.schema.typeName('pet') // 'PetSchemaType'
481
+ * ```
482
+ */
483
+ declare const resolverZod: ResolverZod;
8
484
  //#endregion
9
- export { type PluginZod, pluginZod, pluginZodName };
485
+ export { type PluginZod, type PrinterZodFactory, type PrinterZodMiniFactory, type PrinterZodMiniNodes, type PrinterZodMiniOptions, type PrinterZodNodes, type PrinterZodOptions, type ResolverZod, pluginZod as default, pluginZod, pluginZodName, printerZod, printerZodMini, resolverZod, zodGenerator };
10
486
  //# sourceMappingURL=index.d.ts.map