zod 3.25.74 → 3.25.76
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 +4 -7
- package/src/v4/classic/tests/recursive-types.test.ts +30 -0
- package/src/v4/classic/tests/refine.test.ts +109 -0
- package/src/v4/classic/tests/registries.test.ts +9 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +49 -17
- package/src/v4/core/api.ts +2 -0
- package/src/v4/core/checks.ts +8 -10
- package/src/v4/core/errors.ts +4 -0
- package/src/v4/core/registries.ts +11 -1
- package/src/v4/core/schemas.ts +18 -4
- package/src/v4/core/to-json-schema.ts +52 -26
- package/v4/classic/schemas.cjs +4 -4
- package/v4/classic/schemas.d.cts +2 -2
- package/v4/classic/schemas.d.ts +2 -2
- package/v4/classic/schemas.js +4 -4
- package/v4/core/checks.cjs +18 -12
- package/v4/core/checks.d.cts +2 -1
- package/v4/core/checks.d.ts +2 -1
- package/v4/core/checks.js +18 -12
- package/v4/core/errors.cjs +4 -0
- package/v4/core/errors.js +4 -0
- package/v4/core/registries.cjs +10 -1
- package/v4/core/registries.d.cts +2 -1
- package/v4/core/registries.d.ts +2 -1
- package/v4/core/registries.js +10 -1
- package/v4/core/schemas.cjs +2 -2
- package/v4/core/schemas.d.cts +14 -0
- package/v4/core/schemas.d.ts +14 -0
- package/v4/core/schemas.js +2 -2
- package/v4/core/to-json-schema.cjs +49 -25
- package/v4/core/to-json-schema.d.cts +1 -1
- package/v4/core/to-json-schema.d.ts +1 -1
- package/v4/core/to-json-schema.js +49 -25
package/package.json
CHANGED
|
@@ -1937,10 +1937,10 @@ export const ZodCustom: core.$constructor<ZodCustom> = /*@__PURE__*/ core.$const
|
|
|
1937
1937
|
});
|
|
1938
1938
|
|
|
1939
1939
|
// custom checks
|
|
1940
|
-
export function check<O = unknown>(fn: core.CheckFn<O
|
|
1940
|
+
export function check<O = unknown>(fn: core.CheckFn<O>): core.$ZodCheck<O> {
|
|
1941
1941
|
const ch = new core.$ZodCheck({
|
|
1942
1942
|
check: "custom",
|
|
1943
|
-
...util.normalizeParams(params),
|
|
1943
|
+
// ...util.normalizeParams(params),
|
|
1944
1944
|
});
|
|
1945
1945
|
|
|
1946
1946
|
ch._zod.check = fn;
|
|
@@ -1962,10 +1962,7 @@ export function refine<T>(
|
|
|
1962
1962
|
}
|
|
1963
1963
|
|
|
1964
1964
|
// superRefine
|
|
1965
|
-
export function superRefine<T>(
|
|
1966
|
-
fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>,
|
|
1967
|
-
params?: string | core.$ZodCustomParams
|
|
1968
|
-
): core.$ZodCheck<T> {
|
|
1965
|
+
export function superRefine<T>(fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>): core.$ZodCheck<T> {
|
|
1969
1966
|
const ch = check<T>((payload) => {
|
|
1970
1967
|
(payload as RefinementCtx).addIssue = (issue) => {
|
|
1971
1968
|
if (typeof issue === "string") {
|
|
@@ -1983,7 +1980,7 @@ export function superRefine<T>(
|
|
|
1983
1980
|
};
|
|
1984
1981
|
|
|
1985
1982
|
return fn(payload.value, payload as RefinementCtx<T>);
|
|
1986
|
-
}
|
|
1983
|
+
});
|
|
1987
1984
|
return ch;
|
|
1988
1985
|
}
|
|
1989
1986
|
|
|
@@ -324,3 +324,33 @@ test("recursion compatibility", () => {
|
|
|
324
324
|
},
|
|
325
325
|
});
|
|
326
326
|
});
|
|
327
|
+
|
|
328
|
+
// biome-ignore lint: sadf
|
|
329
|
+
export type RecursiveA = z.ZodUnion<
|
|
330
|
+
[
|
|
331
|
+
z.ZodObject<{
|
|
332
|
+
a: z.ZodDefault<RecursiveA>;
|
|
333
|
+
b: z.ZodPrefault<RecursiveA>;
|
|
334
|
+
c: z.ZodNonOptional<RecursiveA>;
|
|
335
|
+
d: z.ZodOptional<RecursiveA>;
|
|
336
|
+
e: z.ZodNullable<RecursiveA>;
|
|
337
|
+
g: z.ZodReadonly<RecursiveA>;
|
|
338
|
+
h: z.ZodPipe<RecursiveA, z.ZodString>;
|
|
339
|
+
i: z.ZodArray<RecursiveA>;
|
|
340
|
+
j: z.ZodSet<RecursiveA>;
|
|
341
|
+
k: z.ZodMap<RecursiveA, RecursiveA>;
|
|
342
|
+
l: z.ZodRecord<z.ZodString, RecursiveA>;
|
|
343
|
+
m: z.ZodUnion<[RecursiveA, RecursiveA]>;
|
|
344
|
+
n: z.ZodIntersection<RecursiveA, RecursiveA>;
|
|
345
|
+
o: z.ZodLazy<RecursiveA>;
|
|
346
|
+
p: z.ZodPromise<RecursiveA>;
|
|
347
|
+
q: z.ZodCatch<RecursiveA>;
|
|
348
|
+
r: z.ZodSuccess<RecursiveA>;
|
|
349
|
+
s: z.ZodTransform<RecursiveA, string>;
|
|
350
|
+
t: z.ZodTuple<[RecursiveA, RecursiveA]>;
|
|
351
|
+
u: z.ZodObject<{
|
|
352
|
+
a: RecursiveA;
|
|
353
|
+
}>;
|
|
354
|
+
}>,
|
|
355
|
+
]
|
|
356
|
+
>;
|
|
@@ -421,3 +421,112 @@ describe("type refinement", () => {
|
|
|
421
421
|
});
|
|
422
422
|
});
|
|
423
423
|
*/
|
|
424
|
+
|
|
425
|
+
test("when", () => {
|
|
426
|
+
const schema = z
|
|
427
|
+
.strictObject({
|
|
428
|
+
password: z.string().min(8),
|
|
429
|
+
confirmPassword: z.string(),
|
|
430
|
+
other: z.string(),
|
|
431
|
+
})
|
|
432
|
+
.refine(
|
|
433
|
+
(data) => {
|
|
434
|
+
console.log("running check...");
|
|
435
|
+
console.log(data);
|
|
436
|
+
console.log(data.password);
|
|
437
|
+
return data.password === data.confirmPassword;
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
message: "Passwords do not match",
|
|
441
|
+
path: ["confirmPassword"],
|
|
442
|
+
when(payload) {
|
|
443
|
+
if (payload.value === undefined) return false;
|
|
444
|
+
if (payload.value === null) return false;
|
|
445
|
+
// no issues with confirmPassword or password
|
|
446
|
+
return payload.issues.every((iss) => iss.path?.[0] !== "confirmPassword" && iss.path?.[0] !== "password");
|
|
447
|
+
},
|
|
448
|
+
}
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
expect(schema.safeParse(undefined)).toMatchInlineSnapshot(`
|
|
452
|
+
{
|
|
453
|
+
"error": [ZodError: [
|
|
454
|
+
{
|
|
455
|
+
"expected": "object",
|
|
456
|
+
"code": "invalid_type",
|
|
457
|
+
"path": [],
|
|
458
|
+
"message": "Invalid input: expected object, received undefined"
|
|
459
|
+
}
|
|
460
|
+
]],
|
|
461
|
+
"success": false,
|
|
462
|
+
}
|
|
463
|
+
`);
|
|
464
|
+
expect(schema.safeParse(null)).toMatchInlineSnapshot(`
|
|
465
|
+
{
|
|
466
|
+
"error": [ZodError: [
|
|
467
|
+
{
|
|
468
|
+
"expected": "object",
|
|
469
|
+
"code": "invalid_type",
|
|
470
|
+
"path": [],
|
|
471
|
+
"message": "Invalid input: expected object, received null"
|
|
472
|
+
}
|
|
473
|
+
]],
|
|
474
|
+
"success": false,
|
|
475
|
+
}
|
|
476
|
+
`);
|
|
477
|
+
expect(
|
|
478
|
+
schema.safeParse({
|
|
479
|
+
password: "asdf",
|
|
480
|
+
confirmPassword: "asdfg",
|
|
481
|
+
other: "qwer",
|
|
482
|
+
})
|
|
483
|
+
).toMatchInlineSnapshot(`
|
|
484
|
+
{
|
|
485
|
+
"error": [ZodError: [
|
|
486
|
+
{
|
|
487
|
+
"origin": "string",
|
|
488
|
+
"code": "too_small",
|
|
489
|
+
"minimum": 8,
|
|
490
|
+
"inclusive": true,
|
|
491
|
+
"path": [
|
|
492
|
+
"password"
|
|
493
|
+
],
|
|
494
|
+
"message": "Too small: expected string to have >=8 characters"
|
|
495
|
+
}
|
|
496
|
+
]],
|
|
497
|
+
"success": false,
|
|
498
|
+
}
|
|
499
|
+
`);
|
|
500
|
+
|
|
501
|
+
expect(
|
|
502
|
+
schema.safeParse({
|
|
503
|
+
password: "asdf",
|
|
504
|
+
confirmPassword: "asdfg",
|
|
505
|
+
other: 1234,
|
|
506
|
+
})
|
|
507
|
+
).toMatchInlineSnapshot(`
|
|
508
|
+
{
|
|
509
|
+
"error": [ZodError: [
|
|
510
|
+
{
|
|
511
|
+
"origin": "string",
|
|
512
|
+
"code": "too_small",
|
|
513
|
+
"minimum": 8,
|
|
514
|
+
"inclusive": true,
|
|
515
|
+
"path": [
|
|
516
|
+
"password"
|
|
517
|
+
],
|
|
518
|
+
"message": "Too small: expected string to have >=8 characters"
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
"expected": "string",
|
|
522
|
+
"code": "invalid_type",
|
|
523
|
+
"path": [
|
|
524
|
+
"other"
|
|
525
|
+
],
|
|
526
|
+
"message": "Invalid input: expected string, received number"
|
|
527
|
+
}
|
|
528
|
+
]],
|
|
529
|
+
"success": false,
|
|
530
|
+
}
|
|
531
|
+
`);
|
|
532
|
+
});
|
|
@@ -193,3 +193,12 @@ test("function meta with replacement", () => {
|
|
|
193
193
|
|
|
194
194
|
expect(myReg.get(mySchema)!.defaulter("hello", true)).toEqual(5);
|
|
195
195
|
});
|
|
196
|
+
|
|
197
|
+
test("test .clear()", () => {
|
|
198
|
+
const reg = z.registry();
|
|
199
|
+
const a = z.string();
|
|
200
|
+
reg.add(a);
|
|
201
|
+
expect(reg.has(a)).toEqual(true);
|
|
202
|
+
reg.clear();
|
|
203
|
+
expect(reg.has(a)).toEqual(false);
|
|
204
|
+
});
|
|
@@ -28,10 +28,9 @@ describe("toJSONSchema", () => {
|
|
|
28
28
|
"type": "null",
|
|
29
29
|
}
|
|
30
30
|
`);
|
|
31
|
-
expect(z.toJSONSchema(z.undefined())).toMatchInlineSnapshot(`
|
|
31
|
+
expect(z.toJSONSchema(z.undefined(), { unrepresentable: "any" })).toMatchInlineSnapshot(`
|
|
32
32
|
{
|
|
33
33
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
34
|
-
"not": {},
|
|
35
34
|
}
|
|
36
35
|
`);
|
|
37
36
|
expect(z.toJSONSchema(z.any())).toMatchInlineSnapshot(`
|
|
@@ -232,6 +231,7 @@ describe("toJSONSchema", () => {
|
|
|
232
231
|
expect(() => z.toJSONSchema(z.int64())).toThrow("BigInt cannot be represented in JSON Schema");
|
|
233
232
|
expect(() => z.toJSONSchema(z.symbol())).toThrow("Symbols cannot be represented in JSON Schema");
|
|
234
233
|
expect(() => z.toJSONSchema(z.void())).toThrow("Void cannot be represented in JSON Schema");
|
|
234
|
+
expect(() => z.toJSONSchema(z.undefined())).toThrow("Undefined cannot be represented in JSON Schema");
|
|
235
235
|
expect(() => z.toJSONSchema(z.date())).toThrow("Date cannot be represented in JSON Schema");
|
|
236
236
|
expect(() => z.toJSONSchema(z.map(z.string(), z.number()))).toThrow("Map cannot be represented in JSON Schema");
|
|
237
237
|
expect(() => z.toJSONSchema(z.set(z.string()))).toThrow("Set cannot be represented in JSON Schema");
|
|
@@ -1746,16 +1746,17 @@ test("basic registry", () => {
|
|
|
1746
1746
|
myRegistry.add(User, { id: "User" });
|
|
1747
1747
|
myRegistry.add(Post, { id: "Post" });
|
|
1748
1748
|
|
|
1749
|
-
const result = z.z.toJSONSchema(myRegistry);
|
|
1749
|
+
const result = z.z.toJSONSchema(myRegistry, { uri: (id) => `https://example.com/${id}.json` });
|
|
1750
1750
|
expect(result).toMatchInlineSnapshot(`
|
|
1751
1751
|
{
|
|
1752
1752
|
"schemas": {
|
|
1753
1753
|
"Post": {
|
|
1754
|
+
"$id": "https://example.com/Post.json",
|
|
1754
1755
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
1755
1756
|
"additionalProperties": false,
|
|
1756
1757
|
"properties": {
|
|
1757
1758
|
"author": {
|
|
1758
|
-
"$ref": "User",
|
|
1759
|
+
"$ref": "https://example.com/User.json",
|
|
1759
1760
|
},
|
|
1760
1761
|
"content": {
|
|
1761
1762
|
"type": "string",
|
|
@@ -1772,6 +1773,7 @@ test("basic registry", () => {
|
|
|
1772
1773
|
"type": "object",
|
|
1773
1774
|
},
|
|
1774
1775
|
"User": {
|
|
1776
|
+
"$id": "https://example.com/User.json",
|
|
1775
1777
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
1776
1778
|
"additionalProperties": false,
|
|
1777
1779
|
"properties": {
|
|
@@ -1780,7 +1782,7 @@ test("basic registry", () => {
|
|
|
1780
1782
|
},
|
|
1781
1783
|
"posts": {
|
|
1782
1784
|
"items": {
|
|
1783
|
-
"$ref": "Post",
|
|
1785
|
+
"$ref": "https://example.com/Post.json",
|
|
1784
1786
|
},
|
|
1785
1787
|
"type": "array",
|
|
1786
1788
|
},
|
|
@@ -1887,9 +1889,8 @@ test("input type", () => {
|
|
|
1887
1889
|
e: z.string().prefault("hello"),
|
|
1888
1890
|
f: z.string().catch("hello"),
|
|
1889
1891
|
g: z.never(),
|
|
1890
|
-
h: z.
|
|
1891
|
-
i: z.union([z.string(), z.
|
|
1892
|
-
j: z.union([z.string(), z.string().optional()]),
|
|
1892
|
+
h: z.union([z.string(), z.number().default(2)]),
|
|
1893
|
+
i: z.union([z.string(), z.string().optional()]),
|
|
1893
1894
|
});
|
|
1894
1895
|
expect(z.toJSONSchema(schema, { io: "input" })).toMatchInlineSnapshot(`
|
|
1895
1896
|
{
|
|
@@ -1927,9 +1928,6 @@ test("input type", () => {
|
|
|
1927
1928
|
"not": {},
|
|
1928
1929
|
},
|
|
1929
1930
|
"h": {
|
|
1930
|
-
"not": {},
|
|
1931
|
-
},
|
|
1932
|
-
"i": {
|
|
1933
1931
|
"anyOf": [
|
|
1934
1932
|
{
|
|
1935
1933
|
"type": "string",
|
|
@@ -1940,7 +1938,7 @@ test("input type", () => {
|
|
|
1940
1938
|
},
|
|
1941
1939
|
],
|
|
1942
1940
|
},
|
|
1943
|
-
"
|
|
1941
|
+
"i": {
|
|
1944
1942
|
"anyOf": [
|
|
1945
1943
|
{
|
|
1946
1944
|
"type": "string",
|
|
@@ -1995,9 +1993,6 @@ test("input type", () => {
|
|
|
1995
1993
|
"not": {},
|
|
1996
1994
|
},
|
|
1997
1995
|
"h": {
|
|
1998
|
-
"not": {},
|
|
1999
|
-
},
|
|
2000
|
-
"i": {
|
|
2001
1996
|
"anyOf": [
|
|
2002
1997
|
{
|
|
2003
1998
|
"type": "string",
|
|
@@ -2008,7 +2003,7 @@ test("input type", () => {
|
|
|
2008
2003
|
},
|
|
2009
2004
|
],
|
|
2010
2005
|
},
|
|
2011
|
-
"
|
|
2006
|
+
"i": {
|
|
2012
2007
|
"anyOf": [
|
|
2013
2008
|
{
|
|
2014
2009
|
"type": "string",
|
|
@@ -2026,7 +2021,7 @@ test("input type", () => {
|
|
|
2026
2021
|
"e",
|
|
2027
2022
|
"f",
|
|
2028
2023
|
"g",
|
|
2029
|
-
"
|
|
2024
|
+
"h",
|
|
2030
2025
|
],
|
|
2031
2026
|
"type": "object",
|
|
2032
2027
|
}
|
|
@@ -2280,3 +2275,40 @@ test("custom toJSONSchema", () => {
|
|
|
2280
2275
|
}
|
|
2281
2276
|
`);
|
|
2282
2277
|
});
|
|
2278
|
+
|
|
2279
|
+
test("cycle detection - root", () => {
|
|
2280
|
+
const schema = z.object({
|
|
2281
|
+
name: z.string(),
|
|
2282
|
+
get subcategories() {
|
|
2283
|
+
return z.array(schema);
|
|
2284
|
+
},
|
|
2285
|
+
});
|
|
2286
|
+
|
|
2287
|
+
expect(() => z.toJSONSchema(schema, { cycles: "throw" })).toThrowErrorMatchingInlineSnapshot(`
|
|
2288
|
+
[Error: Cycle detected: #/properties/subcategories/items/<root>
|
|
2289
|
+
|
|
2290
|
+
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.]
|
|
2291
|
+
`);
|
|
2292
|
+
});
|
|
2293
|
+
|
|
2294
|
+
test("cycle detection - mutual recursion", () => {
|
|
2295
|
+
const A = z.object({
|
|
2296
|
+
name: z.string(),
|
|
2297
|
+
get subcategories() {
|
|
2298
|
+
return z.array(B);
|
|
2299
|
+
},
|
|
2300
|
+
});
|
|
2301
|
+
|
|
2302
|
+
const B = z.object({
|
|
2303
|
+
name: z.string(),
|
|
2304
|
+
get subcategories() {
|
|
2305
|
+
return z.array(A);
|
|
2306
|
+
},
|
|
2307
|
+
});
|
|
2308
|
+
|
|
2309
|
+
expect(() => z.toJSONSchema(A, { cycles: "throw" })).toThrowErrorMatchingInlineSnapshot(`
|
|
2310
|
+
[Error: Cycle detected: #/properties/subcategories/items/properties/subcategories/items/<root>
|
|
2311
|
+
|
|
2312
|
+
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.]
|
|
2313
|
+
`);
|
|
2314
|
+
});
|
package/src/v4/core/api.ts
CHANGED
|
@@ -779,6 +779,8 @@ export function _nan<T extends schemas.$ZodNaN>(Class: util.SchemaClass<T>, para
|
|
|
779
779
|
});
|
|
780
780
|
}
|
|
781
781
|
|
|
782
|
+
// export type $ZodCheckParams = CheckParams<checks.$ZodCheck, "abort">;
|
|
783
|
+
|
|
782
784
|
export type $ZodCheckLessThanParams = CheckParams<checks.$ZodCheckLessThan, "inclusive" | "value">;
|
|
783
785
|
export function _lt(
|
|
784
786
|
value: util.Numeric,
|
package/src/v4/core/checks.ts
CHANGED
|
@@ -13,18 +13,16 @@ export interface $ZodCheckDef {
|
|
|
13
13
|
error?: errors.$ZodErrorMap<never> | undefined;
|
|
14
14
|
/** If true, no later checks will be executed if this check fails. Default `false`. */
|
|
15
15
|
abort?: boolean | undefined;
|
|
16
|
+
/** If provided, this check will only be executed if the function returns `true`. Defaults to `payload => z.util.isAborted(payload)`. */
|
|
17
|
+
when?: ((payload: schemas.ParsePayload) => boolean) | undefined;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
export interface $ZodCheckInternals<T> {
|
|
19
21
|
def: $ZodCheckDef;
|
|
20
22
|
/** The set of issues this check might throw. */
|
|
21
23
|
issc?: errors.$ZodIssueBase;
|
|
22
|
-
// "_check"(input: $ZodResult<T>): util.MaybeAsync<void>;
|
|
23
24
|
check(payload: schemas.ParsePayload<T>): util.MaybeAsync<void>;
|
|
24
|
-
// _parseB(payload: ParsePayload<any>, ctx: ParseContext): util.MaybeAsync<ParsePayload>;
|
|
25
25
|
onattach: ((schema: schemas.$ZodType) => void)[];
|
|
26
|
-
// "_async": boolean;
|
|
27
|
-
when?: ((payload: schemas.ParsePayload) => boolean) | undefined;
|
|
28
26
|
}
|
|
29
27
|
|
|
30
28
|
export interface $ZodCheck<in T = never> {
|
|
@@ -452,7 +450,7 @@ export const $ZodCheckMaxSize: core.$constructor<$ZodCheckMaxSize> = /*@__PURE__
|
|
|
452
450
|
(inst, def) => {
|
|
453
451
|
$ZodCheck.init(inst, def);
|
|
454
452
|
|
|
455
|
-
inst._zod.when
|
|
453
|
+
inst._zod.def.when ??= (payload) => {
|
|
456
454
|
const val = payload.value;
|
|
457
455
|
return !util.nullish(val) && (val as any).size !== undefined;
|
|
458
456
|
};
|
|
@@ -501,7 +499,7 @@ export const $ZodCheckMinSize: core.$constructor<$ZodCheckMinSize> = /*@__PURE__
|
|
|
501
499
|
(inst, def) => {
|
|
502
500
|
$ZodCheck.init(inst, def);
|
|
503
501
|
|
|
504
|
-
inst._zod.when
|
|
502
|
+
inst._zod.def.when ??= (payload) => {
|
|
505
503
|
const val = payload.value;
|
|
506
504
|
return !util.nullish(val) && (val as any).size !== undefined;
|
|
507
505
|
};
|
|
@@ -550,7 +548,7 @@ export const $ZodCheckSizeEquals: core.$constructor<$ZodCheckSizeEquals> = /*@__
|
|
|
550
548
|
(inst, def) => {
|
|
551
549
|
$ZodCheck.init(inst, def);
|
|
552
550
|
|
|
553
|
-
inst._zod.when
|
|
551
|
+
inst._zod.def.when ??= (payload) => {
|
|
554
552
|
const val = payload.value;
|
|
555
553
|
return !util.nullish(val) && (val as any).size !== undefined;
|
|
556
554
|
};
|
|
@@ -604,7 +602,7 @@ export const $ZodCheckMaxLength: core.$constructor<$ZodCheckMaxLength> = /*@__PU
|
|
|
604
602
|
(inst, def) => {
|
|
605
603
|
$ZodCheck.init(inst, def);
|
|
606
604
|
|
|
607
|
-
inst._zod.when
|
|
605
|
+
inst._zod.def.when ??= (payload) => {
|
|
608
606
|
const val = payload.value;
|
|
609
607
|
return !util.nullish(val) && (val as any).length !== undefined;
|
|
610
608
|
};
|
|
@@ -655,7 +653,7 @@ export const $ZodCheckMinLength: core.$constructor<$ZodCheckMinLength> = /*@__PU
|
|
|
655
653
|
(inst, def) => {
|
|
656
654
|
$ZodCheck.init(inst, def);
|
|
657
655
|
|
|
658
|
-
inst._zod.when
|
|
656
|
+
inst._zod.def.when ??= (payload) => {
|
|
659
657
|
const val = payload.value;
|
|
660
658
|
return !util.nullish(val) && (val as any).length !== undefined;
|
|
661
659
|
};
|
|
@@ -707,7 +705,7 @@ export const $ZodCheckLengthEquals: core.$constructor<$ZodCheckLengthEquals> = /
|
|
|
707
705
|
(inst, def) => {
|
|
708
706
|
$ZodCheck.init(inst, def);
|
|
709
707
|
|
|
710
|
-
inst._zod.when
|
|
708
|
+
inst._zod.def.when ??= (payload) => {
|
|
711
709
|
const val = payload.value;
|
|
712
710
|
return !util.nullish(val) && (val as any).length !== undefined;
|
|
713
711
|
};
|
package/src/v4/core/errors.ts
CHANGED
|
@@ -203,6 +203,10 @@ const initializer = (inst: $ZodError, def: $ZodIssue[]): void => {
|
|
|
203
203
|
enumerable: true,
|
|
204
204
|
// configurable: false,
|
|
205
205
|
});
|
|
206
|
+
Object.defineProperty(inst, "toString", {
|
|
207
|
+
value: () => inst.message,
|
|
208
|
+
enumerable: false,
|
|
209
|
+
});
|
|
206
210
|
};
|
|
207
211
|
|
|
208
212
|
export const $ZodError: $constructor<$ZodError> = $constructor("$ZodError", initializer);
|
|
@@ -27,7 +27,7 @@ type MetadataType = Record<string, unknown> | undefined;
|
|
|
27
27
|
export class $ZodRegistry<Meta extends MetadataType = MetadataType, Schema extends $ZodType = $ZodType> {
|
|
28
28
|
_meta!: Meta;
|
|
29
29
|
_schema!: Schema;
|
|
30
|
-
_map:
|
|
30
|
+
_map: Map<Schema, $replace<Meta, Schema>> = new Map();
|
|
31
31
|
_idmap: Map<string, Schema> = new Map();
|
|
32
32
|
|
|
33
33
|
add<S extends Schema>(
|
|
@@ -45,7 +45,17 @@ export class $ZodRegistry<Meta extends MetadataType = MetadataType, Schema exten
|
|
|
45
45
|
return this as any;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
clear(): this {
|
|
49
|
+
this._map = new Map();
|
|
50
|
+
this._idmap = new Map();
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
|
|
48
54
|
remove(schema: Schema): this {
|
|
55
|
+
const meta: any = this._map.get(schema);
|
|
56
|
+
if (meta && typeof meta === "object" && "id" in meta) {
|
|
57
|
+
this._idmap.delete(meta.id!);
|
|
58
|
+
}
|
|
49
59
|
this._map.delete(schema);
|
|
50
60
|
return this;
|
|
51
61
|
}
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -204,18 +204,18 @@ export const $ZodType: core.$constructor<$ZodType> = /*@__PURE__*/ core.$constru
|
|
|
204
204
|
ctx?: ParseContextInternal | undefined
|
|
205
205
|
): util.MaybeAsync<ParsePayload> => {
|
|
206
206
|
let isAborted = util.aborted(payload);
|
|
207
|
+
|
|
207
208
|
let asyncResult!: Promise<unknown> | undefined;
|
|
208
209
|
for (const ch of checks) {
|
|
209
|
-
if (ch._zod.when) {
|
|
210
|
-
const shouldRun = ch._zod.when(payload);
|
|
211
|
-
|
|
210
|
+
if (ch._zod.def.when) {
|
|
211
|
+
const shouldRun = ch._zod.def.when(payload);
|
|
212
212
|
if (!shouldRun) continue;
|
|
213
213
|
} else if (isAborted) {
|
|
214
214
|
continue;
|
|
215
215
|
}
|
|
216
|
-
|
|
217
216
|
const currLen = payload.issues.length;
|
|
218
217
|
const _ = ch._zod.check(payload as any) as any as ParsePayload;
|
|
218
|
+
|
|
219
219
|
if (_ instanceof Promise && ctx?.async === false) {
|
|
220
220
|
throw new core.$ZodAsyncError();
|
|
221
221
|
}
|
|
@@ -2106,6 +2106,8 @@ export interface $ZodIntersectionInternals<A extends SomeType = $ZodType, B exte
|
|
|
2106
2106
|
extends $ZodTypeInternals<core.output<A> & core.output<B>, core.input<A> & core.input<B>> {
|
|
2107
2107
|
def: $ZodIntersectionDef<A, B>;
|
|
2108
2108
|
isst: never;
|
|
2109
|
+
optin: A["_zod"]["optin"] | B["_zod"]["optin"];
|
|
2110
|
+
optout: A["_zod"]["optout"] | B["_zod"]["optout"];
|
|
2109
2111
|
}
|
|
2110
2112
|
|
|
2111
2113
|
export interface $ZodIntersection<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -2418,6 +2420,8 @@ export interface $ZodRecordInternals<Key extends $ZodRecordKey = $ZodRecordKey,
|
|
|
2418
2420
|
extends $ZodTypeInternals<$InferZodRecordOutput<Key, Value>, $InferZodRecordInput<Key, Value>> {
|
|
2419
2421
|
def: $ZodRecordDef<Key, Value>;
|
|
2420
2422
|
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey<Record<PropertyKey, unknown>>;
|
|
2423
|
+
optin?: "optional" | undefined;
|
|
2424
|
+
optout?: "optional" | undefined;
|
|
2421
2425
|
}
|
|
2422
2426
|
|
|
2423
2427
|
export type $partial = { "~~partial": true };
|
|
@@ -2551,6 +2555,8 @@ export interface $ZodMapInternals<Key extends SomeType = $ZodType, Value extends
|
|
|
2551
2555
|
extends $ZodTypeInternals<Map<core.output<Key>, core.output<Value>>, Map<core.input<Key>, core.input<Value>>> {
|
|
2552
2556
|
def: $ZodMapDef<Key, Value>;
|
|
2553
2557
|
isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey | errors.$ZodIssueInvalidElement<unknown>;
|
|
2558
|
+
optin?: "optional" | undefined;
|
|
2559
|
+
optout?: "optional" | undefined;
|
|
2554
2560
|
}
|
|
2555
2561
|
|
|
2556
2562
|
export interface $ZodMap<Key extends SomeType = $ZodType, Value extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -2650,6 +2656,8 @@ export interface $ZodSetInternals<T extends SomeType = $ZodType>
|
|
|
2650
2656
|
extends $ZodTypeInternals<Set<core.output<T>>, Set<core.input<T>>> {
|
|
2651
2657
|
def: $ZodSetDef<T>;
|
|
2652
2658
|
isst: errors.$ZodIssueInvalidType;
|
|
2659
|
+
optin?: "optional" | undefined;
|
|
2660
|
+
optout?: "optional" | undefined;
|
|
2653
2661
|
}
|
|
2654
2662
|
|
|
2655
2663
|
export interface $ZodSet<T extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -3060,6 +3068,7 @@ export interface $ZodDefaultInternals<T extends SomeType = $ZodType>
|
|
|
3060
3068
|
extends $ZodTypeInternals<util.NoUndefined<core.output<T>>, core.input<T> | undefined> {
|
|
3061
3069
|
def: $ZodDefaultDef<T>;
|
|
3062
3070
|
optin: "optional";
|
|
3071
|
+
optout?: "optional" | undefined; // required
|
|
3063
3072
|
isst: never;
|
|
3064
3073
|
values: T["_zod"]["values"];
|
|
3065
3074
|
}
|
|
@@ -3120,6 +3129,7 @@ export interface $ZodPrefaultInternals<T extends SomeType = $ZodType>
|
|
|
3120
3129
|
extends $ZodTypeInternals<util.NoUndefined<core.output<T>>, core.input<T> | undefined> {
|
|
3121
3130
|
def: $ZodPrefaultDef<T>;
|
|
3122
3131
|
optin: "optional";
|
|
3132
|
+
optout?: "optional" | undefined;
|
|
3123
3133
|
isst: never;
|
|
3124
3134
|
values: T["_zod"]["values"];
|
|
3125
3135
|
}
|
|
@@ -3162,6 +3172,8 @@ export interface $ZodNonOptionalInternals<T extends SomeType = $ZodType>
|
|
|
3162
3172
|
def: $ZodNonOptionalDef<T>;
|
|
3163
3173
|
isst: errors.$ZodIssueInvalidType;
|
|
3164
3174
|
values: T["_zod"]["values"];
|
|
3175
|
+
optin: "optional" | undefined;
|
|
3176
|
+
optout: "optional" | undefined;
|
|
3165
3177
|
}
|
|
3166
3178
|
|
|
3167
3179
|
export interface $ZodNonOptional<T extends SomeType = $ZodType> extends $ZodType {
|
|
@@ -3255,6 +3267,8 @@ export interface $ZodSuccessDef<T extends SomeType = $ZodType> extends $ZodTypeD
|
|
|
3255
3267
|
export interface $ZodSuccessInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<boolean, core.input<T>> {
|
|
3256
3268
|
def: $ZodSuccessDef<T>;
|
|
3257
3269
|
isst: never;
|
|
3270
|
+
optin: T["_zod"]["optin"];
|
|
3271
|
+
optout: "optional" | undefined;
|
|
3258
3272
|
}
|
|
3259
3273
|
|
|
3260
3274
|
export interface $ZodSuccess<T extends SomeType = $ZodType> extends $ZodType {
|