@kubb/plugin-zod 5.0.0-beta.22 → 5.0.0-beta.25
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/dist/index.cjs +48 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +77 -32
- package/dist/index.js +48 -24
- package/dist/index.js.map +1 -1
- package/extension.yaml +646 -185
- package/package.json +4 -4
- package/src/components/Operations.tsx +4 -4
- package/src/components/Zod.tsx +1 -1
- package/src/generators/zodGenerator.tsx +21 -9
- package/src/plugin.ts +21 -8
- package/src/resolvers/resolverZod.ts +9 -4
- package/src/types.ts +42 -21
- package/src/utils.ts +6 -6
package/dist/index.d.ts
CHANGED
|
@@ -222,41 +222,53 @@ type ResolverZod = Resolver & ast.OperationParamsResolver & {
|
|
|
222
222
|
};
|
|
223
223
|
type Options = {
|
|
224
224
|
/**
|
|
225
|
-
*
|
|
225
|
+
* Where the generated Zod schemas are written and how they are exported.
|
|
226
|
+
*
|
|
227
|
+
* @default { path: 'zod', barrel: { type: 'named' } }
|
|
226
228
|
*/
|
|
227
229
|
output?: Output;
|
|
228
230
|
/**
|
|
229
|
-
*
|
|
231
|
+
* Split generated files into subfolders based on the operation's tag.
|
|
230
232
|
*/
|
|
231
233
|
group?: Group;
|
|
232
234
|
/**
|
|
233
|
-
*
|
|
235
|
+
* Skip operations matching at least one entry in the list.
|
|
234
236
|
*/
|
|
235
237
|
exclude?: Array<Exclude>;
|
|
236
238
|
/**
|
|
237
|
-
*
|
|
239
|
+
* Restrict generation to operations matching at least one entry in the list.
|
|
238
240
|
*/
|
|
239
241
|
include?: Array<Include>;
|
|
240
242
|
/**
|
|
241
|
-
*
|
|
243
|
+
* Apply a different options object to operations matching a pattern.
|
|
242
244
|
*/
|
|
243
245
|
override?: Array<Override<ResolvedOptions>>;
|
|
244
246
|
/**
|
|
245
|
-
*
|
|
247
|
+
* Module specifier used in the `import { z } from '...'` statement.
|
|
248
|
+
* Use `'zod/mini'` for the tree-shakeable bundle.
|
|
246
249
|
*
|
|
247
250
|
* @default 'zod'
|
|
248
251
|
*/
|
|
249
252
|
importPath?: 'zod' | 'zod/mini' | (string & {});
|
|
250
253
|
/**
|
|
251
|
-
*
|
|
254
|
+
* Tie each Zod schema to its TypeScript type from `@kubb/plugin-ts`. Requires
|
|
255
|
+
* `@kubb/plugin-ts` in the plugins list. TypeScript fails compilation when the
|
|
256
|
+
* schema drifts from the type.
|
|
252
257
|
*/
|
|
253
258
|
typed?: boolean;
|
|
254
259
|
/**
|
|
255
|
-
*
|
|
260
|
+
* Export a `z.infer<typeof schema>` type alias next to every generated schema.
|
|
261
|
+
* Lets the Zod schema act as the single source of truth.
|
|
256
262
|
*/
|
|
257
263
|
inferred?: boolean;
|
|
258
264
|
/**
|
|
259
|
-
*
|
|
265
|
+
* Wrap schemas in `z.coerce` so input is coerced before validation. Useful for
|
|
266
|
+
* form data and query params where everything arrives as a string.
|
|
267
|
+
* - `true` coerces strings, numbers, and dates.
|
|
268
|
+
* - Object form picks per-primitive coercion.
|
|
269
|
+
*
|
|
270
|
+
* @default false
|
|
271
|
+
* @see https://zod.dev/?id=coercion-for-primitives
|
|
260
272
|
*/
|
|
261
273
|
coercion?: boolean | {
|
|
262
274
|
dates?: boolean;
|
|
@@ -264,50 +276,59 @@ type Options = {
|
|
|
264
276
|
numbers?: boolean;
|
|
265
277
|
};
|
|
266
278
|
/**
|
|
267
|
-
*
|
|
279
|
+
* Emit an `operations.ts` file with request body, query/path params, and per-status
|
|
280
|
+
* response schemas grouped by operation.
|
|
268
281
|
*/
|
|
269
282
|
operations?: boolean;
|
|
270
283
|
/**
|
|
271
|
-
* Validator
|
|
284
|
+
* Validator for `format: uuid` properties.
|
|
285
|
+
* - `'uuid'` — `z.uuid()`. Standard RFC 4122.
|
|
286
|
+
* - `'guid'` — `z.guid()`. Accepts Microsoft-style GUIDs.
|
|
272
287
|
*
|
|
273
288
|
* @default 'uuid'
|
|
274
289
|
*/
|
|
275
290
|
guidType?: 'uuid' | 'guid';
|
|
276
291
|
/**
|
|
277
|
-
*
|
|
292
|
+
* Switch to Zod Mini's functional API for better tree-shaking. Also defaults
|
|
293
|
+
* `importPath` to `'zod/mini'`.
|
|
278
294
|
*
|
|
279
295
|
* @default false
|
|
296
|
+
* @beta
|
|
280
297
|
*/
|
|
281
298
|
mini?: boolean;
|
|
282
299
|
/**
|
|
283
|
-
*
|
|
284
|
-
*
|
|
285
|
-
*
|
|
300
|
+
* Wrap the generated Zod schema string with extra calls. Receives the raw output
|
|
301
|
+
* and the originating `SchemaNode`. Useful for round-tripping OpenAPI metadata
|
|
302
|
+
* back into Zod (e.g. `.openapi(...)`).
|
|
286
303
|
*/
|
|
287
304
|
wrapOutput?: (arg: {
|
|
288
305
|
output: string;
|
|
289
306
|
schema: ast.SchemaNode;
|
|
290
307
|
}) => string | undefined;
|
|
291
308
|
/**
|
|
292
|
-
*
|
|
309
|
+
* Rename properties inside path/query/header schemas. Body schemas are unaffected.
|
|
310
|
+
*
|
|
311
|
+
* @note Must match the value of `paramsCasing` on `@kubb/plugin-ts`.
|
|
293
312
|
*/
|
|
294
313
|
paramsCasing?: 'camelcase';
|
|
295
314
|
/**
|
|
296
|
-
*
|
|
315
|
+
* Custom generators that run alongside the built-in Zod generators.
|
|
297
316
|
*/
|
|
298
317
|
generators?: Array<Generator<PluginZod>>;
|
|
299
318
|
/**
|
|
300
|
-
* Override
|
|
319
|
+
* Override how schema and operation names are built. Methods you omit fall back
|
|
320
|
+
* to the default `resolverZod`.
|
|
301
321
|
*/
|
|
302
322
|
resolver?: Partial<ResolverZod> & ThisType<ResolverZod>;
|
|
303
323
|
/**
|
|
304
|
-
*
|
|
324
|
+
* Replace the Zod handler for a specific schema type (`'integer'`, `'date'`, ...).
|
|
325
|
+
* When `mini: true`, overrides target the Zod Mini printer instead.
|
|
305
326
|
*/
|
|
306
327
|
printer?: {
|
|
307
328
|
nodes?: PrinterZodNodes | PrinterZodMiniNodes;
|
|
308
329
|
};
|
|
309
330
|
/**
|
|
310
|
-
* AST visitor to
|
|
331
|
+
* AST visitor applied to each schema or operation node before printing.
|
|
311
332
|
*/
|
|
312
333
|
transformer?: ast.Visitor;
|
|
313
334
|
};
|
|
@@ -316,7 +337,7 @@ type ResolvedOptions = {
|
|
|
316
337
|
exclude: Array<Exclude>;
|
|
317
338
|
include: Array<Include> | undefined;
|
|
318
339
|
override: Array<Override<ResolvedOptions>>;
|
|
319
|
-
group: Group |
|
|
340
|
+
group: Group | null;
|
|
320
341
|
typed: NonNullable<Options['typed']>;
|
|
321
342
|
inferred: NonNullable<Options['inferred']>;
|
|
322
343
|
importPath: NonNullable<Options['importPath']>;
|
|
@@ -338,23 +359,42 @@ declare global {
|
|
|
338
359
|
}
|
|
339
360
|
//#endregion
|
|
340
361
|
//#region src/generators/zodGenerator.d.ts
|
|
362
|
+
/**
|
|
363
|
+
* Built-in generator for `@kubb/plugin-zod`. Emits one Zod schema per
|
|
364
|
+
* schema in the spec plus per-operation request/response/parameter schemas.
|
|
365
|
+
* When `mini: true`, schemas use the Zod Mini functional API instead of
|
|
366
|
+
* chainable methods.
|
|
367
|
+
*/
|
|
341
368
|
declare const zodGenerator: _$_kubb_core0.Generator<PluginZod, unknown>;
|
|
342
369
|
//#endregion
|
|
343
370
|
//#region src/plugin.d.ts
|
|
344
371
|
/**
|
|
345
|
-
* Canonical plugin name for `@kubb/plugin-zod
|
|
372
|
+
* Canonical plugin name for `@kubb/plugin-zod`. Used for driver lookups and
|
|
373
|
+
* cross-plugin dependency references.
|
|
346
374
|
*/
|
|
347
375
|
declare const pluginZodName = "plugin-zod";
|
|
348
376
|
/**
|
|
349
|
-
* Generates Zod
|
|
350
|
-
*
|
|
351
|
-
*
|
|
377
|
+
* Generates Zod v4 schemas from an OpenAPI spec. Use them to validate API
|
|
378
|
+
* responses at runtime, build form schemas, or feed back into router libraries
|
|
379
|
+
* that consume Zod (tRPC, Hono, Elysia). Pair with `@kubb/plugin-client` and
|
|
380
|
+
* set the client's `parser: 'zod'` to validate every response automatically.
|
|
352
381
|
*
|
|
353
|
-
* @example
|
|
382
|
+
* @example
|
|
354
383
|
* ```ts
|
|
355
|
-
* import
|
|
384
|
+
* import { defineConfig } from 'kubb'
|
|
385
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
386
|
+
* import { pluginZod } from '@kubb/plugin-zod'
|
|
387
|
+
*
|
|
356
388
|
* export default defineConfig({
|
|
357
|
-
*
|
|
389
|
+
* input: { path: './petStore.yaml' },
|
|
390
|
+
* output: { path: './src/gen' },
|
|
391
|
+
* plugins: [
|
|
392
|
+
* pluginTs(),
|
|
393
|
+
* pluginZod({
|
|
394
|
+
* output: { path: './zod' },
|
|
395
|
+
* typed: true,
|
|
396
|
+
* }),
|
|
397
|
+
* ],
|
|
358
398
|
* })
|
|
359
399
|
* ```
|
|
360
400
|
*/
|
|
@@ -362,12 +402,17 @@ declare const pluginZod: (options?: Options | undefined) => _$_kubb_core0.Plugin
|
|
|
362
402
|
//#endregion
|
|
363
403
|
//#region src/resolvers/resolverZod.d.ts
|
|
364
404
|
/**
|
|
365
|
-
*
|
|
405
|
+
* Default resolver used by `@kubb/plugin-zod`. Decides the names and file
|
|
406
|
+
* paths for every generated Zod schema. Schemas use camelCase with a
|
|
407
|
+
* `Schema` suffix (`listPetsSchema`); their inferred types use PascalCase.
|
|
366
408
|
*
|
|
367
|
-
*
|
|
409
|
+
* @example Resolve schema and type names
|
|
410
|
+
* ```ts
|
|
411
|
+
* import { resolverZod } from '@kubb/plugin-zod'
|
|
368
412
|
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
413
|
+
* resolverZod.default('list pets', 'function') // 'listPetsSchema'
|
|
414
|
+
* resolverZod.resolveSchemaTypeName('pet') // 'PetSchema'
|
|
415
|
+
* ```
|
|
371
416
|
*/
|
|
372
417
|
declare const resolverZod: ResolverZod;
|
|
373
418
|
//#endregion
|
package/dist/index.js
CHANGED
|
@@ -261,11 +261,11 @@ function buildSchemaNames(node, { params, resolver }) {
|
|
|
261
261
|
}
|
|
262
262
|
responses["default"] = resolver.resolveResponseName(node);
|
|
263
263
|
return {
|
|
264
|
-
request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) :
|
|
264
|
+
request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null,
|
|
265
265
|
parameters: {
|
|
266
|
-
path: pathParam ? resolver.resolvePathParamsName(node, pathParam) :
|
|
267
|
-
query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) :
|
|
268
|
-
header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) :
|
|
266
|
+
path: pathParam ? resolver.resolvePathParamsName(node, pathParam) : null,
|
|
267
|
+
query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) : null,
|
|
268
|
+
header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) : null
|
|
269
269
|
},
|
|
270
270
|
responses,
|
|
271
271
|
errors
|
|
@@ -717,6 +717,12 @@ const printerZodMini = ast.definePrinter((options) => {
|
|
|
717
717
|
//#region src/generators/zodGenerator.tsx
|
|
718
718
|
const zodPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
719
719
|
const zodMiniPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
720
|
+
/**
|
|
721
|
+
* Built-in generator for `@kubb/plugin-zod`. Emits one Zod schema per
|
|
722
|
+
* schema in the spec plus per-operation request/response/parameter schemas.
|
|
723
|
+
* When `mini: true`, schemas use the Zod Mini functional API instead of
|
|
724
|
+
* chainable methods.
|
|
725
|
+
*/
|
|
720
726
|
const zodGenerator = defineGenerator({
|
|
721
727
|
name: "zod",
|
|
722
728
|
renderer: jsxRendererSync,
|
|
@@ -735,7 +741,7 @@ const zodGenerator = defineGenerator({
|
|
|
735
741
|
}, {
|
|
736
742
|
root,
|
|
737
743
|
output,
|
|
738
|
-
group
|
|
744
|
+
group: group ?? void 0
|
|
739
745
|
}).path
|
|
740
746
|
}));
|
|
741
747
|
const meta = {
|
|
@@ -746,10 +752,10 @@ const zodGenerator = defineGenerator({
|
|
|
746
752
|
}, {
|
|
747
753
|
root,
|
|
748
754
|
output,
|
|
749
|
-
group
|
|
755
|
+
group: group ?? void 0
|
|
750
756
|
})
|
|
751
757
|
};
|
|
752
|
-
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(node.name) :
|
|
758
|
+
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(node.name) : null;
|
|
753
759
|
const cyclicSchemas = new Set(ctx.meta.circularNames);
|
|
754
760
|
const schemaPrinter = mini ? getCachedMiniPrinter() : getCachedStdPrinter();
|
|
755
761
|
function getCachedStdPrinter() {
|
|
@@ -835,12 +841,12 @@ const zodGenerator = defineGenerator({
|
|
|
835
841
|
}, {
|
|
836
842
|
root,
|
|
837
843
|
output,
|
|
838
|
-
group
|
|
844
|
+
group: group ?? void 0
|
|
839
845
|
}) };
|
|
840
846
|
const cyclicSchemas = new Set(ctx.meta.circularNames);
|
|
841
847
|
function renderSchemaEntry({ schema, name, keysToOmit }) {
|
|
842
848
|
if (!schema) return null;
|
|
843
|
-
const inferTypeName = inferred ? resolver.resolveTypeName(name) :
|
|
849
|
+
const inferTypeName = inferred ? resolver.resolveTypeName(name) : null;
|
|
844
850
|
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
845
851
|
name: resolver.resolveSchemaName(schemaName),
|
|
846
852
|
path: resolver.resolveFile({
|
|
@@ -849,7 +855,7 @@ const zodGenerator = defineGenerator({
|
|
|
849
855
|
}, {
|
|
850
856
|
root,
|
|
851
857
|
output,
|
|
852
|
-
group
|
|
858
|
+
group: group ?? void 0
|
|
853
859
|
}).path
|
|
854
860
|
}));
|
|
855
861
|
const cachedStd = zodPrinterCache.get(resolver);
|
|
@@ -972,7 +978,7 @@ const zodGenerator = defineGenerator({
|
|
|
972
978
|
}, {
|
|
973
979
|
root,
|
|
974
980
|
output,
|
|
975
|
-
group
|
|
981
|
+
group: group ?? void 0
|
|
976
982
|
}) };
|
|
977
983
|
const transformedOperations = nodes.map((node) => {
|
|
978
984
|
return {
|
|
@@ -997,7 +1003,7 @@ const zodGenerator = defineGenerator({
|
|
|
997
1003
|
}, {
|
|
998
1004
|
root,
|
|
999
1005
|
output,
|
|
1000
|
-
group
|
|
1006
|
+
group: group ?? void 0
|
|
1001
1007
|
});
|
|
1002
1008
|
return names.map((name) => /* @__PURE__ */ jsx(File.Import, {
|
|
1003
1009
|
name: [name],
|
|
@@ -1036,12 +1042,17 @@ const zodGenerator = defineGenerator({
|
|
|
1036
1042
|
//#endregion
|
|
1037
1043
|
//#region src/resolvers/resolverZod.ts
|
|
1038
1044
|
/**
|
|
1039
|
-
*
|
|
1045
|
+
* Default resolver used by `@kubb/plugin-zod`. Decides the names and file
|
|
1046
|
+
* paths for every generated Zod schema. Schemas use camelCase with a
|
|
1047
|
+
* `Schema` suffix (`listPetsSchema`); their inferred types use PascalCase.
|
|
1040
1048
|
*
|
|
1041
|
-
*
|
|
1049
|
+
* @example Resolve schema and type names
|
|
1050
|
+
* ```ts
|
|
1051
|
+
* import { resolverZod } from '@kubb/plugin-zod'
|
|
1042
1052
|
*
|
|
1043
|
-
*
|
|
1044
|
-
*
|
|
1053
|
+
* resolverZod.default('list pets', 'function') // 'listPetsSchema'
|
|
1054
|
+
* resolverZod.resolveSchemaTypeName('pet') // 'PetSchema'
|
|
1055
|
+
* ```
|
|
1045
1056
|
*/
|
|
1046
1057
|
const resolverZod = defineResolver(() => {
|
|
1047
1058
|
return {
|
|
@@ -1094,19 +1105,32 @@ const resolverZod = defineResolver(() => {
|
|
|
1094
1105
|
//#endregion
|
|
1095
1106
|
//#region src/plugin.ts
|
|
1096
1107
|
/**
|
|
1097
|
-
* Canonical plugin name for `@kubb/plugin-zod
|
|
1108
|
+
* Canonical plugin name for `@kubb/plugin-zod`. Used for driver lookups and
|
|
1109
|
+
* cross-plugin dependency references.
|
|
1098
1110
|
*/
|
|
1099
1111
|
const pluginZodName = "plugin-zod";
|
|
1100
1112
|
/**
|
|
1101
|
-
* Generates Zod
|
|
1102
|
-
*
|
|
1103
|
-
*
|
|
1113
|
+
* Generates Zod v4 schemas from an OpenAPI spec. Use them to validate API
|
|
1114
|
+
* responses at runtime, build form schemas, or feed back into router libraries
|
|
1115
|
+
* that consume Zod (tRPC, Hono, Elysia). Pair with `@kubb/plugin-client` and
|
|
1116
|
+
* set the client's `parser: 'zod'` to validate every response automatically.
|
|
1104
1117
|
*
|
|
1105
|
-
* @example
|
|
1118
|
+
* @example
|
|
1106
1119
|
* ```ts
|
|
1107
|
-
* import
|
|
1120
|
+
* import { defineConfig } from 'kubb'
|
|
1121
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1122
|
+
* import { pluginZod } from '@kubb/plugin-zod'
|
|
1123
|
+
*
|
|
1108
1124
|
* export default defineConfig({
|
|
1109
|
-
*
|
|
1125
|
+
* input: { path: './petStore.yaml' },
|
|
1126
|
+
* output: { path: './src/gen' },
|
|
1127
|
+
* plugins: [
|
|
1128
|
+
* pluginTs(),
|
|
1129
|
+
* pluginZod({
|
|
1130
|
+
* output: { path: './zod' },
|
|
1131
|
+
* typed: true,
|
|
1132
|
+
* }),
|
|
1133
|
+
* ],
|
|
1110
1134
|
* })
|
|
1111
1135
|
* ```
|
|
1112
1136
|
*/
|
|
@@ -1121,7 +1145,7 @@ const pluginZod = definePlugin((options) => {
|
|
|
1121
1145
|
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
1122
1146
|
return `${camelCase(ctx.group)}Controller`;
|
|
1123
1147
|
}
|
|
1124
|
-
} :
|
|
1148
|
+
} : null;
|
|
1125
1149
|
return {
|
|
1126
1150
|
name: pluginZodName,
|
|
1127
1151
|
options,
|