@kubb/plugin-zod 5.2.0 → 5.2.2
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 +40 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +40 -18
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
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
|
-
|
|
1078
|
-
|
|
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") ?? {},
|
|
@@ -1217,10 +1228,12 @@ function collectDirectionalRefNames({ node, printerOptions }) {
|
|
|
1217
1228
|
* Handlers that never recurse into children, so {@link variesByDirection} can call one directly
|
|
1218
1229
|
* to probe both directions without building a printer.
|
|
1219
1230
|
*
|
|
1220
|
-
* `date`
|
|
1221
|
-
* back on requests
|
|
1222
|
-
*
|
|
1223
|
-
*
|
|
1231
|
+
* `date` and `time` are the built-in two-way conversions, decoding `string → Date` on responses
|
|
1232
|
+
* and encoding back on requests. `date` keeps `date` and `date-time` precision apart. `time` has
|
|
1233
|
+
* no date component, so it decodes onto `1970-01-01` UTC and encodes back to `HH:mm:ss`, dropping
|
|
1234
|
+
* fractional seconds. Only `representation: 'date'` fields convert; ISO-string fields print the
|
|
1235
|
+
* matching `z.iso.*()` either way. A `printer.nodes.date` or `printer.nodes.time` override
|
|
1236
|
+
* replaces the whole handler, direction branch included.
|
|
1224
1237
|
*/
|
|
1225
1238
|
const scalarNodes = {
|
|
1226
1239
|
any: () => "z.any()",
|
|
@@ -1292,7 +1305,8 @@ const scalarNodes = {
|
|
|
1292
1305
|
},
|
|
1293
1306
|
time(node) {
|
|
1294
1307
|
if (node.representation === "string") return "z.iso.time()";
|
|
1295
|
-
|
|
1308
|
+
if (this.options.direction === "encode") return "z.date().transform((value) => value.toISOString().slice(11, 19))";
|
|
1309
|
+
return "z.iso.time().transform((value) => new Date(`1970-01-01T${value}Z`))";
|
|
1296
1310
|
},
|
|
1297
1311
|
uuid(node) {
|
|
1298
1312
|
return `${this.options.guidType === "guid" ? "z.guid()" : "z.uuid()"}${lengthConstraints({
|
|
@@ -1358,15 +1372,16 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
|
1358
1372
|
},
|
|
1359
1373
|
object(node) {
|
|
1360
1374
|
const entries = node.properties ?? [];
|
|
1361
|
-
const
|
|
1375
|
+
const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
|
|
1376
|
+
const isStrict = node.additionalProperties === false && patterns.length === 0;
|
|
1377
|
+
const objectBase = `${isStrict ? "z.strictObject" : "z.object"}(${buildZodObjectShape(this, node)})`;
|
|
1362
1378
|
return (() => {
|
|
1363
|
-
const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
|
|
1364
1379
|
if (node.additionalProperties && node.additionalProperties !== true) {
|
|
1365
1380
|
const catchallType = this.transform(node.additionalProperties);
|
|
1366
1381
|
return catchallType ? `${objectBase}.catchall(${catchallType})` : objectBase;
|
|
1367
1382
|
}
|
|
1368
1383
|
if (node.additionalProperties === true) return `${objectBase}.catchall(${this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})`;
|
|
1369
|
-
if (
|
|
1384
|
+
if (isStrict) return objectBase;
|
|
1370
1385
|
if (patterns.length > 0) {
|
|
1371
1386
|
const values = patterns.map(([, valueSchema]) => {
|
|
1372
1387
|
const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
@@ -1395,7 +1410,7 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
|
1395
1410
|
},
|
|
1396
1411
|
union(node) {
|
|
1397
1412
|
const nodeMembers = node.members ?? [];
|
|
1398
|
-
const members = mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember
|
|
1413
|
+
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
1414
|
if (members.length === 0) return "";
|
|
1400
1415
|
if (members.length === 1) return members[0];
|
|
1401
1416
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
@@ -1449,9 +1464,15 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
|
1449
1464
|
});
|
|
1450
1465
|
//#endregion
|
|
1451
1466
|
//#region src/printers/printerZodMini.ts
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1467
|
+
/**
|
|
1468
|
+
* A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
|
|
1469
|
+
* Marking the node lets `object()` build `z.strictObject(...)` itself, with no rewrite afterwards.
|
|
1470
|
+
*/
|
|
1471
|
+
function strictOneOfNode(node) {
|
|
1472
|
+
return node.type === "object" && node.additionalProperties === void 0 ? {
|
|
1473
|
+
...node,
|
|
1474
|
+
additionalProperties: false
|
|
1475
|
+
} : node;
|
|
1455
1476
|
}
|
|
1456
1477
|
function getMemberConstraintMini({ member, regexType }) {
|
|
1457
1478
|
if (member.primitive === "string") return lengthChecksMini({
|
|
@@ -1614,14 +1635,15 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
|
1614
1635
|
},
|
|
1615
1636
|
object(node) {
|
|
1616
1637
|
const entries = node.properties ?? [];
|
|
1617
|
-
const objectBase = `z.object(${buildZodMiniObjectShape(this, node)})`;
|
|
1618
1638
|
const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
|
|
1639
|
+
const isStrict = node.additionalProperties === false && patterns.length === 0;
|
|
1640
|
+
const objectBase = `${isStrict ? "z.strictObject" : "z.object"}(${buildZodMiniObjectShape(this, node)})`;
|
|
1619
1641
|
if (node.additionalProperties && node.additionalProperties !== true) {
|
|
1620
1642
|
const catchallType = this.transform(node.additionalProperties);
|
|
1621
1643
|
return catchallType ? `z.catchall(${objectBase}, ${catchallType})` : objectBase;
|
|
1622
1644
|
}
|
|
1623
1645
|
if (node.additionalProperties === true) return `z.catchall(${objectBase}, ${this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})`;
|
|
1624
|
-
if (
|
|
1646
|
+
if (isStrict) return objectBase;
|
|
1625
1647
|
if (patterns.length > 0) {
|
|
1626
1648
|
const values = patterns.map(([, valueSchema]) => {
|
|
1627
1649
|
const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
@@ -1649,7 +1671,7 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
|
1649
1671
|
},
|
|
1650
1672
|
union(node) {
|
|
1651
1673
|
const nodeMembers = node.members ?? [];
|
|
1652
|
-
const members = mapSchemaMembers(node, (memberNode) => this.transform(
|
|
1674
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(node.strategy === "one" ? strictOneOfNode(memberNode) : memberNode)).map(({ output }) => output).filter(Boolean);
|
|
1653
1675
|
if (members.length === 0) return "";
|
|
1654
1676
|
if (members.length === 1) return members[0];
|
|
1655
1677
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|