zod 4.0.12 → 4.0.13
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/package.json +1 -1
- package/src/v4/classic/tests/optional.test.ts +13 -0
- package/src/v4/core/schemas.ts +10 -1
- package/src/v4/core/versions.ts +1 -1
- package/v4/core/schemas.cjs +10 -1
- package/v4/core/schemas.js +10 -1
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
package/package.json
CHANGED
|
@@ -121,3 +121,16 @@ test("pipe optionality inside objects", () => {
|
|
|
121
121
|
e: string;
|
|
122
122
|
}>();
|
|
123
123
|
});
|
|
124
|
+
|
|
125
|
+
test("optional prop with pipe", () => {
|
|
126
|
+
const schema = z.object({
|
|
127
|
+
id: z
|
|
128
|
+
.union([z.number(), z.string().nullish()])
|
|
129
|
+
.transform((val) => (val === null || val === undefined ? val : Number(val)))
|
|
130
|
+
.pipe(z.number())
|
|
131
|
+
.optional(),
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
schema.parse({});
|
|
135
|
+
schema.parse({}, { jitless: true });
|
|
136
|
+
});
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -3007,6 +3007,13 @@ export interface $ZodOptional<T extends SomeType = $ZodType> extends $ZodType {
|
|
|
3007
3007
|
_zod: $ZodOptionalInternals<T>;
|
|
3008
3008
|
}
|
|
3009
3009
|
|
|
3010
|
+
function handleOptionalResult(result: ParsePayload, input: unknown) {
|
|
3011
|
+
if (result.issues.length && input === undefined) {
|
|
3012
|
+
return { issues: [], value: undefined };
|
|
3013
|
+
}
|
|
3014
|
+
return result;
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3010
3017
|
export const $ZodOptional: core.$constructor<$ZodOptional> = /*@__PURE__*/ core.$constructor(
|
|
3011
3018
|
"$ZodOptional",
|
|
3012
3019
|
(inst, def) => {
|
|
@@ -3024,7 +3031,9 @@ export const $ZodOptional: core.$constructor<$ZodOptional> = /*@__PURE__*/ core.
|
|
|
3024
3031
|
|
|
3025
3032
|
inst._zod.parse = (payload, ctx) => {
|
|
3026
3033
|
if (def.innerType._zod.optin === "optional") {
|
|
3027
|
-
|
|
3034
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
3035
|
+
if (result instanceof Promise) return result.then((r) => handleOptionalResult(r, payload.value));
|
|
3036
|
+
return handleOptionalResult(result, payload.value);
|
|
3028
3037
|
}
|
|
3029
3038
|
if (payload.value === undefined) {
|
|
3030
3039
|
return payload;
|
package/src/v4/core/versions.ts
CHANGED
package/v4/core/schemas.cjs
CHANGED
|
@@ -1415,6 +1415,12 @@ exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
|
|
|
1415
1415
|
return payload;
|
|
1416
1416
|
};
|
|
1417
1417
|
});
|
|
1418
|
+
function handleOptionalResult(result, input) {
|
|
1419
|
+
if (result.issues.length && input === undefined) {
|
|
1420
|
+
return { issues: [], value: undefined };
|
|
1421
|
+
}
|
|
1422
|
+
return result;
|
|
1423
|
+
}
|
|
1418
1424
|
exports.$ZodOptional = core.$constructor("$ZodOptional", (inst, def) => {
|
|
1419
1425
|
exports.$ZodType.init(inst, def);
|
|
1420
1426
|
inst._zod.optin = "optional";
|
|
@@ -1428,7 +1434,10 @@ exports.$ZodOptional = core.$constructor("$ZodOptional", (inst, def) => {
|
|
|
1428
1434
|
});
|
|
1429
1435
|
inst._zod.parse = (payload, ctx) => {
|
|
1430
1436
|
if (def.innerType._zod.optin === "optional") {
|
|
1431
|
-
|
|
1437
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
1438
|
+
if (result instanceof Promise)
|
|
1439
|
+
return result.then((r) => handleOptionalResult(r, payload.value));
|
|
1440
|
+
return handleOptionalResult(result, payload.value);
|
|
1432
1441
|
}
|
|
1433
1442
|
if (payload.value === undefined) {
|
|
1434
1443
|
return payload;
|
package/v4/core/schemas.js
CHANGED
|
@@ -1384,6 +1384,12 @@ export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (i
|
|
|
1384
1384
|
return payload;
|
|
1385
1385
|
};
|
|
1386
1386
|
});
|
|
1387
|
+
function handleOptionalResult(result, input) {
|
|
1388
|
+
if (result.issues.length && input === undefined) {
|
|
1389
|
+
return { issues: [], value: undefined };
|
|
1390
|
+
}
|
|
1391
|
+
return result;
|
|
1392
|
+
}
|
|
1387
1393
|
export const $ZodOptional = /*@__PURE__*/ core.$constructor("$ZodOptional", (inst, def) => {
|
|
1388
1394
|
$ZodType.init(inst, def);
|
|
1389
1395
|
inst._zod.optin = "optional";
|
|
@@ -1397,7 +1403,10 @@ export const $ZodOptional = /*@__PURE__*/ core.$constructor("$ZodOptional", (ins
|
|
|
1397
1403
|
});
|
|
1398
1404
|
inst._zod.parse = (payload, ctx) => {
|
|
1399
1405
|
if (def.innerType._zod.optin === "optional") {
|
|
1400
|
-
|
|
1406
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
1407
|
+
if (result instanceof Promise)
|
|
1408
|
+
return result.then((r) => handleOptionalResult(r, payload.value));
|
|
1409
|
+
return handleOptionalResult(result, payload.value);
|
|
1401
1410
|
}
|
|
1402
1411
|
if (payload.value === undefined) {
|
|
1403
1412
|
return payload;
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED