@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/README.md +25 -5
- package/dist/index.cjs +644 -197
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +103 -34
- package/dist/index.js +640 -199
- package/dist/index.js.map +1 -1
- package/extension.yaml +968 -0
- package/package.json +10 -13
- package/src/components/Operations.tsx +6 -5
- package/src/components/Zod.tsx +1 -1
- package/src/generators/zodGenerator.tsx +210 -60
- package/src/plugin.ts +23 -20
- package/src/printers/printerZod.ts +91 -68
- package/src/printers/printerZodMini.ts +46 -49
- package/src/resolvers/resolverZod.ts +31 -19
- package/src/types.ts +57 -21
- package/src/utils.ts +113 -36
package/dist/index.d.ts
CHANGED
|
@@ -51,12 +51,21 @@ type PrinterZodOptions = {
|
|
|
51
51
|
/**
|
|
52
52
|
* Properties to exclude using `.omit({ key: true })`.
|
|
53
53
|
*/
|
|
54
|
-
keysToOmit?: Array<string
|
|
54
|
+
keysToOmit?: Array<string> | null;
|
|
55
55
|
/**
|
|
56
56
|
* Schema names that form circular dependency chains.
|
|
57
57
|
* Properties referencing these emit lazy getters wrapping refs in `z.lazy(() => …)`.
|
|
58
58
|
*/
|
|
59
59
|
cyclicSchemas?: ReadonlySet<string>;
|
|
60
|
+
/**
|
|
61
|
+
* Print direction for `dateType: 'date'` fields (`Date` in TypeScript):
|
|
62
|
+
* - `'output'` (default) — decode the wire `string` into a `Date` (response bodies).
|
|
63
|
+
* - `'input'` — encode a `Date` back into the wire `string` (request bodies/params).
|
|
64
|
+
*
|
|
65
|
+
* Diverging the directions requires the generator to emit an `${name}InputSchema`
|
|
66
|
+
* variant for each date-bearing component.
|
|
67
|
+
*/
|
|
68
|
+
direction?: 'input' | 'output';
|
|
60
69
|
/**
|
|
61
70
|
* Custom handler map for node type overrides.
|
|
62
71
|
*/
|
|
@@ -121,7 +130,7 @@ type PrinterZodMiniOptions = {
|
|
|
121
130
|
/**
|
|
122
131
|
* Properties to exclude using `.omit({ key: true })`.
|
|
123
132
|
*/
|
|
124
|
-
keysToOmit?: Array<string
|
|
133
|
+
keysToOmit?: Array<string> | null;
|
|
125
134
|
/**
|
|
126
135
|
* Schema names that form circular dependency chains.
|
|
127
136
|
* Properties referencing these emit lazy getters wrapping refs in `z.lazy(() => …)`.
|
|
@@ -166,6 +175,21 @@ type ResolverZod = Resolver & ast.OperationParamsResolver & {
|
|
|
166
175
|
* `resolver.resolveSchemaTypeName('pet') // → 'Pet'`
|
|
167
176
|
*/
|
|
168
177
|
resolveSchemaTypeName(this: ResolverZod, name: string): string;
|
|
178
|
+
/**
|
|
179
|
+
* Resolves the schema function name for the request (input) direction of a
|
|
180
|
+
* date-bearing component, where `Date` is encoded back to a wire `string`.
|
|
181
|
+
*
|
|
182
|
+
* @example Input schema names
|
|
183
|
+
* `resolver.resolveInputSchemaName('order') // → 'orderInputSchema'`
|
|
184
|
+
*/
|
|
185
|
+
resolveInputSchemaName(this: ResolverZod, name: string): string;
|
|
186
|
+
/**
|
|
187
|
+
* Resolves the inferred type name for the request (input) direction variant.
|
|
188
|
+
*
|
|
189
|
+
* @example Input schema type names
|
|
190
|
+
* `resolver.resolveInputSchemaTypeName('order') // → 'OrderInputSchema'`
|
|
191
|
+
*/
|
|
192
|
+
resolveInputSchemaTypeName(this: ResolverZod, name: string): string;
|
|
169
193
|
/**
|
|
170
194
|
* Resolves the generated type name from the schema.
|
|
171
195
|
*
|
|
@@ -222,41 +246,53 @@ type ResolverZod = Resolver & ast.OperationParamsResolver & {
|
|
|
222
246
|
};
|
|
223
247
|
type Options = {
|
|
224
248
|
/**
|
|
225
|
-
*
|
|
249
|
+
* Where the generated Zod schemas are written and how they are exported.
|
|
250
|
+
*
|
|
251
|
+
* @default { path: 'zod', barrel: { type: 'named' } }
|
|
226
252
|
*/
|
|
227
253
|
output?: Output;
|
|
228
254
|
/**
|
|
229
|
-
*
|
|
255
|
+
* Split generated files into subfolders based on the operation's tag.
|
|
230
256
|
*/
|
|
231
257
|
group?: Group;
|
|
232
258
|
/**
|
|
233
|
-
*
|
|
259
|
+
* Skip operations matching at least one entry in the list.
|
|
234
260
|
*/
|
|
235
261
|
exclude?: Array<Exclude>;
|
|
236
262
|
/**
|
|
237
|
-
*
|
|
263
|
+
* Restrict generation to operations matching at least one entry in the list.
|
|
238
264
|
*/
|
|
239
265
|
include?: Array<Include>;
|
|
240
266
|
/**
|
|
241
|
-
*
|
|
267
|
+
* Apply a different options object to operations matching a pattern.
|
|
242
268
|
*/
|
|
243
269
|
override?: Array<Override<ResolvedOptions>>;
|
|
244
270
|
/**
|
|
245
|
-
*
|
|
271
|
+
* Module specifier used in the `import { z } from '...'` statement.
|
|
272
|
+
* Use `'zod/mini'` for the tree-shakeable bundle.
|
|
246
273
|
*
|
|
247
274
|
* @default 'zod'
|
|
248
275
|
*/
|
|
249
276
|
importPath?: 'zod' | 'zod/mini' | (string & {});
|
|
250
277
|
/**
|
|
251
|
-
*
|
|
278
|
+
* Tie each Zod schema to its TypeScript type from `@kubb/plugin-ts`. Requires
|
|
279
|
+
* `@kubb/plugin-ts` in the plugins list. TypeScript fails compilation when the
|
|
280
|
+
* schema drifts from the type.
|
|
252
281
|
*/
|
|
253
282
|
typed?: boolean;
|
|
254
283
|
/**
|
|
255
|
-
*
|
|
284
|
+
* Export a `z.infer<typeof schema>` type alias next to every generated schema.
|
|
285
|
+
* Lets the Zod schema act as the single source of truth.
|
|
256
286
|
*/
|
|
257
287
|
inferred?: boolean;
|
|
258
288
|
/**
|
|
259
|
-
*
|
|
289
|
+
* Wrap schemas in `z.coerce` so input is coerced before validation. Useful for
|
|
290
|
+
* form data and query params where everything arrives as a string.
|
|
291
|
+
* - `true` coerces strings, numbers, and dates.
|
|
292
|
+
* - Object form picks per-primitive coercion.
|
|
293
|
+
*
|
|
294
|
+
* @default false
|
|
295
|
+
* @see https://zod.dev/?id=coercion-for-primitives
|
|
260
296
|
*/
|
|
261
297
|
coercion?: boolean | {
|
|
262
298
|
dates?: boolean;
|
|
@@ -264,50 +300,59 @@ type Options = {
|
|
|
264
300
|
numbers?: boolean;
|
|
265
301
|
};
|
|
266
302
|
/**
|
|
267
|
-
*
|
|
303
|
+
* Emit an `operations.ts` file with request body, query/path params, and per-status
|
|
304
|
+
* response schemas grouped by operation.
|
|
268
305
|
*/
|
|
269
306
|
operations?: boolean;
|
|
270
307
|
/**
|
|
271
|
-
* Validator
|
|
308
|
+
* Validator for `format: uuid` properties.
|
|
309
|
+
* - `'uuid'` — `z.uuid()`. Standard RFC 4122.
|
|
310
|
+
* - `'guid'` — `z.guid()`. Accepts Microsoft-style GUIDs.
|
|
272
311
|
*
|
|
273
312
|
* @default 'uuid'
|
|
274
313
|
*/
|
|
275
314
|
guidType?: 'uuid' | 'guid';
|
|
276
315
|
/**
|
|
277
|
-
*
|
|
316
|
+
* Switch to Zod Mini's functional API for better tree-shaking. Also defaults
|
|
317
|
+
* `importPath` to `'zod/mini'`.
|
|
278
318
|
*
|
|
279
319
|
* @default false
|
|
320
|
+
* @beta
|
|
280
321
|
*/
|
|
281
322
|
mini?: boolean;
|
|
282
323
|
/**
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
324
|
+
* Wrap the generated Zod schema string with extra calls. Receives the raw output
|
|
325
|
+
* and the originating `SchemaNode`. Useful for round-tripping OpenAPI metadata
|
|
326
|
+
* back into Zod (e.g. `.openapi(...)`).
|
|
286
327
|
*/
|
|
287
328
|
wrapOutput?: (arg: {
|
|
288
329
|
output: string;
|
|
289
330
|
schema: ast.SchemaNode;
|
|
290
331
|
}) => string | undefined;
|
|
291
332
|
/**
|
|
292
|
-
*
|
|
333
|
+
* Rename properties inside path/query/header schemas. Body schemas are unaffected.
|
|
334
|
+
*
|
|
335
|
+
* @note Must match the value of `paramsCasing` on `@kubb/plugin-ts`.
|
|
293
336
|
*/
|
|
294
337
|
paramsCasing?: 'camelcase';
|
|
295
338
|
/**
|
|
296
|
-
*
|
|
339
|
+
* Custom generators that run alongside the built-in Zod generators.
|
|
297
340
|
*/
|
|
298
341
|
generators?: Array<Generator<PluginZod>>;
|
|
299
342
|
/**
|
|
300
|
-
* Override
|
|
343
|
+
* Override how schema and operation names are built. Methods you omit fall back
|
|
344
|
+
* to the default `resolverZod`.
|
|
301
345
|
*/
|
|
302
346
|
resolver?: Partial<ResolverZod> & ThisType<ResolverZod>;
|
|
303
347
|
/**
|
|
304
|
-
*
|
|
348
|
+
* Replace the Zod handler for a specific schema type (`'integer'`, `'date'`, ...).
|
|
349
|
+
* When `mini: true`, overrides target the Zod Mini printer instead.
|
|
305
350
|
*/
|
|
306
351
|
printer?: {
|
|
307
352
|
nodes?: PrinterZodNodes | PrinterZodMiniNodes;
|
|
308
353
|
};
|
|
309
354
|
/**
|
|
310
|
-
* AST visitor to
|
|
355
|
+
* AST visitor applied to each schema or operation node before printing.
|
|
311
356
|
*/
|
|
312
357
|
transformer?: ast.Visitor;
|
|
313
358
|
};
|
|
@@ -316,7 +361,7 @@ type ResolvedOptions = {
|
|
|
316
361
|
exclude: Array<Exclude>;
|
|
317
362
|
include: Array<Include> | undefined;
|
|
318
363
|
override: Array<Override<ResolvedOptions>>;
|
|
319
|
-
group: Group |
|
|
364
|
+
group: Group | null;
|
|
320
365
|
typed: NonNullable<Options['typed']>;
|
|
321
366
|
inferred: NonNullable<Options['inferred']>;
|
|
322
367
|
importPath: NonNullable<Options['importPath']>;
|
|
@@ -338,23 +383,42 @@ declare global {
|
|
|
338
383
|
}
|
|
339
384
|
//#endregion
|
|
340
385
|
//#region src/generators/zodGenerator.d.ts
|
|
386
|
+
/**
|
|
387
|
+
* Built-in generator for `@kubb/plugin-zod`. Emits one Zod schema per
|
|
388
|
+
* schema in the spec plus per-operation request/response/parameter schemas.
|
|
389
|
+
* When `mini: true`, schemas use the Zod Mini functional API instead of
|
|
390
|
+
* chainable methods.
|
|
391
|
+
*/
|
|
341
392
|
declare const zodGenerator: _$_kubb_core0.Generator<PluginZod, unknown>;
|
|
342
393
|
//#endregion
|
|
343
394
|
//#region src/plugin.d.ts
|
|
344
395
|
/**
|
|
345
|
-
* Canonical plugin name for `@kubb/plugin-zod
|
|
396
|
+
* Canonical plugin name for `@kubb/plugin-zod`. Used for driver lookups and
|
|
397
|
+
* cross-plugin dependency references.
|
|
346
398
|
*/
|
|
347
399
|
declare const pluginZodName = "plugin-zod";
|
|
348
400
|
/**
|
|
349
|
-
* Generates Zod
|
|
350
|
-
*
|
|
351
|
-
*
|
|
401
|
+
* Generates Zod v4 schemas from an OpenAPI spec. Use them to validate API
|
|
402
|
+
* responses at runtime, build form schemas, or feed back into router libraries
|
|
403
|
+
* that consume Zod (tRPC, Hono, Elysia). Pair with `@kubb/plugin-client` and
|
|
404
|
+
* set the client's `parser: 'zod'` to validate every response automatically.
|
|
352
405
|
*
|
|
353
|
-
* @example
|
|
406
|
+
* @example
|
|
354
407
|
* ```ts
|
|
355
|
-
* import
|
|
408
|
+
* import { defineConfig } from 'kubb'
|
|
409
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
410
|
+
* import { pluginZod } from '@kubb/plugin-zod'
|
|
411
|
+
*
|
|
356
412
|
* export default defineConfig({
|
|
357
|
-
*
|
|
413
|
+
* input: { path: './petStore.yaml' },
|
|
414
|
+
* output: { path: './src/gen' },
|
|
415
|
+
* plugins: [
|
|
416
|
+
* pluginTs(),
|
|
417
|
+
* pluginZod({
|
|
418
|
+
* output: { path: './zod' },
|
|
419
|
+
* typed: true,
|
|
420
|
+
* }),
|
|
421
|
+
* ],
|
|
358
422
|
* })
|
|
359
423
|
* ```
|
|
360
424
|
*/
|
|
@@ -362,12 +426,17 @@ declare const pluginZod: (options?: Options | undefined) => _$_kubb_core0.Plugin
|
|
|
362
426
|
//#endregion
|
|
363
427
|
//#region src/resolvers/resolverZod.d.ts
|
|
364
428
|
/**
|
|
365
|
-
*
|
|
429
|
+
* Default resolver used by `@kubb/plugin-zod`. Decides the names and file
|
|
430
|
+
* paths for every generated Zod schema. Schemas use camelCase with a
|
|
431
|
+
* `Schema` suffix (`listPetsSchema`); their inferred types use PascalCase.
|
|
366
432
|
*
|
|
367
|
-
*
|
|
433
|
+
* @example Resolve schema and type names
|
|
434
|
+
* ```ts
|
|
435
|
+
* import { resolverZod } from '@kubb/plugin-zod'
|
|
368
436
|
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
437
|
+
* resolverZod.default('list pets', 'function') // 'listPetsSchema'
|
|
438
|
+
* resolverZod.resolveSchemaTypeName('pet') // 'PetSchema'
|
|
439
|
+
* ```
|
|
371
440
|
*/
|
|
372
441
|
declare const resolverZod: ResolverZod;
|
|
373
442
|
//#endregion
|