zod 4.5.4 → 4.6.0
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/compile.cjs +19 -0
- package/index.cjs +19 -0
- package/locales/index.cjs +19 -0
- package/mini/index.cjs +19 -0
- package/package.json +8 -7
- package/src/v3/helpers/errorUtil.ts +1 -1
- package/src/v4/classic/checks.ts +0 -1
- package/src/v4/classic/external.ts +2 -0
- package/src/v4/classic/from-json-schema.ts +248 -25
- package/src/v4/classic/schemas.ts +255 -164
- package/src/v4/classic/tests/assignability.test.ts +59 -9
- package/src/v4/classic/tests/bigint.test.ts +6 -0
- package/src/v4/classic/tests/continuability.test.ts +2 -2
- package/src/v4/classic/tests/cyclic-data.test.ts +274 -0
- package/src/v4/classic/tests/enum.test.ts +9 -0
- package/src/v4/classic/tests/error.test.ts +130 -1
- package/src/v4/classic/tests/firstparty.test.ts +4 -0
- package/src/v4/classic/tests/from-json-schema.test.ts +157 -0
- package/src/v4/classic/tests/iban.test.ts +81 -0
- package/src/v4/classic/tests/instance-footprint.test.ts +18 -2
- package/src/v4/classic/tests/instanceof.test.ts +27 -7
- package/src/v4/classic/tests/issue-schema.test.ts +88 -56
- package/src/v4/classic/tests/object.test.ts +56 -0
- package/src/v4/classic/tests/optin-ladder.test.ts +3 -2
- package/src/v4/classic/tests/properties.test.ts +225 -0
- package/src/v4/classic/tests/recursive-types.test.ts +1 -1
- package/src/v4/classic/tests/standard-schema.test.ts +19 -0
- package/src/v4/classic/tests/string.test.ts +25 -0
- package/src/v4/classic/tests/template-literal.test.ts +33 -1
- package/src/v4/classic/tests/to-json-schema.test.ts +147 -3
- package/src/v4/classic/tests/validate.test.ts +569 -0
- package/src/v4/core/api.ts +30 -6
- package/src/v4/core/checks.ts +1 -101
- package/src/v4/core/compile.ts +193 -81
- package/src/v4/core/doc.ts +6 -2
- package/src/v4/core/errors.ts +3 -6
- package/src/v4/core/json-schema-generator.ts +2 -2
- package/src/v4/core/json-schema-processors.ts +226 -68
- package/src/v4/core/memoizer.ts +78 -28
- package/src/v4/core/parse.ts +37 -16
- package/src/v4/core/regexes.ts +10 -4
- package/src/v4/core/schemas.ts +314 -83
- package/src/v4/core/tests/cached.test.ts +54 -0
- package/src/v4/core/tests/compile-generative.test.ts +445 -0
- package/src/v4/core/tests/compile.test.ts +222 -2
- package/src/v4/core/tests/email-regex.test.ts +140 -0
- package/src/v4/core/tests/polyfill-collision.test.ts +46 -0
- package/src/v4/core/to-json-schema.ts +10 -8
- package/src/v4/core/util.ts +222 -147
- package/src/v4/core/versions.ts +2 -2
- package/src/v4/core/visit.ts +11 -0
- package/src/v4/locales/ar.ts +1 -0
- package/src/v4/locales/az.ts +1 -0
- package/src/v4/locales/be.ts +1 -0
- package/src/v4/locales/bg.ts +1 -0
- package/src/v4/locales/bn.ts +1 -0
- package/src/v4/locales/ca.ts +1 -0
- package/src/v4/locales/ckb.ts +1 -0
- package/src/v4/locales/cs.ts +1 -0
- package/src/v4/locales/da.ts +1 -0
- package/src/v4/locales/de.ts +1 -0
- package/src/v4/locales/el.ts +1 -0
- package/src/v4/locales/en.ts +1 -0
- package/src/v4/locales/eo.ts +1 -0
- package/src/v4/locales/es.ts +1 -0
- package/src/v4/locales/fa.ts +1 -0
- package/src/v4/locales/fi.ts +1 -0
- package/src/v4/locales/fr-CA.ts +1 -0
- package/src/v4/locales/fr.ts +1 -0
- package/src/v4/locales/gu.ts +1 -0
- package/src/v4/locales/he.ts +1 -0
- package/src/v4/locales/hi.ts +1 -0
- package/src/v4/locales/hr.ts +1 -0
- package/src/v4/locales/hu.ts +1 -0
- package/src/v4/locales/hy.ts +1 -0
- package/src/v4/locales/id.ts +1 -0
- package/src/v4/locales/is.ts +1 -0
- package/src/v4/locales/it.ts +1 -0
- package/src/v4/locales/ja.ts +1 -0
- package/src/v4/locales/ka.ts +1 -0
- package/src/v4/locales/km.ts +1 -0
- package/src/v4/locales/kn.ts +1 -0
- package/src/v4/locales/ko.ts +1 -0
- package/src/v4/locales/lt.ts +1 -0
- package/src/v4/locales/mk.ts +1 -0
- package/src/v4/locales/ms.ts +1 -0
- package/src/v4/locales/ne.ts +1 -0
- package/src/v4/locales/nl.ts +1 -0
- package/src/v4/locales/nn.ts +1 -0
- package/src/v4/locales/no.ts +1 -0
- package/src/v4/locales/ota.ts +1 -0
- package/src/v4/locales/pl.ts +1 -0
- package/src/v4/locales/ps.ts +1 -0
- package/src/v4/locales/pt-BR.ts +1 -0
- package/src/v4/locales/pt.ts +1 -0
- package/src/v4/locales/ro.ts +1 -0
- package/src/v4/locales/ru.ts +1 -0
- package/src/v4/locales/sk.ts +1 -0
- package/src/v4/locales/sl.ts +1 -0
- package/src/v4/locales/sv.ts +1 -0
- package/src/v4/locales/ta.ts +1 -0
- package/src/v4/locales/th.ts +1 -0
- package/src/v4/locales/tk.ts +1 -0
- package/src/v4/locales/tr.ts +1 -0
- package/src/v4/locales/uk.ts +1 -0
- package/src/v4/locales/ur.ts +1 -0
- package/src/v4/locales/uz.ts +1 -0
- package/src/v4/locales/vi.ts +1 -0
- package/src/v4/locales/yo.ts +1 -0
- package/src/v4/locales/zh-CN.ts +1 -0
- package/src/v4/locales/zh-TW.ts +1 -0
- package/src/v4/mini/checks.ts +0 -1
- package/src/v4/mini/external.ts +2 -0
- package/src/v4/mini/schemas.ts +38 -2
- package/src/v4/mini/tests/checks.test.ts +1 -1
- package/src/v4/mini/tests/computed.test.ts +11 -17
- package/src/v4/mini/tests/index.test.ts +8 -0
- package/src/v4/mini/tests/string.test.ts +16 -0
- package/v3/ZodError.cjs +19 -0
- package/v3/errors.cjs +19 -0
- package/v3/external.cjs +19 -0
- package/v3/helpers/enumUtil.cjs +19 -0
- package/v3/helpers/errorUtil.cjs +20 -1
- package/v3/helpers/errorUtil.js +1 -1
- package/v3/helpers/parseUtil.cjs +19 -0
- package/v3/helpers/partialUtil.cjs +19 -0
- package/v3/helpers/typeAliases.cjs +19 -0
- package/v3/helpers/util.cjs +19 -0
- package/v3/index.cjs +19 -0
- package/v3/standard-schema.cjs +19 -0
- package/v3/types.cjs +19 -0
- package/v4/classic/checks.cjs +20 -2
- package/v4/classic/checks.d.cts +1 -1
- package/v4/classic/checks.d.ts +1 -1
- package/v4/classic/checks.js +1 -1
- package/v4/classic/coerce.cjs +19 -0
- package/v4/classic/compat.cjs +19 -0
- package/v4/classic/deep-partial.cjs +19 -0
- package/v4/classic/errors.cjs +19 -0
- package/v4/classic/external.cjs +22 -1
- package/v4/classic/external.d.cts +1 -1
- package/v4/classic/external.d.ts +1 -1
- package/v4/classic/external.js +1 -1
- package/v4/classic/from-json-schema.cjs +264 -24
- package/v4/classic/from-json-schema.js +245 -24
- package/v4/classic/in-out.cjs +19 -0
- package/v4/classic/index.cjs +19 -0
- package/v4/classic/iso.cjs +19 -0
- package/v4/classic/parse.cjs +19 -0
- package/v4/classic/schemas.cjs +100 -31
- package/v4/classic/schemas.d.cts +17 -1
- package/v4/classic/schemas.d.ts +17 -1
- package/v4/classic/schemas.js +77 -29
- package/v4/core/api.cjs +37 -2
- package/v4/core/api.d.cts +6 -4
- package/v4/core/api.d.ts +6 -4
- package/v4/core/api.js +17 -2
- package/v4/core/checks.cjs +19 -96
- package/v4/core/checks.d.cts +1 -1
- package/v4/core/checks.d.ts +1 -1
- package/v4/core/checks.js +0 -96
- package/v4/core/compile.cjs +164 -65
- package/v4/core/compile.d.cts +18 -4
- package/v4/core/compile.d.ts +18 -4
- package/v4/core/compile.js +145 -66
- package/v4/core/core.cjs +19 -0
- package/v4/core/doc.cjs +26 -2
- package/v4/core/doc.js +7 -2
- package/v4/core/errors.cjs +21 -5
- package/v4/core/errors.js +2 -5
- package/v4/core/index.cjs +19 -0
- package/v4/core/json-schema-generator.cjs +20 -1
- package/v4/core/json-schema-generator.js +2 -2
- package/v4/core/json-schema-processors.cjs +215 -63
- package/v4/core/json-schema-processors.d.cts +17 -0
- package/v4/core/json-schema-processors.d.ts +17 -0
- package/v4/core/json-schema-processors.js +195 -64
- package/v4/core/json-schema.cjs +19 -0
- package/v4/core/memoizer.cjs +114 -23
- package/v4/core/memoizer.js +72 -23
- package/v4/core/parse.cjs +56 -16
- package/v4/core/parse.js +37 -16
- package/v4/core/regexes.cjs +29 -6
- package/v4/core/regexes.d.cts +2 -0
- package/v4/core/regexes.d.ts +2 -0
- package/v4/core/regexes.js +8 -4
- package/v4/core/registries.cjs +19 -0
- package/v4/core/schemas.cjs +285 -66
- package/v4/core/schemas.d.cts +34 -3
- package/v4/core/schemas.d.ts +34 -3
- package/v4/core/schemas.js +264 -65
- package/v4/core/standard-schema.cjs +19 -0
- package/v4/core/to-json-schema.cjs +29 -7
- package/v4/core/to-json-schema.d.cts +7 -3
- package/v4/core/to-json-schema.d.ts +7 -3
- package/v4/core/to-json-schema.js +8 -7
- package/v4/core/util.cjs +181 -143
- package/v4/core/util.d.cts +25 -4
- package/v4/core/util.d.ts +25 -4
- package/v4/core/util.js +160 -143
- package/v4/core/versions.cjs +21 -2
- package/v4/core/versions.d.cts +1 -1
- package/v4/core/versions.d.ts +1 -1
- package/v4/core/versions.js +2 -2
- package/v4/core/visit.cjs +31 -0
- package/v4/core/visit.js +12 -0
- package/v4/index.cjs +19 -0
- package/v4/locales/ar.cjs +1 -0
- package/v4/locales/ar.js +1 -0
- package/v4/locales/az.cjs +1 -0
- package/v4/locales/az.js +1 -0
- package/v4/locales/be.cjs +1 -0
- package/v4/locales/be.js +1 -0
- package/v4/locales/bg.cjs +1 -0
- package/v4/locales/bg.js +1 -0
- package/v4/locales/bn.cjs +1 -0
- package/v4/locales/bn.js +1 -0
- package/v4/locales/ca.cjs +1 -0
- package/v4/locales/ca.js +1 -0
- package/v4/locales/ckb.cjs +1 -0
- package/v4/locales/ckb.js +1 -0
- package/v4/locales/cs.cjs +1 -0
- package/v4/locales/cs.js +1 -0
- package/v4/locales/da.cjs +1 -0
- package/v4/locales/da.js +1 -0
- package/v4/locales/de.cjs +1 -0
- package/v4/locales/de.js +1 -0
- package/v4/locales/el.cjs +1 -0
- package/v4/locales/el.js +1 -0
- package/v4/locales/en.cjs +1 -0
- package/v4/locales/en.js +1 -0
- package/v4/locales/eo.cjs +1 -0
- package/v4/locales/eo.js +1 -0
- package/v4/locales/es.cjs +1 -0
- package/v4/locales/es.js +1 -0
- package/v4/locales/fa.cjs +1 -0
- package/v4/locales/fa.js +1 -0
- package/v4/locales/fi.cjs +1 -0
- package/v4/locales/fi.js +1 -0
- package/v4/locales/fr-CA.cjs +1 -0
- package/v4/locales/fr-CA.js +1 -0
- package/v4/locales/fr.cjs +1 -0
- package/v4/locales/fr.js +1 -0
- package/v4/locales/gu.cjs +1 -0
- package/v4/locales/gu.js +1 -0
- package/v4/locales/he.cjs +1 -0
- package/v4/locales/he.js +1 -0
- package/v4/locales/hi.cjs +1 -0
- package/v4/locales/hi.js +1 -0
- package/v4/locales/hr.cjs +1 -0
- package/v4/locales/hr.js +1 -0
- package/v4/locales/hu.cjs +1 -0
- package/v4/locales/hu.js +1 -0
- package/v4/locales/hy.cjs +1 -0
- package/v4/locales/hy.js +1 -0
- package/v4/locales/id.cjs +1 -0
- package/v4/locales/id.js +1 -0
- package/v4/locales/index.cjs +19 -0
- package/v4/locales/is.cjs +1 -0
- package/v4/locales/is.js +1 -0
- package/v4/locales/it.cjs +1 -0
- package/v4/locales/it.js +1 -0
- package/v4/locales/ja.cjs +1 -0
- package/v4/locales/ja.js +1 -0
- package/v4/locales/ka.cjs +1 -0
- package/v4/locales/ka.js +1 -0
- package/v4/locales/km.cjs +1 -0
- package/v4/locales/km.js +1 -0
- package/v4/locales/kn.cjs +1 -0
- package/v4/locales/kn.js +1 -0
- package/v4/locales/ko.cjs +1 -0
- package/v4/locales/ko.js +1 -0
- package/v4/locales/lt.cjs +1 -0
- package/v4/locales/lt.js +1 -0
- package/v4/locales/mk.cjs +1 -0
- package/v4/locales/mk.js +1 -0
- package/v4/locales/ms.cjs +1 -0
- package/v4/locales/ms.js +1 -0
- package/v4/locales/ne.cjs +1 -0
- package/v4/locales/ne.js +1 -0
- package/v4/locales/nl.cjs +1 -0
- package/v4/locales/nl.js +1 -0
- package/v4/locales/nn.cjs +1 -0
- package/v4/locales/nn.js +1 -0
- package/v4/locales/no.cjs +1 -0
- package/v4/locales/no.js +1 -0
- package/v4/locales/ota.cjs +1 -0
- package/v4/locales/ota.js +1 -0
- package/v4/locales/pl.cjs +1 -0
- package/v4/locales/pl.js +1 -0
- package/v4/locales/ps.cjs +1 -0
- package/v4/locales/ps.js +1 -0
- package/v4/locales/pt-BR.cjs +1 -0
- package/v4/locales/pt-BR.js +1 -0
- package/v4/locales/pt.cjs +1 -0
- package/v4/locales/pt.js +1 -0
- package/v4/locales/ro.cjs +1 -0
- package/v4/locales/ro.js +1 -0
- package/v4/locales/ru.cjs +1 -0
- package/v4/locales/ru.js +1 -0
- package/v4/locales/sk.cjs +1 -0
- package/v4/locales/sk.js +1 -0
- package/v4/locales/sl.cjs +1 -0
- package/v4/locales/sl.js +1 -0
- package/v4/locales/sv.cjs +1 -0
- package/v4/locales/sv.js +1 -0
- package/v4/locales/ta.cjs +1 -0
- package/v4/locales/ta.js +1 -0
- package/v4/locales/th.cjs +1 -0
- package/v4/locales/th.js +1 -0
- package/v4/locales/tk.cjs +1 -0
- package/v4/locales/tk.js +1 -0
- package/v4/locales/tr.cjs +1 -0
- package/v4/locales/tr.js +1 -0
- package/v4/locales/uk.cjs +1 -0
- package/v4/locales/uk.js +1 -0
- package/v4/locales/ur.cjs +1 -0
- package/v4/locales/ur.js +1 -0
- package/v4/locales/uz.cjs +1 -0
- package/v4/locales/uz.js +1 -0
- package/v4/locales/vi.cjs +1 -0
- package/v4/locales/vi.js +1 -0
- package/v4/locales/yo.cjs +1 -0
- package/v4/locales/yo.js +1 -0
- package/v4/locales/zh-CN.cjs +1 -0
- package/v4/locales/zh-CN.js +1 -0
- package/v4/locales/zh-TW.cjs +1 -0
- package/v4/locales/zh-TW.js +1 -0
- package/v4/mini/checks.cjs +20 -2
- package/v4/mini/checks.d.cts +1 -1
- package/v4/mini/checks.d.ts +1 -1
- package/v4/mini/checks.js +1 -1
- package/v4/mini/coerce.cjs +19 -0
- package/v4/mini/deep-partial.cjs +19 -0
- package/v4/mini/external.cjs +22 -1
- package/v4/mini/external.d.cts +1 -1
- package/v4/mini/external.d.ts +1 -1
- package/v4/mini/external.js +1 -1
- package/v4/mini/in-out.cjs +19 -0
- package/v4/mini/index.cjs +19 -0
- package/v4/mini/iso.cjs +19 -0
- package/v4/mini/parse.cjs +19 -0
- package/v4/mini/schemas.cjs +42 -4
- package/v4/mini/schemas.d.cts +8 -0
- package/v4/mini/schemas.d.ts +8 -0
- package/v4/mini/schemas.js +19 -2
- package/v4-mini/index.cjs +19 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { util } from "zod/v4/core";
|
|
4
|
+
|
|
5
|
+
test("cached defers the getter until the first read", () => {
|
|
6
|
+
let runs = 0;
|
|
7
|
+
const box = util.cached(() => ++runs);
|
|
8
|
+
expect(runs).toBe(0);
|
|
9
|
+
expect(box.value).toBe(1);
|
|
10
|
+
expect(box.value).toBe(1);
|
|
11
|
+
expect(runs).toBe(1);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test("cached memoizes an undefined result", () => {
|
|
15
|
+
let runs = 0;
|
|
16
|
+
const box = util.cached(() => {
|
|
17
|
+
runs++;
|
|
18
|
+
return undefined;
|
|
19
|
+
});
|
|
20
|
+
expect(box.value).toBeUndefined();
|
|
21
|
+
expect(box.value).toBeUndefined();
|
|
22
|
+
expect(runs).toBe(1);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("cached retries after the getter throws", () => {
|
|
26
|
+
let attempts = 0;
|
|
27
|
+
const box = util.cached(() => {
|
|
28
|
+
if (++attempts < 2) throw new Error("boom");
|
|
29
|
+
return "recovered";
|
|
30
|
+
});
|
|
31
|
+
expect(() => box.value).toThrow("boom");
|
|
32
|
+
expect(box.value).toBe("recovered");
|
|
33
|
+
expect(attempts).toBe(2);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("cached rejects assignment to value", () => {
|
|
37
|
+
const box = util.cached(() => 1);
|
|
38
|
+
expect(() => {
|
|
39
|
+
(box as { value: number }).value = 2;
|
|
40
|
+
}).toThrow(TypeError);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// the getter is inherited, so a box carries only its two state slots and stays out of dictionary mode
|
|
44
|
+
test("cached keeps value on the prototype", () => {
|
|
45
|
+
const box = util.cached(() => 1);
|
|
46
|
+
expect(Object.getOwnPropertyDescriptor(box, "value")).toBeUndefined();
|
|
47
|
+
expect("value" in box).toBe(true);
|
|
48
|
+
expect(box.value).toBe(1);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("allowsEval reads through the same box", () => {
|
|
52
|
+
expect(util.allowsEval.value).toBe(true);
|
|
53
|
+
expect(util.allowsEval.value).toBe(true);
|
|
54
|
+
});
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import * as z from "../../index.js";
|
|
4
|
+
import { compile, compileFn } from "../compile.js";
|
|
5
|
+
|
|
6
|
+
// Generative differential: random schemas over the compiled node types, a valid value for each, single corruptions of that value, and the assertion that the interpreter and the compiled schema agree on the verdict, the output, the issues and how often user callbacks ran. A fixture suite covers the compositions somebody wrote down; this covers the ones nobody did. Seeds are fixed, so a failure names the seed and the schema it built.
|
|
7
|
+
|
|
8
|
+
function mulberry32(seed: number): () => number {
|
|
9
|
+
return () => {
|
|
10
|
+
seed = (seed + 0x6d2b79f5) | 0;
|
|
11
|
+
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
|
|
12
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
13
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Gen {
|
|
18
|
+
schema: z.ZodType;
|
|
19
|
+
desc: string;
|
|
20
|
+
valid: () => unknown;
|
|
21
|
+
// one corruption of a valid value; not always rejected (a loose object absorbs an extra key), and both sides must agree either way
|
|
22
|
+
corrupt: (v: unknown) => unknown;
|
|
23
|
+
// this node itself has no fast emitter and compiles through the runtime
|
|
24
|
+
island: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface Ctx {
|
|
28
|
+
rand: () => number;
|
|
29
|
+
// runs per generated callback, so the two-run bound holds for each one rather than for the sum
|
|
30
|
+
calls: Map<number, number>;
|
|
31
|
+
nextCallback: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function count(c: Ctx, id: number): void {
|
|
35
|
+
c.calls.set(id, (c.calls.get(id) ?? 0) + 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const pick = <T>(c: Ctx, xs: readonly T[]): T => xs[Math.floor(c.rand() * xs.length)]!;
|
|
39
|
+
const int = (c: Ctx, lo: number, hi: number): number => lo + Math.floor(c.rand() * (hi - lo + 1));
|
|
40
|
+
const JUNK = [42, "x", null, undefined, true, [], {}, -0, Number.NaN] as const;
|
|
41
|
+
|
|
42
|
+
function leaf(c: Ctx): Gen {
|
|
43
|
+
switch (int(c, 0, 7)) {
|
|
44
|
+
case 0: {
|
|
45
|
+
const min = int(c, 0, 3);
|
|
46
|
+
return {
|
|
47
|
+
schema: z.string().min(min),
|
|
48
|
+
desc: `string.min(${min})`,
|
|
49
|
+
valid: () => "x".repeat(min + int(c, 0, 2)),
|
|
50
|
+
corrupt: () => (c.rand() < 0.5 ? 42 : "x".repeat(Math.max(0, min - 1))),
|
|
51
|
+
island: false,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
case 1:
|
|
55
|
+
return {
|
|
56
|
+
schema: z.email(),
|
|
57
|
+
desc: "email",
|
|
58
|
+
valid: () => "a@b.co",
|
|
59
|
+
corrupt: () => (c.rand() < 0.5 ? "nope" : 1),
|
|
60
|
+
island: false,
|
|
61
|
+
};
|
|
62
|
+
case 2: {
|
|
63
|
+
const lo = int(c, -5, 0);
|
|
64
|
+
const hi = int(c, 1, 5);
|
|
65
|
+
return {
|
|
66
|
+
schema: z.number().int().min(lo).max(hi),
|
|
67
|
+
desc: `int[${lo},${hi}]`,
|
|
68
|
+
valid: () => int(c, lo, hi),
|
|
69
|
+
corrupt: () => pick(c, [hi + 1, lo - 1, 1.5, "1"]),
|
|
70
|
+
island: false,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
case 3:
|
|
74
|
+
return {
|
|
75
|
+
schema: z.boolean(),
|
|
76
|
+
desc: "boolean",
|
|
77
|
+
valid: () => c.rand() < 0.5,
|
|
78
|
+
corrupt: () => pick(c, ["true", 0, null]),
|
|
79
|
+
island: false,
|
|
80
|
+
};
|
|
81
|
+
case 4: {
|
|
82
|
+
const lit = pick(c, ["a", 1, true] as const);
|
|
83
|
+
return {
|
|
84
|
+
schema: z.literal(lit),
|
|
85
|
+
desc: `literal(${JSON.stringify(lit)})`,
|
|
86
|
+
valid: () => lit,
|
|
87
|
+
corrupt: () => pick(c, ["b", 2, false]),
|
|
88
|
+
island: false,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
case 5:
|
|
92
|
+
return {
|
|
93
|
+
schema: z.enum(["a", "b", "c"]),
|
|
94
|
+
desc: "enum",
|
|
95
|
+
valid: () => pick(c, ["a", "b", "c"]),
|
|
96
|
+
corrupt: () => pick(c, ["d", 2, null]),
|
|
97
|
+
island: false,
|
|
98
|
+
};
|
|
99
|
+
case 6: {
|
|
100
|
+
const id = c.nextCallback++;
|
|
101
|
+
return {
|
|
102
|
+
schema: z.number().superRefine((v, ctx) => {
|
|
103
|
+
count(c, id);
|
|
104
|
+
if (v < 0) ctx.addIssue({ code: "custom", message: "negative" });
|
|
105
|
+
}),
|
|
106
|
+
desc: "number.superRefine",
|
|
107
|
+
valid: () => int(c, 0, 9),
|
|
108
|
+
corrupt: () => pick(c, [-1, "x"]),
|
|
109
|
+
island: false,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
default: {
|
|
113
|
+
const id = c.nextCallback++;
|
|
114
|
+
return {
|
|
115
|
+
schema: z.string().transform((s) => {
|
|
116
|
+
count(c, id);
|
|
117
|
+
return s.length;
|
|
118
|
+
}),
|
|
119
|
+
desc: "string.transform",
|
|
120
|
+
valid: () => "y".repeat(int(c, 0, 3)),
|
|
121
|
+
corrupt: () => 42,
|
|
122
|
+
island: false,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function wrap(c: Ctx, inner: Gen): Gen {
|
|
129
|
+
switch (int(c, 0, 4)) {
|
|
130
|
+
case 0:
|
|
131
|
+
return {
|
|
132
|
+
...inner,
|
|
133
|
+
schema: inner.schema.optional(),
|
|
134
|
+
desc: `${inner.desc}.optional`,
|
|
135
|
+
valid: () => (c.rand() < 0.3 ? undefined : inner.valid()),
|
|
136
|
+
island: false,
|
|
137
|
+
};
|
|
138
|
+
case 1:
|
|
139
|
+
return {
|
|
140
|
+
...inner,
|
|
141
|
+
schema: inner.schema.nullable(),
|
|
142
|
+
desc: `${inner.desc}.nullable`,
|
|
143
|
+
valid: () => (c.rand() < 0.3 ? null : inner.valid()),
|
|
144
|
+
island: false,
|
|
145
|
+
};
|
|
146
|
+
case 2: {
|
|
147
|
+
const dflt = inner.valid();
|
|
148
|
+
return {
|
|
149
|
+
...inner,
|
|
150
|
+
schema: inner.schema.default(dflt as never),
|
|
151
|
+
desc: `${inner.desc}.default`,
|
|
152
|
+
valid: () => (c.rand() < 0.3 ? undefined : inner.valid()),
|
|
153
|
+
island: false,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
case 3:
|
|
157
|
+
return { ...inner, schema: inner.schema.readonly(), desc: `${inner.desc}.readonly`, island: false };
|
|
158
|
+
default:
|
|
159
|
+
return {
|
|
160
|
+
...inner,
|
|
161
|
+
schema: inner.schema.optional().nonoptional(),
|
|
162
|
+
desc: `${inner.desc}.optional.nonoptional`,
|
|
163
|
+
corrupt: (v) => (c.rand() < 0.5 ? undefined : inner.corrupt(v)),
|
|
164
|
+
island: false,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function container(c: Ctx, depth: number): Gen {
|
|
170
|
+
const child = () => gen(c, depth - 1);
|
|
171
|
+
switch (int(c, 0, 8)) {
|
|
172
|
+
case 0:
|
|
173
|
+
case 1: {
|
|
174
|
+
const n = int(c, 1, 4);
|
|
175
|
+
const entries = Array.from({ length: n }, (_, i) => [`k${i}`, child(), c.rand() < 0.3] as const);
|
|
176
|
+
const shape = Object.fromEntries(entries.map(([k, g, opt]) => [k, opt ? g.schema.optional() : g.schema]));
|
|
177
|
+
const mode = pick(c, ["plain", "strict", "loose", "catchall"] as const);
|
|
178
|
+
const schema =
|
|
179
|
+
mode === "strict"
|
|
180
|
+
? z.strictObject(shape)
|
|
181
|
+
: mode === "loose"
|
|
182
|
+
? z.looseObject(shape)
|
|
183
|
+
: mode === "catchall"
|
|
184
|
+
? z.object(shape).catchall(z.number())
|
|
185
|
+
: z.object(shape);
|
|
186
|
+
const valid = () => {
|
|
187
|
+
const o: Record<string, unknown> = {};
|
|
188
|
+
for (const [k, g, opt] of entries) if (!opt || c.rand() < 0.6) o[k] = g.valid();
|
|
189
|
+
if (mode === "catchall" && c.rand() < 0.5) o.extra = 1;
|
|
190
|
+
return o;
|
|
191
|
+
};
|
|
192
|
+
return {
|
|
193
|
+
schema,
|
|
194
|
+
desc: `${mode}{${entries.map(([k, g, opt]) => `${k}${opt ? "?" : ""}:${g.desc}`).join(",")}}`,
|
|
195
|
+
valid,
|
|
196
|
+
corrupt: (v) => {
|
|
197
|
+
if (typeof v !== "object" || v === null) return v;
|
|
198
|
+
const o = { ...(v as Record<string, unknown>) };
|
|
199
|
+
const [k, g, opt] = pick(c, entries);
|
|
200
|
+
switch (int(c, 0, 3)) {
|
|
201
|
+
case 0:
|
|
202
|
+
o[k] = g.corrupt(k in o ? o[k] : g.valid());
|
|
203
|
+
break;
|
|
204
|
+
case 1:
|
|
205
|
+
if (opt) o[k] = g.corrupt(g.valid());
|
|
206
|
+
else delete o[k];
|
|
207
|
+
break;
|
|
208
|
+
case 2:
|
|
209
|
+
o.extra = "e";
|
|
210
|
+
break;
|
|
211
|
+
default:
|
|
212
|
+
return 42;
|
|
213
|
+
}
|
|
214
|
+
return o;
|
|
215
|
+
},
|
|
216
|
+
island: false,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
case 2: {
|
|
220
|
+
const g = child();
|
|
221
|
+
const min = int(c, 0, 2);
|
|
222
|
+
return {
|
|
223
|
+
schema: z.array(g.schema).min(min),
|
|
224
|
+
desc: `array(${g.desc}).min(${min})`,
|
|
225
|
+
valid: () => Array.from({ length: min + int(c, 0, 2) }, () => g.valid()),
|
|
226
|
+
corrupt: (v) => {
|
|
227
|
+
if (!Array.isArray(v)) return v;
|
|
228
|
+
const a = [...v];
|
|
229
|
+
if (a.length && c.rand() < 0.6) a[int(c, 0, a.length - 1)] = g.corrupt(a[0]);
|
|
230
|
+
else if (min > 0) a.length = min - 1;
|
|
231
|
+
else return 42;
|
|
232
|
+
return a;
|
|
233
|
+
},
|
|
234
|
+
island: false,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
case 3: {
|
|
238
|
+
const items = Array.from({ length: int(c, 1, 3) }, child);
|
|
239
|
+
const rest = c.rand() < 0.4 ? child() : null;
|
|
240
|
+
const schema = rest
|
|
241
|
+
? z.tuple(items.map((g) => g.schema) as [z.ZodType, ...z.ZodType[]], rest.schema)
|
|
242
|
+
: z.tuple(items.map((g) => g.schema) as [z.ZodType, ...z.ZodType[]]);
|
|
243
|
+
return {
|
|
244
|
+
schema,
|
|
245
|
+
desc: `tuple(${items.map((g) => g.desc).join(",")}${rest ? `,...${rest.desc}` : ""})`,
|
|
246
|
+
valid: () => [
|
|
247
|
+
...items.map((g) => g.valid()),
|
|
248
|
+
...(rest ? Array.from({ length: int(c, 0, 2) }, () => rest.valid()) : []),
|
|
249
|
+
],
|
|
250
|
+
corrupt: (v) => {
|
|
251
|
+
if (!Array.isArray(v)) return v;
|
|
252
|
+
const a = [...v];
|
|
253
|
+
const i = int(c, 0, items.length - 1);
|
|
254
|
+
if (c.rand() < 0.6) a[i] = items[i]!.corrupt(a[i]);
|
|
255
|
+
else if (c.rand() < 0.5) a.push("extra");
|
|
256
|
+
else a.length = i;
|
|
257
|
+
return a;
|
|
258
|
+
},
|
|
259
|
+
island: false,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
case 4: {
|
|
263
|
+
const g = child();
|
|
264
|
+
const enumKeys = c.rand() < 0.4;
|
|
265
|
+
const schema = enumKeys ? z.record(z.enum(["a", "b"]), g.schema) : z.record(z.string(), g.schema);
|
|
266
|
+
return {
|
|
267
|
+
schema,
|
|
268
|
+
desc: `record(${enumKeys ? "enum" : "string"},${g.desc})`,
|
|
269
|
+
valid: () =>
|
|
270
|
+
enumKeys
|
|
271
|
+
? { a: g.valid(), b: g.valid() }
|
|
272
|
+
: Object.fromEntries(Array.from({ length: int(c, 0, 3) }, (_, i) => [`r${i}`, g.valid()])),
|
|
273
|
+
corrupt: (v) => {
|
|
274
|
+
if (typeof v !== "object" || v === null) return v;
|
|
275
|
+
const o = { ...(v as Record<string, unknown>) };
|
|
276
|
+
const keys = Object.keys(o);
|
|
277
|
+
if (keys.length && c.rand() < 0.6) o[pick(c, keys)] = g.corrupt(o[keys[0]!]);
|
|
278
|
+
else if (enumKeys) o.z = g.valid();
|
|
279
|
+
else return 42;
|
|
280
|
+
return o;
|
|
281
|
+
},
|
|
282
|
+
island: false,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
case 5: {
|
|
286
|
+
const options = Array.from({ length: int(c, 2, 3) }, child);
|
|
287
|
+
return {
|
|
288
|
+
schema: z.union(options.map((g) => g.schema) as [z.ZodType, z.ZodType, ...z.ZodType[]]),
|
|
289
|
+
desc: `union(${options.map((g) => g.desc).join("|")})`,
|
|
290
|
+
valid: () => pick(c, options).valid(),
|
|
291
|
+
corrupt: (v) => pick(c, options).corrupt(v),
|
|
292
|
+
island: false,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
case 6: {
|
|
296
|
+
const branches = Array.from({ length: int(c, 2, 3) }, (_, i) => [i, child()] as const);
|
|
297
|
+
return {
|
|
298
|
+
schema: z.discriminatedUnion(
|
|
299
|
+
"t",
|
|
300
|
+
branches.map(([i, g]) => z.object({ t: z.literal(`b${i}`), v: g.schema })) as [
|
|
301
|
+
z.ZodObject<{ t: z.ZodLiteral<string>; v: z.ZodType }>,
|
|
302
|
+
...z.ZodObject<{ t: z.ZodLiteral<string>; v: z.ZodType }>[],
|
|
303
|
+
]
|
|
304
|
+
),
|
|
305
|
+
desc: `dunion(${branches.map(([, g]) => g.desc).join("|")})`,
|
|
306
|
+
valid: () => {
|
|
307
|
+
const [i, g] = pick(c, branches);
|
|
308
|
+
return { t: `b${i}`, v: g.valid() };
|
|
309
|
+
},
|
|
310
|
+
corrupt: (v) => {
|
|
311
|
+
if (typeof v !== "object" || v === null) return v;
|
|
312
|
+
const o = { ...(v as { t: string; v: unknown }) };
|
|
313
|
+
const branch = branches.find(([i]) => `b${i}` === o.t);
|
|
314
|
+
if (branch && c.rand() < 0.6) o.v = branch[1].corrupt(o.v);
|
|
315
|
+
else o.t = "nope";
|
|
316
|
+
return o;
|
|
317
|
+
},
|
|
318
|
+
island: false,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
case 7: {
|
|
322
|
+
const min = int(c, 1, 3);
|
|
323
|
+
return {
|
|
324
|
+
schema: z.string().pipe(z.string().min(min)),
|
|
325
|
+
desc: `string.pipe(min(${min}))`,
|
|
326
|
+
valid: () => "p".repeat(min + int(c, 0, 1)),
|
|
327
|
+
corrupt: () => (c.rand() < 0.5 ? "p".repeat(min - 1) : 7),
|
|
328
|
+
island: false,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
default: {
|
|
332
|
+
// map and set: the compiled result still has to agree
|
|
333
|
+
const g = child();
|
|
334
|
+
if (c.rand() < 0.5) {
|
|
335
|
+
return {
|
|
336
|
+
schema: z.map(z.string(), g.schema),
|
|
337
|
+
desc: `map(string,${g.desc})`,
|
|
338
|
+
valid: () => new Map(Array.from({ length: int(c, 0, 2) }, (_, i) => [`m${i}`, g.valid()])),
|
|
339
|
+
corrupt: (v) => {
|
|
340
|
+
if (!(v instanceof Map)) return v;
|
|
341
|
+
const m = new Map(v);
|
|
342
|
+
if (m.size && c.rand() < 0.7) m.set("m0", g.corrupt(m.get("m0")));
|
|
343
|
+
else return [];
|
|
344
|
+
return m;
|
|
345
|
+
},
|
|
346
|
+
island: true,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
schema: z.set(g.schema),
|
|
351
|
+
desc: `set(${g.desc})`,
|
|
352
|
+
valid: () => new Set(Array.from({ length: int(c, 0, 2) }, () => g.valid())),
|
|
353
|
+
corrupt: (v) => {
|
|
354
|
+
if (!(v instanceof Set)) return v;
|
|
355
|
+
const s = new Set(v);
|
|
356
|
+
if (c.rand() < 0.7) s.add(g.corrupt(g.valid()));
|
|
357
|
+
else return {};
|
|
358
|
+
return s;
|
|
359
|
+
},
|
|
360
|
+
island: true,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function gen(c: Ctx, depth: number): Gen {
|
|
367
|
+
if (depth === 0) return leaf(c);
|
|
368
|
+
const r = c.rand();
|
|
369
|
+
if (r < 0.25) return leaf(c);
|
|
370
|
+
if (r < 0.4) return wrap(c, gen(c, depth - 1));
|
|
371
|
+
return container(c, depth);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function attempt<T>(fn: () => T): { value?: T; threw?: string } {
|
|
375
|
+
try {
|
|
376
|
+
return { value: fn() };
|
|
377
|
+
} catch (err) {
|
|
378
|
+
return { threw: (err as Error)?.constructor?.name || "Error" };
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function describe(value: unknown): string {
|
|
383
|
+
return JSON.stringify(value, (_k, v) =>
|
|
384
|
+
v instanceof Map ? { $map: [...v] } : v instanceof Set ? { $set: [...v] } : v === undefined ? "$undefined" : v
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// own-key order and undefined-valued-vs-absent keys, which toStrictEqual does not see
|
|
389
|
+
function shape(value: unknown): unknown {
|
|
390
|
+
if (Array.isArray(value)) return value.map(shape);
|
|
391
|
+
if (value && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) {
|
|
392
|
+
return Reflect.ownKeys(value).map((k) => [k, shape((value as Record<PropertyKey, unknown>)[k])]);
|
|
393
|
+
}
|
|
394
|
+
return value;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const SEEDS = 400;
|
|
398
|
+
|
|
399
|
+
test("random schemas parse identically through the interpreter and the compiler", () => {
|
|
400
|
+
// every failing seed is collected, so one run names all of them
|
|
401
|
+
const failures: string[] = [];
|
|
402
|
+
for (let seed = 1; seed <= SEEDS; seed++) {
|
|
403
|
+
try {
|
|
404
|
+
checkSeed(seed);
|
|
405
|
+
} catch (err) {
|
|
406
|
+
failures.push((err as Error).message.split("\n")[0]!);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
expect(failures).toEqual([]);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
function checkSeed(seed: number): void {
|
|
413
|
+
{
|
|
414
|
+
const c: Ctx = { rand: mulberry32(seed), calls: new Map(), nextCallback: 0 };
|
|
415
|
+
const g = gen(c, 3);
|
|
416
|
+
const compiled = compile(g.schema);
|
|
417
|
+
const inputs = [g.valid(), g.corrupt(g.valid()), g.corrupt(g.corrupt(g.valid())), pick(c, JUNK)];
|
|
418
|
+
for (const input of inputs) {
|
|
419
|
+
const label = `seed ${seed} ${g.desc} input ${describe(input)}`;
|
|
420
|
+
c.calls.clear();
|
|
421
|
+
const a = attempt(() => g.schema.safeParse(input));
|
|
422
|
+
const runtimeCalls = new Map(c.calls);
|
|
423
|
+
c.calls.clear();
|
|
424
|
+
const b = attempt(() => compiled.safeParse(input));
|
|
425
|
+
const compiledCalls = new Map(c.calls);
|
|
426
|
+
expect(b.threw, `${label}: throw`).toBe(a.threw);
|
|
427
|
+
if (a.threw) continue;
|
|
428
|
+
expect(b.value!.success, `${label}: verdict`).toBe(a.value!.success);
|
|
429
|
+
if (a.value!.success) {
|
|
430
|
+
expect(b.value!.data, `${label}: data`).toStrictEqual(a.value!.data);
|
|
431
|
+
expect(shape(b.value!.data), `${label}: key order`).toEqual(shape(a.value!.data));
|
|
432
|
+
} else {
|
|
433
|
+
expect(b.value!.error!.issues, `${label}: issues`).toEqual(a.value!.error!.issues);
|
|
434
|
+
}
|
|
435
|
+
// every callback the interpreter reaches, the compiled schema reaches too; the fast pass runs it at most once and the runtime re-parse at most once more
|
|
436
|
+
for (const id of new Set([...compiledCalls.keys(), ...runtimeCalls.keys()])) {
|
|
437
|
+
const n = compiledCalls.get(id) ?? 0;
|
|
438
|
+
const r = runtimeCalls.get(id) ?? 0;
|
|
439
|
+
expect(n, `${label}: callback ${id} ran ${n} vs ${r}`).toBeGreaterThanOrEqual(r);
|
|
440
|
+
expect(n, `${label}: callback ${id} ran ${n} vs ${r}`).toBeLessThanOrEqual(2 * r);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
expect(() => compileFn(g.schema), `${g.desc}: compile refused`).not.toThrow();
|
|
444
|
+
}
|
|
445
|
+
}
|