@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.cjs CHANGED
@@ -1074,8 +1074,20 @@ function buildGroupedParamsSchema({ params, optional }) {
1074
1074
  }
1075
1075
  //#endregion
1076
1076
  //#region src/printers/printerZod.ts
1077
- function strictOneOfMember$1(member, node, cyclicSchemas) {
1078
- if (node.type === "object" && node.additionalProperties === void 0) return `${member}.strict()`;
1077
+ /**
1078
+ * A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
1079
+ * Marking the node lets `object()` build `z.strictObject(...)` itself; appending `.strict()` to the
1080
+ * printed member instead would read `.shape` eagerly and run a self-reference's deferred getter
1081
+ * inside the temporal dead zone.
1082
+ */
1083
+ function strictOneOfNode$1(node) {
1084
+ return node.type === "object" && node.additionalProperties === void 0 ? {
1085
+ ...node,
1086
+ additionalProperties: false
1087
+ } : node;
1088
+ }
1089
+ __name(strictOneOfNode$1, "strictOneOfNode");
1090
+ function strictOneOfMember(member, node, cyclicSchemas) {
1079
1091
  if (node.type === "ref") {
1080
1092
  if (member.startsWith("z.lazy(")) return member;
1081
1093
  const refName = kubb_kit.ast.resolveRefName(node);
@@ -1086,7 +1098,6 @@ function strictOneOfMember$1(member, node, cyclicSchemas) {
1086
1098
  }
1087
1099
  return member;
1088
1100
  }
1089
- __name(strictOneOfMember$1, "strictOneOfMember");
1090
1101
  function getMemberConstraint({ member, regexType }) {
1091
1102
  if (member.primitive === "string") return lengthConstraints({
1092
1103
  ...kubb_kit.ast.narrowSchema(member, "string") ?? {},
@@ -1358,15 +1369,16 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
1358
1369
  },
1359
1370
  object(node) {
1360
1371
  const entries = node.properties ?? [];
1361
- const objectBase = `z.object(${buildZodObjectShape(this, node)})`;
1372
+ const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
1373
+ const isStrict = node.additionalProperties === false && patterns.length === 0;
1374
+ const objectBase = `${isStrict ? "z.strictObject" : "z.object"}(${buildZodObjectShape(this, node)})`;
1362
1375
  return (() => {
1363
- const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
1364
1376
  if (node.additionalProperties && node.additionalProperties !== true) {
1365
1377
  const catchallType = this.transform(node.additionalProperties);
1366
1378
  return catchallType ? `${objectBase}.catchall(${catchallType})` : objectBase;
1367
1379
  }
1368
1380
  if (node.additionalProperties === true) return `${objectBase}.catchall(${this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})`;
1369
- if (node.additionalProperties === false && patterns.length === 0) return `${objectBase}.strict()`;
1381
+ if (isStrict) return objectBase;
1370
1382
  if (patterns.length > 0) {
1371
1383
  const values = patterns.map(([, valueSchema]) => {
1372
1384
  const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
@@ -1395,7 +1407,7 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
1395
1407
  },
1396
1408
  union(node) {
1397
1409
  const nodeMembers = node.members ?? [];
1398
- const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember$1(output, schema, cyclicSchemaNames) : output).filter(Boolean);
1410
+ 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);
1399
1411
  if (members.length === 0) return "";
1400
1412
  if (members.length === 1) return members[0];
1401
1413
  const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
@@ -1449,9 +1461,15 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
1449
1461
  });
1450
1462
  //#endregion
1451
1463
  //#region src/printers/printerZodMini.ts
1452
- function strictOneOfMember(member, node) {
1453
- if (node.type === "object" && (node.additionalProperties === void 0 || node.additionalProperties === false)) return member.replace(/^z\.object\(/, "z.strictObject(");
1454
- return member;
1464
+ /**
1465
+ * A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
1466
+ * Marking the node lets `object()` build `z.strictObject(...)` itself, with no rewrite afterwards.
1467
+ */
1468
+ function strictOneOfNode(node) {
1469
+ return node.type === "object" && node.additionalProperties === void 0 ? {
1470
+ ...node,
1471
+ additionalProperties: false
1472
+ } : node;
1455
1473
  }
1456
1474
  function getMemberConstraintMini({ member, regexType }) {
1457
1475
  if (member.primitive === "string") return lengthChecksMini({
@@ -1614,14 +1632,15 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
1614
1632
  },
1615
1633
  object(node) {
1616
1634
  const entries = node.properties ?? [];
1617
- const objectBase = `z.object(${buildZodMiniObjectShape(this, node)})`;
1618
1635
  const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
1636
+ const isStrict = node.additionalProperties === false && patterns.length === 0;
1637
+ const objectBase = `${isStrict ? "z.strictObject" : "z.object"}(${buildZodMiniObjectShape(this, node)})`;
1619
1638
  if (node.additionalProperties && node.additionalProperties !== true) {
1620
1639
  const catchallType = this.transform(node.additionalProperties);
1621
1640
  return catchallType ? `z.catchall(${objectBase}, ${catchallType})` : objectBase;
1622
1641
  }
1623
1642
  if (node.additionalProperties === true) return `z.catchall(${objectBase}, ${this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})`;
1624
- if (node.additionalProperties === false && patterns.length === 0) return objectBase.replace(/^z\.object\(/, "z.strictObject(");
1643
+ if (isStrict) return objectBase;
1625
1644
  if (patterns.length > 0) {
1626
1645
  const values = patterns.map(([, valueSchema]) => {
1627
1646
  const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
@@ -1649,7 +1668,7 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
1649
1668
  },
1650
1669
  union(node) {
1651
1670
  const nodeMembers = node.members ?? [];
1652
- const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember(output, schema) : output).filter(Boolean);
1671
+ const members = mapSchemaMembers(node, (memberNode) => this.transform(node.strategy === "one" ? strictOneOfNode(memberNode) : memberNode)).map(({ output }) => output).filter(Boolean);
1653
1672
  if (members.length === 0) return "";
1654
1673
  if (members.length === 1) return members[0];
1655
1674
  const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));