zod 4.4.2 → 4.4.3
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/schemas.ts +2 -0
- package/src/v4/classic/tests/catch.test.ts +52 -4
- package/src/v4/classic/tests/partial.test.ts +20 -23
- package/src/v4/classic/tests/preprocess.test.ts +25 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +0 -1
- package/src/v4/core/schemas.ts +16 -7
- package/src/v4/core/versions.ts +1 -1
- package/v4/classic/schemas.cjs +2 -0
- package/v4/classic/schemas.js +2 -0
- package/v4/core/schemas.cjs +11 -7
- package/v4/core/schemas.d.cts +5 -0
- package/v4/core/schemas.d.ts +5 -0
- package/v4/core/schemas.js +11 -7
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
package/package.json
CHANGED
|
@@ -2034,10 +2034,12 @@ export const ZodTransform: core.$constructor<ZodTransform> = /*@__PURE__*/ core.
|
|
|
2034
2034
|
if (output instanceof Promise) {
|
|
2035
2035
|
return output.then((output) => {
|
|
2036
2036
|
payload.value = output;
|
|
2037
|
+
payload.fallback = true;
|
|
2037
2038
|
return payload;
|
|
2038
2039
|
});
|
|
2039
2040
|
}
|
|
2040
2041
|
payload.value = output;
|
|
2042
|
+
payload.fallback = true;
|
|
2041
2043
|
return payload;
|
|
2042
2044
|
};
|
|
2043
2045
|
}
|
|
@@ -128,8 +128,9 @@ test("native enum", () => {
|
|
|
128
128
|
fruit: z.nativeEnum(Fruits).catch(Fruits.apple),
|
|
129
129
|
});
|
|
130
130
|
|
|
131
|
-
|
|
132
|
-
expect(schema.
|
|
131
|
+
// Absent keys flow through to the catch handler.
|
|
132
|
+
expect(schema.parse({})).toEqual({ fruit: Fruits.apple });
|
|
133
|
+
expect(schema.parse({}, { jitless: true })).toEqual({ fruit: Fruits.apple });
|
|
133
134
|
expect(schema.parse({ fruit: 15 })).toEqual({ fruit: Fruits.apple });
|
|
134
135
|
});
|
|
135
136
|
|
|
@@ -138,8 +139,8 @@ test("enum", () => {
|
|
|
138
139
|
fruit: z.enum(["apple", "orange"]).catch("apple"),
|
|
139
140
|
});
|
|
140
141
|
|
|
141
|
-
expect(schema.
|
|
142
|
-
expect(schema.
|
|
142
|
+
expect(schema.parse({})).toEqual({ fruit: "apple" });
|
|
143
|
+
expect(schema.parse({}, { jitless: true })).toEqual({ fruit: "apple" });
|
|
143
144
|
expect(schema.parse({ fruit: true })).toEqual({ fruit: "apple" });
|
|
144
145
|
expect(schema.parse({ fruit: 15 })).toEqual({ fruit: "apple" });
|
|
145
146
|
});
|
|
@@ -276,3 +277,50 @@ test("direction-aware catch", () => {
|
|
|
276
277
|
// But valid values should still work in reverse
|
|
277
278
|
expect(z.encode(schema, "world")).toBe("world");
|
|
278
279
|
});
|
|
280
|
+
|
|
281
|
+
test("optional clobbers catch through pipe boundaries", () => {
|
|
282
|
+
expect(
|
|
283
|
+
z
|
|
284
|
+
.string()
|
|
285
|
+
.catch("X")
|
|
286
|
+
.transform((s) => s + "!")
|
|
287
|
+
.optional()
|
|
288
|
+
.parse(undefined)
|
|
289
|
+
).toBeUndefined();
|
|
290
|
+
expect(z.string().catch("X").pipe(z.string()).optional().parse(undefined)).toBeUndefined();
|
|
291
|
+
expect(
|
|
292
|
+
z
|
|
293
|
+
.string()
|
|
294
|
+
.catch("X")
|
|
295
|
+
.transform((s) => s + "!")
|
|
296
|
+
.transform((s) => s.toLowerCase())
|
|
297
|
+
.optional()
|
|
298
|
+
.parse(undefined)
|
|
299
|
+
).toBeUndefined();
|
|
300
|
+
expect(
|
|
301
|
+
z
|
|
302
|
+
.object({
|
|
303
|
+
a: z
|
|
304
|
+
.string()
|
|
305
|
+
.catch("X")
|
|
306
|
+
.transform((s) => s + "!")
|
|
307
|
+
.optional(),
|
|
308
|
+
})
|
|
309
|
+
.parse({})
|
|
310
|
+
).toEqual({});
|
|
311
|
+
|
|
312
|
+
expect(
|
|
313
|
+
z
|
|
314
|
+
.string()
|
|
315
|
+
.catch("X")
|
|
316
|
+
.transform((s) => s + "!")
|
|
317
|
+
.parse("hi")
|
|
318
|
+
).toBe("hi!");
|
|
319
|
+
expect(
|
|
320
|
+
z
|
|
321
|
+
.string()
|
|
322
|
+
.catch("X")
|
|
323
|
+
.transform((s) => s + "!")
|
|
324
|
+
.parse(123)
|
|
325
|
+
).toBe("X!");
|
|
326
|
+
});
|
|
@@ -156,30 +156,27 @@ test("catch/prefault/default", () => {
|
|
|
156
156
|
f: z.string().prefault("prefault value"),
|
|
157
157
|
});
|
|
158
158
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
159
|
+
// Catch (d) and default/prefault (b, c, e, f) handle absent keys gracefully.
|
|
160
|
+
// `a: catch().optional()` short-circuits to undefined when the original
|
|
161
|
+
// input was undefined, so the property is omitted from the output. All
|
|
162
|
+
// other catch/default/prefault keys produce their fallback values.
|
|
163
|
+
expect(mySchema.parse({})).toMatchInlineSnapshot(`
|
|
164
|
+
{
|
|
165
|
+
"b": "default value",
|
|
166
|
+
"c": "prefault value",
|
|
167
|
+
"d": "catch value",
|
|
168
|
+
"e": "default value",
|
|
169
|
+
"f": "prefault value",
|
|
170
|
+
}
|
|
170
171
|
`);
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
"d",
|
|
180
|
-
],
|
|
181
|
-
},
|
|
182
|
-
]
|
|
172
|
+
expect(mySchema.parse({}, { jitless: true })).toMatchInlineSnapshot(`
|
|
173
|
+
{
|
|
174
|
+
"b": "default value",
|
|
175
|
+
"c": "prefault value",
|
|
176
|
+
"d": "catch value",
|
|
177
|
+
"e": "default value",
|
|
178
|
+
"f": "prefault value",
|
|
179
|
+
}
|
|
183
180
|
`);
|
|
184
181
|
|
|
185
182
|
expect(mySchema.parse({ d: undefined })).toMatchInlineSnapshot(`
|
|
@@ -281,6 +281,31 @@ test("perform transform with non-fatal issues", () => {
|
|
|
281
281
|
`);
|
|
282
282
|
});
|
|
283
283
|
|
|
284
|
+
test("preprocess accepts absent object keys (4.3 parity)", () => {
|
|
285
|
+
const schema = z.object({ a: z.preprocess((v) => v ?? "X", z.string()) });
|
|
286
|
+
expect(schema.parse({})).toEqual({ a: "X" });
|
|
287
|
+
expect(schema.parse({ a: "hi" })).toEqual({ a: "hi" });
|
|
288
|
+
expect(schema.parse({ a: undefined })).toEqual({ a: "X" });
|
|
289
|
+
|
|
290
|
+
// Outer optional clobbers preprocess output on undefined input
|
|
291
|
+
expect(
|
|
292
|
+
z
|
|
293
|
+
.preprocess((v) => v ?? "X", z.string())
|
|
294
|
+
.optional()
|
|
295
|
+
.parse(undefined)
|
|
296
|
+
).toBeUndefined();
|
|
297
|
+
expect(
|
|
298
|
+
z
|
|
299
|
+
.preprocess((v) => v ?? "X", z.string())
|
|
300
|
+
.optional()
|
|
301
|
+
.parse("hi")
|
|
302
|
+
).toBe("hi");
|
|
303
|
+
expect(z.object({ a: z.preprocess((v) => v ?? "X", z.string()).optional() }).parse({})).toEqual({});
|
|
304
|
+
|
|
305
|
+
// Top-level direct call unchanged
|
|
306
|
+
expect(z.preprocess((v) => v ?? "X", z.string()).parse(undefined)).toBe("X");
|
|
307
|
+
});
|
|
308
|
+
|
|
284
309
|
// https://github.com/colinhacks/zod/issues/5917
|
|
285
310
|
test("optional propagates through preprocess inside object", () => {
|
|
286
311
|
const outer = z.object({ x: z.preprocess((v) => v, z.number()).optional() });
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -36,6 +36,11 @@ export interface ParsePayload<T = unknown> {
|
|
|
36
36
|
issues: errors.$ZodRawIssue[];
|
|
37
37
|
/** A way to mark a whole payload as aborted. Used in codecs/pipes. */
|
|
38
38
|
aborted?: boolean;
|
|
39
|
+
/** @internal Marks a value as a fallback that an outer wrapper (e.g.
|
|
40
|
+
* $ZodOptional) may override with its own interpretation when input was
|
|
41
|
+
* undefined. Set by $ZodCatch when catchValue substitutes and by every
|
|
42
|
+
* $ZodTransform invocation. */
|
|
43
|
+
fallback?: boolean | undefined;
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
|
|
@@ -3417,6 +3422,7 @@ export const $ZodTransform: core.$constructor<$ZodTransform> = /*@__PURE__*/ cor
|
|
|
3417
3422
|
"$ZodTransform",
|
|
3418
3423
|
(inst, def) => {
|
|
3419
3424
|
$ZodType.init(inst, def);
|
|
3425
|
+
inst._zod.optin = "optional";
|
|
3420
3426
|
inst._zod.parse = (payload, ctx) => {
|
|
3421
3427
|
if (ctx.direction === "backward") {
|
|
3422
3428
|
throw new core.$ZodEncodeError(inst.constructor.name);
|
|
@@ -3427,6 +3433,7 @@ export const $ZodTransform: core.$constructor<$ZodTransform> = /*@__PURE__*/ cor
|
|
|
3427
3433
|
const output = _out instanceof Promise ? _out : Promise.resolve(_out);
|
|
3428
3434
|
return output.then((output) => {
|
|
3429
3435
|
payload.value = output;
|
|
3436
|
+
payload.fallback = true;
|
|
3430
3437
|
return payload;
|
|
3431
3438
|
});
|
|
3432
3439
|
}
|
|
@@ -3436,6 +3443,7 @@ export const $ZodTransform: core.$constructor<$ZodTransform> = /*@__PURE__*/ cor
|
|
|
3436
3443
|
}
|
|
3437
3444
|
|
|
3438
3445
|
payload.value = _out;
|
|
3446
|
+
payload.fallback = true;
|
|
3439
3447
|
return payload;
|
|
3440
3448
|
};
|
|
3441
3449
|
}
|
|
@@ -3468,7 +3476,7 @@ export interface $ZodOptional<T extends SomeType = $ZodType> extends $ZodType {
|
|
|
3468
3476
|
}
|
|
3469
3477
|
|
|
3470
3478
|
function handleOptionalResult(result: ParsePayload, input: unknown) {
|
|
3471
|
-
if (result.issues.length
|
|
3479
|
+
if (input === undefined && (result.issues.length || result.fallback)) {
|
|
3472
3480
|
return { issues: [], value: undefined };
|
|
3473
3481
|
}
|
|
3474
3482
|
return result;
|
|
@@ -3491,9 +3499,10 @@ export const $ZodOptional: core.$constructor<$ZodOptional> = /*@__PURE__*/ core.
|
|
|
3491
3499
|
|
|
3492
3500
|
inst._zod.parse = (payload, ctx) => {
|
|
3493
3501
|
if (def.innerType._zod.optin === "optional") {
|
|
3502
|
+
const input = payload.value;
|
|
3494
3503
|
const result = def.innerType._zod.run(payload, ctx);
|
|
3495
|
-
if (result instanceof Promise) return result.then((r) => handleOptionalResult(r,
|
|
3496
|
-
return handleOptionalResult(result,
|
|
3504
|
+
if (result instanceof Promise) return result.then((r) => handleOptionalResult(r, input));
|
|
3505
|
+
return handleOptionalResult(result, input);
|
|
3497
3506
|
}
|
|
3498
3507
|
if (payload.value === undefined) {
|
|
3499
3508
|
return payload;
|
|
@@ -3886,7 +3895,7 @@ export interface $ZodCatch<T extends SomeType = $ZodType> extends $ZodType {
|
|
|
3886
3895
|
|
|
3887
3896
|
export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
|
|
3888
3897
|
$ZodType.init(inst, def);
|
|
3889
|
-
|
|
3898
|
+
inst._zod.optin = "optional";
|
|
3890
3899
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
3891
3900
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
3892
3901
|
|
|
@@ -3909,6 +3918,7 @@ export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$const
|
|
|
3909
3918
|
input: payload.value,
|
|
3910
3919
|
});
|
|
3911
3920
|
payload.issues = [];
|
|
3921
|
+
payload.fallback = true;
|
|
3912
3922
|
}
|
|
3913
3923
|
|
|
3914
3924
|
return payload;
|
|
@@ -3926,6 +3936,7 @@ export const $ZodCatch: core.$constructor<$ZodCatch> = /*@__PURE__*/ core.$const
|
|
|
3926
3936
|
});
|
|
3927
3937
|
|
|
3928
3938
|
payload.issues = [];
|
|
3939
|
+
payload.fallback = true;
|
|
3929
3940
|
}
|
|
3930
3941
|
|
|
3931
3942
|
return payload;
|
|
@@ -4030,7 +4041,7 @@ function handlePipeResult(left: ParsePayload, next: $ZodType, ctx: ParseContextI
|
|
|
4030
4041
|
left.aborted = true;
|
|
4031
4042
|
return left;
|
|
4032
4043
|
}
|
|
4033
|
-
return next._zod.run({ value: left.value, issues: left.issues }, ctx);
|
|
4044
|
+
return next._zod.run({ value: left.value, issues: left.issues, fallback: left.fallback }, ctx);
|
|
4034
4045
|
}
|
|
4035
4046
|
|
|
4036
4047
|
////////////////////////////////////////////
|
|
@@ -4144,8 +4155,6 @@ export const $ZodPreprocess: core.$constructor<$ZodPreprocess> = /*@__PURE__*/ c
|
|
|
4144
4155
|
"$ZodPreprocess",
|
|
4145
4156
|
(inst, def) => {
|
|
4146
4157
|
$ZodPipe.init(inst, def);
|
|
4147
|
-
util.defineLazy(inst._zod, "optin", () => def.out._zod.optin);
|
|
4148
|
-
util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
|
|
4149
4158
|
}
|
|
4150
4159
|
);
|
|
4151
4160
|
|
package/src/v4/core/versions.ts
CHANGED
package/v4/classic/schemas.cjs
CHANGED
|
@@ -1203,10 +1203,12 @@ exports.ZodTransform = core.$constructor("ZodTransform", (inst, def) => {
|
|
|
1203
1203
|
if (output instanceof Promise) {
|
|
1204
1204
|
return output.then((output) => {
|
|
1205
1205
|
payload.value = output;
|
|
1206
|
+
payload.fallback = true;
|
|
1206
1207
|
return payload;
|
|
1207
1208
|
});
|
|
1208
1209
|
}
|
|
1209
1210
|
payload.value = output;
|
|
1211
|
+
payload.fallback = true;
|
|
1210
1212
|
return payload;
|
|
1211
1213
|
};
|
|
1212
1214
|
});
|
package/v4/classic/schemas.js
CHANGED
|
@@ -1085,10 +1085,12 @@ export const ZodTransform = /*@__PURE__*/ core.$constructor("ZodTransform", (ins
|
|
|
1085
1085
|
if (output instanceof Promise) {
|
|
1086
1086
|
return output.then((output) => {
|
|
1087
1087
|
payload.value = output;
|
|
1088
|
+
payload.fallback = true;
|
|
1088
1089
|
return payload;
|
|
1089
1090
|
});
|
|
1090
1091
|
}
|
|
1091
1092
|
payload.value = output;
|
|
1093
|
+
payload.fallback = true;
|
|
1092
1094
|
return payload;
|
|
1093
1095
|
};
|
|
1094
1096
|
});
|
package/v4/core/schemas.cjs
CHANGED
|
@@ -1748,6 +1748,7 @@ exports.$ZodFile = core.$constructor("$ZodFile", (inst, def) => {
|
|
|
1748
1748
|
});
|
|
1749
1749
|
exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
|
|
1750
1750
|
exports.$ZodType.init(inst, def);
|
|
1751
|
+
inst._zod.optin = "optional";
|
|
1751
1752
|
inst._zod.parse = (payload, ctx) => {
|
|
1752
1753
|
if (ctx.direction === "backward") {
|
|
1753
1754
|
throw new core.$ZodEncodeError(inst.constructor.name);
|
|
@@ -1757,6 +1758,7 @@ exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
|
|
|
1757
1758
|
const output = _out instanceof Promise ? _out : Promise.resolve(_out);
|
|
1758
1759
|
return output.then((output) => {
|
|
1759
1760
|
payload.value = output;
|
|
1761
|
+
payload.fallback = true;
|
|
1760
1762
|
return payload;
|
|
1761
1763
|
});
|
|
1762
1764
|
}
|
|
@@ -1764,11 +1766,12 @@ exports.$ZodTransform = core.$constructor("$ZodTransform", (inst, def) => {
|
|
|
1764
1766
|
throw new core.$ZodAsyncError();
|
|
1765
1767
|
}
|
|
1766
1768
|
payload.value = _out;
|
|
1769
|
+
payload.fallback = true;
|
|
1767
1770
|
return payload;
|
|
1768
1771
|
};
|
|
1769
1772
|
});
|
|
1770
1773
|
function handleOptionalResult(result, input) {
|
|
1771
|
-
if (result.issues.length
|
|
1774
|
+
if (input === undefined && (result.issues.length || result.fallback)) {
|
|
1772
1775
|
return { issues: [], value: undefined };
|
|
1773
1776
|
}
|
|
1774
1777
|
return result;
|
|
@@ -1786,10 +1789,11 @@ exports.$ZodOptional = core.$constructor("$ZodOptional", (inst, def) => {
|
|
|
1786
1789
|
});
|
|
1787
1790
|
inst._zod.parse = (payload, ctx) => {
|
|
1788
1791
|
if (def.innerType._zod.optin === "optional") {
|
|
1792
|
+
const input = payload.value;
|
|
1789
1793
|
const result = def.innerType._zod.run(payload, ctx);
|
|
1790
1794
|
if (result instanceof Promise)
|
|
1791
|
-
return result.then((r) => handleOptionalResult(r,
|
|
1792
|
-
return handleOptionalResult(result,
|
|
1795
|
+
return result.then((r) => handleOptionalResult(r, input));
|
|
1796
|
+
return handleOptionalResult(result, input);
|
|
1793
1797
|
}
|
|
1794
1798
|
if (payload.value === undefined) {
|
|
1795
1799
|
return payload;
|
|
@@ -1916,7 +1920,7 @@ exports.$ZodSuccess = core.$constructor("$ZodSuccess", (inst, def) => {
|
|
|
1916
1920
|
});
|
|
1917
1921
|
exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
|
|
1918
1922
|
exports.$ZodType.init(inst, def);
|
|
1919
|
-
|
|
1923
|
+
inst._zod.optin = "optional";
|
|
1920
1924
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
1921
1925
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
1922
1926
|
inst._zod.parse = (payload, ctx) => {
|
|
@@ -1937,6 +1941,7 @@ exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
|
|
|
1937
1941
|
input: payload.value,
|
|
1938
1942
|
});
|
|
1939
1943
|
payload.issues = [];
|
|
1944
|
+
payload.fallback = true;
|
|
1940
1945
|
}
|
|
1941
1946
|
return payload;
|
|
1942
1947
|
});
|
|
@@ -1951,6 +1956,7 @@ exports.$ZodCatch = core.$constructor("$ZodCatch", (inst, def) => {
|
|
|
1951
1956
|
input: payload.value,
|
|
1952
1957
|
});
|
|
1953
1958
|
payload.issues = [];
|
|
1959
|
+
payload.fallback = true;
|
|
1954
1960
|
}
|
|
1955
1961
|
return payload;
|
|
1956
1962
|
};
|
|
@@ -1997,7 +2003,7 @@ function handlePipeResult(left, next, ctx) {
|
|
|
1997
2003
|
left.aborted = true;
|
|
1998
2004
|
return left;
|
|
1999
2005
|
}
|
|
2000
|
-
return next._zod.run({ value: left.value, issues: left.issues }, ctx);
|
|
2006
|
+
return next._zod.run({ value: left.value, issues: left.issues, fallback: left.fallback }, ctx);
|
|
2001
2007
|
}
|
|
2002
2008
|
exports.$ZodCodec = core.$constructor("$ZodCodec", (inst, def) => {
|
|
2003
2009
|
exports.$ZodType.init(inst, def);
|
|
@@ -2055,8 +2061,6 @@ function handleCodecTxResult(left, value, nextSchema, ctx) {
|
|
|
2055
2061
|
}
|
|
2056
2062
|
exports.$ZodPreprocess = core.$constructor("$ZodPreprocess", (inst, def) => {
|
|
2057
2063
|
exports.$ZodPipe.init(inst, def);
|
|
2058
|
-
util.defineLazy(inst._zod, "optin", () => def.out._zod.optin);
|
|
2059
|
-
util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
|
|
2060
2064
|
});
|
|
2061
2065
|
exports.$ZodReadonly = core.$constructor("$ZodReadonly", (inst, def) => {
|
|
2062
2066
|
exports.$ZodType.init(inst, def);
|
package/v4/core/schemas.d.cts
CHANGED
|
@@ -25,6 +25,11 @@ export interface ParsePayload<T = unknown> {
|
|
|
25
25
|
issues: errors.$ZodRawIssue[];
|
|
26
26
|
/** A way to mark a whole payload as aborted. Used in codecs/pipes. */
|
|
27
27
|
aborted?: boolean;
|
|
28
|
+
/** @internal Marks a value as a fallback that an outer wrapper (e.g.
|
|
29
|
+
* $ZodOptional) may override with its own interpretation when input was
|
|
30
|
+
* undefined. Set by $ZodCatch when catchValue substitutes and by every
|
|
31
|
+
* $ZodTransform invocation. */
|
|
32
|
+
fallback?: boolean | undefined;
|
|
28
33
|
}
|
|
29
34
|
export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
|
|
30
35
|
export interface $ZodTypeDef {
|
package/v4/core/schemas.d.ts
CHANGED
|
@@ -25,6 +25,11 @@ export interface ParsePayload<T = unknown> {
|
|
|
25
25
|
issues: errors.$ZodRawIssue[];
|
|
26
26
|
/** A way to mark a whole payload as aborted. Used in codecs/pipes. */
|
|
27
27
|
aborted?: boolean;
|
|
28
|
+
/** @internal Marks a value as a fallback that an outer wrapper (e.g.
|
|
29
|
+
* $ZodOptional) may override with its own interpretation when input was
|
|
30
|
+
* undefined. Set by $ZodCatch when catchValue substitutes and by every
|
|
31
|
+
* $ZodTransform invocation. */
|
|
32
|
+
fallback?: boolean | undefined;
|
|
28
33
|
}
|
|
29
34
|
export type CheckFn<T> = (input: ParsePayload<T>) => util.MaybeAsync<void>;
|
|
30
35
|
export interface $ZodTypeDef {
|
package/v4/core/schemas.js
CHANGED
|
@@ -1717,6 +1717,7 @@ export const $ZodFile = /*@__PURE__*/ core.$constructor("$ZodFile", (inst, def)
|
|
|
1717
1717
|
});
|
|
1718
1718
|
export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (inst, def) => {
|
|
1719
1719
|
$ZodType.init(inst, def);
|
|
1720
|
+
inst._zod.optin = "optional";
|
|
1720
1721
|
inst._zod.parse = (payload, ctx) => {
|
|
1721
1722
|
if (ctx.direction === "backward") {
|
|
1722
1723
|
throw new core.$ZodEncodeError(inst.constructor.name);
|
|
@@ -1726,6 +1727,7 @@ export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (i
|
|
|
1726
1727
|
const output = _out instanceof Promise ? _out : Promise.resolve(_out);
|
|
1727
1728
|
return output.then((output) => {
|
|
1728
1729
|
payload.value = output;
|
|
1730
|
+
payload.fallback = true;
|
|
1729
1731
|
return payload;
|
|
1730
1732
|
});
|
|
1731
1733
|
}
|
|
@@ -1733,11 +1735,12 @@ export const $ZodTransform = /*@__PURE__*/ core.$constructor("$ZodTransform", (i
|
|
|
1733
1735
|
throw new core.$ZodAsyncError();
|
|
1734
1736
|
}
|
|
1735
1737
|
payload.value = _out;
|
|
1738
|
+
payload.fallback = true;
|
|
1736
1739
|
return payload;
|
|
1737
1740
|
};
|
|
1738
1741
|
});
|
|
1739
1742
|
function handleOptionalResult(result, input) {
|
|
1740
|
-
if (result.issues.length
|
|
1743
|
+
if (input === undefined && (result.issues.length || result.fallback)) {
|
|
1741
1744
|
return { issues: [], value: undefined };
|
|
1742
1745
|
}
|
|
1743
1746
|
return result;
|
|
@@ -1755,10 +1758,11 @@ export const $ZodOptional = /*@__PURE__*/ core.$constructor("$ZodOptional", (ins
|
|
|
1755
1758
|
});
|
|
1756
1759
|
inst._zod.parse = (payload, ctx) => {
|
|
1757
1760
|
if (def.innerType._zod.optin === "optional") {
|
|
1761
|
+
const input = payload.value;
|
|
1758
1762
|
const result = def.innerType._zod.run(payload, ctx);
|
|
1759
1763
|
if (result instanceof Promise)
|
|
1760
|
-
return result.then((r) => handleOptionalResult(r,
|
|
1761
|
-
return handleOptionalResult(result,
|
|
1764
|
+
return result.then((r) => handleOptionalResult(r, input));
|
|
1765
|
+
return handleOptionalResult(result, input);
|
|
1762
1766
|
}
|
|
1763
1767
|
if (payload.value === undefined) {
|
|
1764
1768
|
return payload;
|
|
@@ -1885,7 +1889,7 @@ export const $ZodSuccess = /*@__PURE__*/ core.$constructor("$ZodSuccess", (inst,
|
|
|
1885
1889
|
});
|
|
1886
1890
|
export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def) => {
|
|
1887
1891
|
$ZodType.init(inst, def);
|
|
1888
|
-
|
|
1892
|
+
inst._zod.optin = "optional";
|
|
1889
1893
|
util.defineLazy(inst._zod, "optout", () => def.innerType._zod.optout);
|
|
1890
1894
|
util.defineLazy(inst._zod, "values", () => def.innerType._zod.values);
|
|
1891
1895
|
inst._zod.parse = (payload, ctx) => {
|
|
@@ -1906,6 +1910,7 @@ export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def
|
|
|
1906
1910
|
input: payload.value,
|
|
1907
1911
|
});
|
|
1908
1912
|
payload.issues = [];
|
|
1913
|
+
payload.fallback = true;
|
|
1909
1914
|
}
|
|
1910
1915
|
return payload;
|
|
1911
1916
|
});
|
|
@@ -1920,6 +1925,7 @@ export const $ZodCatch = /*@__PURE__*/ core.$constructor("$ZodCatch", (inst, def
|
|
|
1920
1925
|
input: payload.value,
|
|
1921
1926
|
});
|
|
1922
1927
|
payload.issues = [];
|
|
1928
|
+
payload.fallback = true;
|
|
1923
1929
|
}
|
|
1924
1930
|
return payload;
|
|
1925
1931
|
};
|
|
@@ -1966,7 +1972,7 @@ function handlePipeResult(left, next, ctx) {
|
|
|
1966
1972
|
left.aborted = true;
|
|
1967
1973
|
return left;
|
|
1968
1974
|
}
|
|
1969
|
-
return next._zod.run({ value: left.value, issues: left.issues }, ctx);
|
|
1975
|
+
return next._zod.run({ value: left.value, issues: left.issues, fallback: left.fallback }, ctx);
|
|
1970
1976
|
}
|
|
1971
1977
|
export const $ZodCodec = /*@__PURE__*/ core.$constructor("$ZodCodec", (inst, def) => {
|
|
1972
1978
|
$ZodType.init(inst, def);
|
|
@@ -2024,8 +2030,6 @@ function handleCodecTxResult(left, value, nextSchema, ctx) {
|
|
|
2024
2030
|
}
|
|
2025
2031
|
export const $ZodPreprocess = /*@__PURE__*/ core.$constructor("$ZodPreprocess", (inst, def) => {
|
|
2026
2032
|
$ZodPipe.init(inst, def);
|
|
2027
|
-
util.defineLazy(inst._zod, "optin", () => def.out._zod.optin);
|
|
2028
|
-
util.defineLazy(inst._zod, "optout", () => def.out._zod.optout);
|
|
2029
2033
|
});
|
|
2030
2034
|
export const $ZodReadonly = /*@__PURE__*/ core.$constructor("$ZodReadonly", (inst, def) => {
|
|
2031
2035
|
$ZodType.init(inst, def);
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED