@kubb/plugin-zod 5.0.0-beta.98 → 5.0.0-beta.99
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 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +48 -31
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -346,27 +346,6 @@ function toFilePath(name, caseLast = camelCase) {
|
|
|
346
346
|
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
347
347
|
}
|
|
348
348
|
//#endregion
|
|
349
|
-
//#region ../../internals/shared/src/params.ts
|
|
350
|
-
const caseParamsCache = /* @__PURE__ */ new WeakMap();
|
|
351
|
-
/**
|
|
352
|
-
* Applies camelCase to parameter names and returns a new array without mutating the input.
|
|
353
|
-
*
|
|
354
|
-
* Run it before handing parameters to schema builders so output property keys get the right casing
|
|
355
|
-
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
|
|
356
|
-
* original array is returned unchanged. Results are cached per input array.
|
|
357
|
-
*/
|
|
358
|
-
function caseParams(params, casing) {
|
|
359
|
-
if (!casing) return params;
|
|
360
|
-
const cached = caseParamsCache.get(params);
|
|
361
|
-
if (cached) return cached;
|
|
362
|
-
const result = params.map((param) => ({
|
|
363
|
-
...param,
|
|
364
|
-
name: camelCase(param.name)
|
|
365
|
-
}));
|
|
366
|
-
caseParamsCache.set(params, result);
|
|
367
|
-
return result;
|
|
368
|
-
}
|
|
369
|
-
//#endregion
|
|
370
349
|
//#region ../../internals/shared/src/operation.ts
|
|
371
350
|
/**
|
|
372
351
|
* Maps a content type to the PascalCase suffix used to name per-content-type variants
|
|
@@ -543,6 +522,45 @@ function createGroupConfig(group) {
|
|
|
543
522
|
};
|
|
544
523
|
}
|
|
545
524
|
//#endregion
|
|
525
|
+
//#region ../../internals/shared/src/schemaTraversal.ts
|
|
526
|
+
/**
|
|
527
|
+
* Maps each property of an object schema to its transformed output. Pairs every result with the
|
|
528
|
+
* original property so the printer keeps full control over modifiers, getters, and key syntax.
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* ```ts
|
|
532
|
+
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
|
|
533
|
+
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
|
|
534
|
+
* ```
|
|
535
|
+
*/
|
|
536
|
+
function mapSchemaProperties(node, transform) {
|
|
537
|
+
return node.properties.map((property) => ({
|
|
538
|
+
name: property.name,
|
|
539
|
+
property,
|
|
540
|
+
output: transform(property.schema)
|
|
541
|
+
}));
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Maps each member of a union or intersection schema to its transformed output, pairing every
|
|
545
|
+
* result with the original member.
|
|
546
|
+
*/
|
|
547
|
+
function mapSchemaMembers(node, transform) {
|
|
548
|
+
return (node.members ?? []).map((schema) => ({
|
|
549
|
+
schema,
|
|
550
|
+
output: transform(schema)
|
|
551
|
+
}));
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
|
|
555
|
+
* the original item.
|
|
556
|
+
*/
|
|
557
|
+
function mapSchemaItems(node, transform) {
|
|
558
|
+
return (node.items ?? []).map((schema) => ({
|
|
559
|
+
schema,
|
|
560
|
+
output: transform(schema)
|
|
561
|
+
}));
|
|
562
|
+
}
|
|
563
|
+
//#endregion
|
|
546
564
|
//#region src/components/Zod.tsx
|
|
547
565
|
function Zod({ name, node, printer, inferTypeName, cyclic }) {
|
|
548
566
|
const output = printer.print(node);
|
|
@@ -904,7 +922,7 @@ function buildZodObjectShape(ctx, node) {
|
|
|
904
922
|
const objectNode = ast.narrowSchema(node, "object");
|
|
905
923
|
if (!objectNode) return "{}";
|
|
906
924
|
const isCyclic = (schema) => ctx.options.cyclicSchemas != null && ast.containsCircularRef(schema, { circularSchemas: ctx.options.cyclicSchemas });
|
|
907
|
-
return buildObject(
|
|
925
|
+
return buildObject(mapSchemaProperties(objectNode, (schema) => {
|
|
908
926
|
const hasSelfRef = isCyclic(schema);
|
|
909
927
|
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
910
928
|
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
@@ -1056,18 +1074,18 @@ const printerZod = ast.createPrinter((options) => {
|
|
|
1056
1074
|
})();
|
|
1057
1075
|
},
|
|
1058
1076
|
array(node) {
|
|
1059
|
-
const base = `z.array(${
|
|
1077
|
+
const base = `z.array(${mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean).join(", ") || this.transform(ast.factory.createSchema({ type: "unknown" }))})${lengthConstraints({
|
|
1060
1078
|
...node,
|
|
1061
1079
|
regexType: this.options.regexType
|
|
1062
1080
|
})}`;
|
|
1063
1081
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
1064
1082
|
},
|
|
1065
1083
|
tuple(node) {
|
|
1066
|
-
return `z.tuple(${buildList(
|
|
1084
|
+
return `z.tuple(${buildList(mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean))})`;
|
|
1067
1085
|
},
|
|
1068
1086
|
union(node) {
|
|
1069
1087
|
const nodeMembers = node.members ?? [];
|
|
1070
|
-
const members =
|
|
1088
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember$1(output, schema, cyclicSchemaNames) : output).filter(Boolean);
|
|
1071
1089
|
if (members.length === 0) return "";
|
|
1072
1090
|
if (members.length === 1) return members[0];
|
|
1073
1091
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
@@ -1144,7 +1162,7 @@ function buildZodMiniObjectShape(ctx, node) {
|
|
|
1144
1162
|
const objectNode = ast.narrowSchema(node, "object");
|
|
1145
1163
|
if (!objectNode) return "{}";
|
|
1146
1164
|
const isCyclic = (schema) => ctx.options.cyclicSchemas != null && ast.containsCircularRef(schema, { circularSchemas: ctx.options.cyclicSchemas });
|
|
1147
|
-
return buildObject(
|
|
1165
|
+
return buildObject(mapSchemaProperties(objectNode, (schema) => {
|
|
1148
1166
|
const hasSelfRef = isCyclic(schema);
|
|
1149
1167
|
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
1150
1168
|
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
@@ -1282,18 +1300,18 @@ const printerZodMini = ast.createPrinter((options) => {
|
|
|
1282
1300
|
return objectBase;
|
|
1283
1301
|
},
|
|
1284
1302
|
array(node) {
|
|
1285
|
-
const base = `z.array(${
|
|
1303
|
+
const base = `z.array(${mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean).join(", ") || this.transform(ast.factory.createSchema({ type: "unknown" }))})${lengthChecksMini({
|
|
1286
1304
|
...node,
|
|
1287
1305
|
regexType: this.options.regexType
|
|
1288
1306
|
})}`;
|
|
1289
1307
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
1290
1308
|
},
|
|
1291
1309
|
tuple(node) {
|
|
1292
|
-
return `z.tuple(${buildList(
|
|
1310
|
+
return `z.tuple(${buildList(mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean))})`;
|
|
1293
1311
|
},
|
|
1294
1312
|
union(node) {
|
|
1295
1313
|
const nodeMembers = node.members ?? [];
|
|
1296
|
-
const members =
|
|
1314
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember(output, schema) : output).filter(Boolean);
|
|
1297
1315
|
if (members.length === 0) return "";
|
|
1298
1316
|
if (members.length === 1) return members[0];
|
|
1299
1317
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
@@ -1521,7 +1539,6 @@ const zodGenerator = defineGenerator({
|
|
|
1521
1539
|
const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1522
1540
|
const dateType = getOasAdapter(adapter).options.dateType;
|
|
1523
1541
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
1524
|
-
const params = caseParams(node.parameters, "camelcase");
|
|
1525
1542
|
const meta = { file: resolver.file({
|
|
1526
1543
|
name: node.operationId,
|
|
1527
1544
|
extname: ".ts",
|
|
@@ -1627,7 +1644,7 @@ const zodGenerator = defineGenerator({
|
|
|
1627
1644
|
name
|
|
1628
1645
|
});
|
|
1629
1646
|
}
|
|
1630
|
-
const paramSchemas =
|
|
1647
|
+
const paramSchemas = node.parameters.map((param) => renderSchemaEntry({
|
|
1631
1648
|
schema: param.schema,
|
|
1632
1649
|
name: resolver.param.name(node, param),
|
|
1633
1650
|
direction: "input"
|