@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.cjs
CHANGED
|
@@ -356,27 +356,6 @@ function toFilePath(name, caseLast = camelCase) {
|
|
|
356
356
|
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
357
357
|
}
|
|
358
358
|
//#endregion
|
|
359
|
-
//#region ../../internals/shared/src/params.ts
|
|
360
|
-
const caseParamsCache = /* @__PURE__ */ new WeakMap();
|
|
361
|
-
/**
|
|
362
|
-
* Applies camelCase to parameter names and returns a new array without mutating the input.
|
|
363
|
-
*
|
|
364
|
-
* Run it before handing parameters to schema builders so output property keys get the right casing
|
|
365
|
-
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
|
|
366
|
-
* original array is returned unchanged. Results are cached per input array.
|
|
367
|
-
*/
|
|
368
|
-
function caseParams(params, casing) {
|
|
369
|
-
if (!casing) return params;
|
|
370
|
-
const cached = caseParamsCache.get(params);
|
|
371
|
-
if (cached) return cached;
|
|
372
|
-
const result = params.map((param) => ({
|
|
373
|
-
...param,
|
|
374
|
-
name: camelCase(param.name)
|
|
375
|
-
}));
|
|
376
|
-
caseParamsCache.set(params, result);
|
|
377
|
-
return result;
|
|
378
|
-
}
|
|
379
|
-
//#endregion
|
|
380
359
|
//#region ../../internals/shared/src/operation.ts
|
|
381
360
|
/**
|
|
382
361
|
* Maps a content type to the PascalCase suffix used to name per-content-type variants
|
|
@@ -553,6 +532,45 @@ function createGroupConfig(group) {
|
|
|
553
532
|
};
|
|
554
533
|
}
|
|
555
534
|
//#endregion
|
|
535
|
+
//#region ../../internals/shared/src/schemaTraversal.ts
|
|
536
|
+
/**
|
|
537
|
+
* Maps each property of an object schema to its transformed output. Pairs every result with the
|
|
538
|
+
* original property so the printer keeps full control over modifiers, getters, and key syntax.
|
|
539
|
+
*
|
|
540
|
+
* @example
|
|
541
|
+
* ```ts
|
|
542
|
+
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
|
|
543
|
+
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
|
|
544
|
+
* ```
|
|
545
|
+
*/
|
|
546
|
+
function mapSchemaProperties(node, transform) {
|
|
547
|
+
return node.properties.map((property) => ({
|
|
548
|
+
name: property.name,
|
|
549
|
+
property,
|
|
550
|
+
output: transform(property.schema)
|
|
551
|
+
}));
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Maps each member of a union or intersection schema to its transformed output, pairing every
|
|
555
|
+
* result with the original member.
|
|
556
|
+
*/
|
|
557
|
+
function mapSchemaMembers(node, transform) {
|
|
558
|
+
return (node.members ?? []).map((schema) => ({
|
|
559
|
+
schema,
|
|
560
|
+
output: transform(schema)
|
|
561
|
+
}));
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
|
|
565
|
+
* the original item.
|
|
566
|
+
*/
|
|
567
|
+
function mapSchemaItems(node, transform) {
|
|
568
|
+
return (node.items ?? []).map((schema) => ({
|
|
569
|
+
schema,
|
|
570
|
+
output: transform(schema)
|
|
571
|
+
}));
|
|
572
|
+
}
|
|
573
|
+
//#endregion
|
|
556
574
|
//#region src/components/Zod.tsx
|
|
557
575
|
function Zod({ name, node, printer, inferTypeName, cyclic }) {
|
|
558
576
|
const output = printer.print(node);
|
|
@@ -914,7 +932,7 @@ function buildZodObjectShape(ctx, node) {
|
|
|
914
932
|
const objectNode = kubb_kit.ast.narrowSchema(node, "object");
|
|
915
933
|
if (!objectNode) return "{}";
|
|
916
934
|
const isCyclic = (schema) => ctx.options.cyclicSchemas != null && kubb_kit.ast.containsCircularRef(schema, { circularSchemas: ctx.options.cyclicSchemas });
|
|
917
|
-
return buildObject(
|
|
935
|
+
return buildObject(mapSchemaProperties(objectNode, (schema) => {
|
|
918
936
|
const hasSelfRef = isCyclic(schema);
|
|
919
937
|
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
920
938
|
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
@@ -1066,18 +1084,18 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
|
1066
1084
|
})();
|
|
1067
1085
|
},
|
|
1068
1086
|
array(node) {
|
|
1069
|
-
const base = `z.array(${
|
|
1087
|
+
const base = `z.array(${mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean).join(", ") || this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})${lengthConstraints({
|
|
1070
1088
|
...node,
|
|
1071
1089
|
regexType: this.options.regexType
|
|
1072
1090
|
})}`;
|
|
1073
1091
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
1074
1092
|
},
|
|
1075
1093
|
tuple(node) {
|
|
1076
|
-
return `z.tuple(${buildList(
|
|
1094
|
+
return `z.tuple(${buildList(mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean))})`;
|
|
1077
1095
|
},
|
|
1078
1096
|
union(node) {
|
|
1079
1097
|
const nodeMembers = node.members ?? [];
|
|
1080
|
-
const members =
|
|
1098
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember$1(output, schema, cyclicSchemaNames) : output).filter(Boolean);
|
|
1081
1099
|
if (members.length === 0) return "";
|
|
1082
1100
|
if (members.length === 1) return members[0];
|
|
1083
1101
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
@@ -1154,7 +1172,7 @@ function buildZodMiniObjectShape(ctx, node) {
|
|
|
1154
1172
|
const objectNode = kubb_kit.ast.narrowSchema(node, "object");
|
|
1155
1173
|
if (!objectNode) return "{}";
|
|
1156
1174
|
const isCyclic = (schema) => ctx.options.cyclicSchemas != null && kubb_kit.ast.containsCircularRef(schema, { circularSchemas: ctx.options.cyclicSchemas });
|
|
1157
|
-
return buildObject(
|
|
1175
|
+
return buildObject(mapSchemaProperties(objectNode, (schema) => {
|
|
1158
1176
|
const hasSelfRef = isCyclic(schema);
|
|
1159
1177
|
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
1160
1178
|
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
@@ -1292,18 +1310,18 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
|
1292
1310
|
return objectBase;
|
|
1293
1311
|
},
|
|
1294
1312
|
array(node) {
|
|
1295
|
-
const base = `z.array(${
|
|
1313
|
+
const base = `z.array(${mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean).join(", ") || this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})${lengthChecksMini({
|
|
1296
1314
|
...node,
|
|
1297
1315
|
regexType: this.options.regexType
|
|
1298
1316
|
})}`;
|
|
1299
1317
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
1300
1318
|
},
|
|
1301
1319
|
tuple(node) {
|
|
1302
|
-
return `z.tuple(${buildList(
|
|
1320
|
+
return `z.tuple(${buildList(mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean))})`;
|
|
1303
1321
|
},
|
|
1304
1322
|
union(node) {
|
|
1305
1323
|
const nodeMembers = node.members ?? [];
|
|
1306
|
-
const members =
|
|
1324
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember(output, schema) : output).filter(Boolean);
|
|
1307
1325
|
if (members.length === 0) return "";
|
|
1308
1326
|
if (members.length === 1) return members[0];
|
|
1309
1327
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
@@ -1531,7 +1549,6 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1531
1549
|
const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1532
1550
|
const dateType = getOasAdapter(adapter).options.dateType;
|
|
1533
1551
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
1534
|
-
const params = caseParams(node.parameters, "camelcase");
|
|
1535
1552
|
const meta = { file: resolver.file({
|
|
1536
1553
|
name: node.operationId,
|
|
1537
1554
|
extname: ".ts",
|
|
@@ -1637,7 +1654,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1637
1654
|
name
|
|
1638
1655
|
});
|
|
1639
1656
|
}
|
|
1640
|
-
const paramSchemas =
|
|
1657
|
+
const paramSchemas = node.parameters.map((param) => renderSchemaEntry({
|
|
1641
1658
|
schema: param.schema,
|
|
1642
1659
|
name: resolver.param.name(node, param),
|
|
1643
1660
|
direction: "input"
|