@kubb/plugin-zod 5.2.0 → 5.2.1

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.js CHANGED
@@ -1064,8 +1064,20 @@ function buildGroupedParamsSchema({ params, optional }) {
1064
1064
  }
1065
1065
  //#endregion
1066
1066
  //#region src/printers/printerZod.ts
1067
- function strictOneOfMember$1(member, node, cyclicSchemas) {
1068
- if (node.type === "object" && node.additionalProperties === void 0) return `${member}.strict()`;
1067
+ /**
1068
+ * A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
1069
+ * Marking the node lets `object()` build `z.strictObject(...)` itself; appending `.strict()` to the
1070
+ * printed member instead would read `.shape` eagerly and run a self-reference's deferred getter
1071
+ * inside the temporal dead zone.
1072
+ */
1073
+ function strictOneOfNode$1(node) {
1074
+ return node.type === "object" && node.additionalProperties === void 0 ? {
1075
+ ...node,
1076
+ additionalProperties: false
1077
+ } : node;
1078
+ }
1079
+ __name(strictOneOfNode$1, "strictOneOfNode");
1080
+ function strictOneOfMember(member, node, cyclicSchemas) {
1069
1081
  if (node.type === "ref") {
1070
1082
  if (member.startsWith("z.lazy(")) return member;
1071
1083
  const refName = ast.resolveRefName(node);
@@ -1076,7 +1088,6 @@ function strictOneOfMember$1(member, node, cyclicSchemas) {
1076
1088
  }
1077
1089
  return member;
1078
1090
  }
1079
- __name(strictOneOfMember$1, "strictOneOfMember");
1080
1091
  function getMemberConstraint({ member, regexType }) {
1081
1092
  if (member.primitive === "string") return lengthConstraints({
1082
1093
  ...ast.narrowSchema(member, "string") ?? {},
@@ -1348,15 +1359,16 @@ const printerZod = ast.createPrinter((options) => {
1348
1359
  },
1349
1360
  object(node) {
1350
1361
  const entries = node.properties ?? [];
1351
- const objectBase = `z.object(${buildZodObjectShape(this, node)})`;
1362
+ const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
1363
+ const isStrict = node.additionalProperties === false && patterns.length === 0;
1364
+ const objectBase = `${isStrict ? "z.strictObject" : "z.object"}(${buildZodObjectShape(this, node)})`;
1352
1365
  return (() => {
1353
- const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
1354
1366
  if (node.additionalProperties && node.additionalProperties !== true) {
1355
1367
  const catchallType = this.transform(node.additionalProperties);
1356
1368
  return catchallType ? `${objectBase}.catchall(${catchallType})` : objectBase;
1357
1369
  }
1358
1370
  if (node.additionalProperties === true) return `${objectBase}.catchall(${this.transform(ast.factory.createSchema({ type: "unknown" }))})`;
1359
- if (node.additionalProperties === false && patterns.length === 0) return `${objectBase}.strict()`;
1371
+ if (isStrict) return objectBase;
1360
1372
  if (patterns.length > 0) {
1361
1373
  const values = patterns.map(([, valueSchema]) => {
1362
1374
  const valueType = this.transform(valueSchema) ?? this.transform(ast.factory.createSchema({ type: "unknown" }));
@@ -1385,7 +1397,7 @@ const printerZod = ast.createPrinter((options) => {
1385
1397
  },
1386
1398
  union(node) {
1387
1399
  const nodeMembers = node.members ?? [];
1388
- const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember$1(output, schema, cyclicSchemaNames) : output).filter(Boolean);
1400
+ const members = mapSchemaMembers(node, (memberNode) => this.transform(node.strategy === "one" ? strictOneOfNode$1(memberNode) : memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember(output, schema, cyclicSchemaNames) : output).filter(Boolean);
1389
1401
  if (members.length === 0) return "";
1390
1402
  if (members.length === 1) return members[0];
1391
1403
  const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
@@ -1439,9 +1451,15 @@ const printerZod = ast.createPrinter((options) => {
1439
1451
  });
1440
1452
  //#endregion
1441
1453
  //#region src/printers/printerZodMini.ts
1442
- function strictOneOfMember(member, node) {
1443
- if (node.type === "object" && (node.additionalProperties === void 0 || node.additionalProperties === false)) return member.replace(/^z\.object\(/, "z.strictObject(");
1444
- return member;
1454
+ /**
1455
+ * A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
1456
+ * Marking the node lets `object()` build `z.strictObject(...)` itself, with no rewrite afterwards.
1457
+ */
1458
+ function strictOneOfNode(node) {
1459
+ return node.type === "object" && node.additionalProperties === void 0 ? {
1460
+ ...node,
1461
+ additionalProperties: false
1462
+ } : node;
1445
1463
  }
1446
1464
  function getMemberConstraintMini({ member, regexType }) {
1447
1465
  if (member.primitive === "string") return lengthChecksMini({
@@ -1604,14 +1622,15 @@ const printerZodMini = ast.createPrinter((options) => {
1604
1622
  },
1605
1623
  object(node) {
1606
1624
  const entries = node.properties ?? [];
1607
- const objectBase = `z.object(${buildZodMiniObjectShape(this, node)})`;
1608
1625
  const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
1626
+ const isStrict = node.additionalProperties === false && patterns.length === 0;
1627
+ const objectBase = `${isStrict ? "z.strictObject" : "z.object"}(${buildZodMiniObjectShape(this, node)})`;
1609
1628
  if (node.additionalProperties && node.additionalProperties !== true) {
1610
1629
  const catchallType = this.transform(node.additionalProperties);
1611
1630
  return catchallType ? `z.catchall(${objectBase}, ${catchallType})` : objectBase;
1612
1631
  }
1613
1632
  if (node.additionalProperties === true) return `z.catchall(${objectBase}, ${this.transform(ast.factory.createSchema({ type: "unknown" }))})`;
1614
- if (node.additionalProperties === false && patterns.length === 0) return objectBase.replace(/^z\.object\(/, "z.strictObject(");
1633
+ if (isStrict) return objectBase;
1615
1634
  if (patterns.length > 0) {
1616
1635
  const values = patterns.map(([, valueSchema]) => {
1617
1636
  const valueType = this.transform(valueSchema) ?? this.transform(ast.factory.createSchema({ type: "unknown" }));
@@ -1639,7 +1658,7 @@ const printerZodMini = ast.createPrinter((options) => {
1639
1658
  },
1640
1659
  union(node) {
1641
1660
  const nodeMembers = node.members ?? [];
1642
- const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember(output, schema) : output).filter(Boolean);
1661
+ const members = mapSchemaMembers(node, (memberNode) => this.transform(node.strategy === "one" ? strictOneOfNode(memberNode) : memberNode)).map(({ output }) => output).filter(Boolean);
1643
1662
  if (members.length === 0) return "";
1644
1663
  if (members.length === 1) return members[0];
1645
1664
  const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));