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