zod 3.13.4 → 3.14.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/README.md +5 -4
- package/lib/ZodError.js +110 -205
- package/lib/benchmarks/discriminatedUnion.js +25 -25
- package/lib/benchmarks/index.js +15 -53
- package/lib/benchmarks/object.js +23 -23
- package/lib/benchmarks/primitives.d.ts +5 -0
- package/lib/benchmarks/primitives.js +78 -0
- package/lib/benchmarks/realworld.d.ts +5 -0
- package/lib/benchmarks/realworld.js +56 -0
- package/lib/benchmarks/string.js +27 -16
- package/lib/benchmarks/union.js +25 -25
- package/lib/helpers/errorUtil.js +2 -6
- package/lib/helpers/parseUtil.d.ts +7 -5
- package/lib/helpers/parseUtil.js +84 -244
- package/lib/helpers/util.js +20 -66
- package/lib/index.js +1 -1
- package/lib/index.mjs +1636 -2167
- package/lib/types.d.ts +6 -4
- package/lib/types.js +1422 -1772
- package/package.json +16 -14
package/lib/benchmarks/object.js
CHANGED
|
@@ -3,67 +3,67 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const emptySuite = new benchmark_1.default.Suite("z.object: empty");
|
|
9
|
+
const shortSuite = new benchmark_1.default.Suite("z.object: short");
|
|
10
|
+
const longSuite = new benchmark_1.default.Suite("z.object: long");
|
|
11
|
+
const empty = index_1.z.object({});
|
|
12
|
+
const short = index_1.z.object({
|
|
13
13
|
string: index_1.z.string(),
|
|
14
14
|
});
|
|
15
|
-
|
|
15
|
+
const long = index_1.z.object({
|
|
16
16
|
string: index_1.z.string(),
|
|
17
17
|
number: index_1.z.number(),
|
|
18
18
|
boolean: index_1.z.boolean(),
|
|
19
19
|
});
|
|
20
20
|
emptySuite
|
|
21
|
-
.add("valid",
|
|
21
|
+
.add("valid", () => {
|
|
22
22
|
empty.parse({});
|
|
23
23
|
})
|
|
24
|
-
.add("valid: extra keys",
|
|
24
|
+
.add("valid: extra keys", () => {
|
|
25
25
|
empty.parse({ string: "string" });
|
|
26
26
|
})
|
|
27
|
-
.add("invalid: null",
|
|
27
|
+
.add("invalid: null", () => {
|
|
28
28
|
try {
|
|
29
29
|
empty.parse(null);
|
|
30
30
|
}
|
|
31
31
|
catch (err) { }
|
|
32
32
|
})
|
|
33
|
-
.on("cycle",
|
|
34
|
-
console.log(
|
|
33
|
+
.on("cycle", (e) => {
|
|
34
|
+
console.log(`${emptySuite.name}: ${e.target}`);
|
|
35
35
|
});
|
|
36
36
|
shortSuite
|
|
37
|
-
.add("valid",
|
|
37
|
+
.add("valid", () => {
|
|
38
38
|
short.parse({ string: "string" });
|
|
39
39
|
})
|
|
40
|
-
.add("valid: extra keys",
|
|
40
|
+
.add("valid: extra keys", () => {
|
|
41
41
|
short.parse({ string: "string", number: 42 });
|
|
42
42
|
})
|
|
43
|
-
.add("invalid: null",
|
|
43
|
+
.add("invalid: null", () => {
|
|
44
44
|
try {
|
|
45
45
|
short.parse(null);
|
|
46
46
|
}
|
|
47
47
|
catch (err) { }
|
|
48
48
|
})
|
|
49
|
-
.on("cycle",
|
|
50
|
-
console.log(
|
|
49
|
+
.on("cycle", (e) => {
|
|
50
|
+
console.log(`${shortSuite.name}: ${e.target}`);
|
|
51
51
|
});
|
|
52
52
|
longSuite
|
|
53
|
-
.add("valid",
|
|
53
|
+
.add("valid", () => {
|
|
54
54
|
long.parse({ string: "string", number: 42, boolean: true });
|
|
55
55
|
})
|
|
56
|
-
.add("valid: extra keys",
|
|
56
|
+
.add("valid: extra keys", () => {
|
|
57
57
|
long.parse({ string: "string", number: 42, boolean: true, list: [] });
|
|
58
58
|
})
|
|
59
|
-
.add("invalid: null",
|
|
59
|
+
.add("invalid: null", () => {
|
|
60
60
|
try {
|
|
61
61
|
long.parse(null);
|
|
62
62
|
}
|
|
63
63
|
catch (err) { }
|
|
64
64
|
})
|
|
65
|
-
.on("cycle",
|
|
66
|
-
console.log(
|
|
65
|
+
.on("cycle", (e) => {
|
|
66
|
+
console.log(`${longSuite.name}: ${e.target}`);
|
|
67
67
|
});
|
|
68
68
|
exports.default = {
|
|
69
69
|
suites: [emptySuite, shortSuite, longSuite],
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const enumSuite = new benchmark_1.default.Suite("z.enum");
|
|
9
|
+
const enumSchema = index_1.z.enum(["a", "b", "c"]);
|
|
10
|
+
enumSuite
|
|
11
|
+
.add("valid", () => {
|
|
12
|
+
enumSchema.parse("a");
|
|
13
|
+
})
|
|
14
|
+
.add("invalid", () => {
|
|
15
|
+
try {
|
|
16
|
+
enumSchema.parse("x");
|
|
17
|
+
}
|
|
18
|
+
catch (e) { }
|
|
19
|
+
})
|
|
20
|
+
.on("cycle", (e) => {
|
|
21
|
+
console.log(`z.enum: ${e.target}`);
|
|
22
|
+
});
|
|
23
|
+
const undefinedSuite = new benchmark_1.default.Suite("z.undefined");
|
|
24
|
+
const undefinedSchema = index_1.z.undefined();
|
|
25
|
+
undefinedSuite
|
|
26
|
+
.add("valid", () => {
|
|
27
|
+
undefinedSchema.parse(undefined);
|
|
28
|
+
})
|
|
29
|
+
.add("invalid", () => {
|
|
30
|
+
try {
|
|
31
|
+
undefinedSchema.parse(1);
|
|
32
|
+
}
|
|
33
|
+
catch (e) { }
|
|
34
|
+
})
|
|
35
|
+
.on("cycle", (e) => {
|
|
36
|
+
console.log(`z.undefined: ${e.target}`);
|
|
37
|
+
});
|
|
38
|
+
const literalSuite = new benchmark_1.default.Suite("z.literal");
|
|
39
|
+
const short = "short";
|
|
40
|
+
const bad = "bad";
|
|
41
|
+
const literalSchema = index_1.z.literal("short");
|
|
42
|
+
literalSuite
|
|
43
|
+
.add("valid", () => {
|
|
44
|
+
literalSchema.parse(short);
|
|
45
|
+
})
|
|
46
|
+
.add("invalid", () => {
|
|
47
|
+
try {
|
|
48
|
+
literalSchema.parse(bad);
|
|
49
|
+
}
|
|
50
|
+
catch (e) { }
|
|
51
|
+
})
|
|
52
|
+
.on("cycle", (e) => {
|
|
53
|
+
console.log(`z.literal: ${e.target}`);
|
|
54
|
+
});
|
|
55
|
+
const numberSuite = new benchmark_1.default.Suite("z.number");
|
|
56
|
+
const numberSchema = index_1.z.number().int();
|
|
57
|
+
numberSuite
|
|
58
|
+
.add("valid", () => {
|
|
59
|
+
numberSchema.parse(1);
|
|
60
|
+
})
|
|
61
|
+
.add("invalid type", () => {
|
|
62
|
+
try {
|
|
63
|
+
numberSchema.parse("bad");
|
|
64
|
+
}
|
|
65
|
+
catch (e) { }
|
|
66
|
+
})
|
|
67
|
+
.add("invalid number", () => {
|
|
68
|
+
try {
|
|
69
|
+
numberSchema.parse(0.5);
|
|
70
|
+
}
|
|
71
|
+
catch (e) { }
|
|
72
|
+
})
|
|
73
|
+
.on("cycle", (e) => {
|
|
74
|
+
console.log(`z.number: ${e.target}`);
|
|
75
|
+
});
|
|
76
|
+
exports.default = {
|
|
77
|
+
suites: [enumSuite, undefinedSuite, literalSuite, numberSuite],
|
|
78
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const shortSuite = new benchmark_1.default.Suite("realworld");
|
|
9
|
+
const People = index_1.z.array(index_1.z.object({
|
|
10
|
+
type: index_1.z.literal("person"),
|
|
11
|
+
hair: index_1.z.enum(["blue", "brown"]),
|
|
12
|
+
active: index_1.z.boolean(),
|
|
13
|
+
name: index_1.z.string(),
|
|
14
|
+
age: index_1.z.number().int(),
|
|
15
|
+
hobbies: index_1.z.array(index_1.z.string()),
|
|
16
|
+
address: index_1.z.object({
|
|
17
|
+
street: index_1.z.string(),
|
|
18
|
+
zip: index_1.z.string(),
|
|
19
|
+
country: index_1.z.string(),
|
|
20
|
+
}),
|
|
21
|
+
}));
|
|
22
|
+
let i = 0;
|
|
23
|
+
function num() {
|
|
24
|
+
return ++i;
|
|
25
|
+
}
|
|
26
|
+
function str() {
|
|
27
|
+
return (++i % 100).toString(16);
|
|
28
|
+
}
|
|
29
|
+
function array(fn) {
|
|
30
|
+
return Array.from({ length: ++i % 10 }, () => fn());
|
|
31
|
+
}
|
|
32
|
+
const people = Array.from({ length: 100 }, () => {
|
|
33
|
+
return {
|
|
34
|
+
type: "person",
|
|
35
|
+
hair: i % 2 ? "blue" : "brown",
|
|
36
|
+
active: !!(i % 2),
|
|
37
|
+
name: str(),
|
|
38
|
+
age: num(),
|
|
39
|
+
hobbies: array(str),
|
|
40
|
+
address: {
|
|
41
|
+
street: str(),
|
|
42
|
+
zip: str(),
|
|
43
|
+
country: str(),
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
shortSuite
|
|
48
|
+
.add("valid", () => {
|
|
49
|
+
People.parse(people);
|
|
50
|
+
})
|
|
51
|
+
.on("cycle", (e) => {
|
|
52
|
+
console.log(`${shortSuite.name}: ${e.target}`);
|
|
53
|
+
});
|
|
54
|
+
exports.default = {
|
|
55
|
+
suites: [shortSuite],
|
|
56
|
+
};
|
package/lib/benchmarks/string.js
CHANGED
|
@@ -3,41 +3,52 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const SUITE_NAME = "z.string";
|
|
9
|
+
const suite = new benchmark_1.default.Suite(SUITE_NAME);
|
|
10
|
+
const empty = "";
|
|
11
|
+
const short = "short";
|
|
12
|
+
const long = "long".repeat(256);
|
|
13
|
+
const manual = (str) => {
|
|
14
14
|
if (typeof str !== "string") {
|
|
15
15
|
throw new Error("Not a string");
|
|
16
16
|
}
|
|
17
17
|
return str;
|
|
18
18
|
};
|
|
19
|
-
|
|
19
|
+
const stringSchema = index_1.z.string();
|
|
20
|
+
const optionalStringSchema = index_1.z.string().optional();
|
|
21
|
+
const optionalNullableStringSchema = index_1.z.string().optional().nullable();
|
|
20
22
|
suite
|
|
21
|
-
.add("empty string",
|
|
23
|
+
.add("empty string", () => {
|
|
22
24
|
stringSchema.parse(empty);
|
|
23
25
|
})
|
|
24
|
-
.add("short string",
|
|
26
|
+
.add("short string", () => {
|
|
25
27
|
stringSchema.parse(short);
|
|
26
28
|
})
|
|
27
|
-
.add("long string",
|
|
29
|
+
.add("long string", () => {
|
|
28
30
|
stringSchema.parse(long);
|
|
29
31
|
})
|
|
30
|
-
.add("
|
|
32
|
+
.add("optional string", () => {
|
|
33
|
+
optionalStringSchema.parse(long);
|
|
34
|
+
})
|
|
35
|
+
.add("nullable string", () => {
|
|
36
|
+
optionalNullableStringSchema.parse(long);
|
|
37
|
+
})
|
|
38
|
+
.add("nullable (null) string", () => {
|
|
39
|
+
optionalNullableStringSchema.parse(null);
|
|
40
|
+
})
|
|
41
|
+
.add("invalid: null", () => {
|
|
31
42
|
try {
|
|
32
43
|
stringSchema.parse(null);
|
|
33
44
|
}
|
|
34
45
|
catch (err) { }
|
|
35
46
|
})
|
|
36
|
-
.add("manual parser: long",
|
|
47
|
+
.add("manual parser: long", () => {
|
|
37
48
|
manual(long);
|
|
38
49
|
})
|
|
39
|
-
.on("cycle",
|
|
40
|
-
console.log(
|
|
50
|
+
.on("cycle", (e) => {
|
|
51
|
+
console.log(`${SUITE_NAME}: ${e.target}`);
|
|
41
52
|
});
|
|
42
53
|
exports.default = {
|
|
43
54
|
suites: [suite],
|
package/lib/benchmarks/union.js
CHANGED
|
@@ -3,76 +3,76 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const benchmark_1 = __importDefault(require("benchmark"));
|
|
7
|
+
const index_1 = require("../index");
|
|
8
|
+
const doubleSuite = new benchmark_1.default.Suite("z.union: double");
|
|
9
|
+
const manySuite = new benchmark_1.default.Suite("z.union: many");
|
|
10
|
+
const aSchema = index_1.z.object({
|
|
11
11
|
type: index_1.z.literal("a"),
|
|
12
12
|
});
|
|
13
|
-
|
|
13
|
+
const objA = {
|
|
14
14
|
type: "a",
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
const bSchema = index_1.z.object({
|
|
17
17
|
type: index_1.z.literal("b"),
|
|
18
18
|
});
|
|
19
|
-
|
|
19
|
+
const objB = {
|
|
20
20
|
type: "b",
|
|
21
21
|
};
|
|
22
|
-
|
|
22
|
+
const cSchema = index_1.z.object({
|
|
23
23
|
type: index_1.z.literal("c"),
|
|
24
24
|
});
|
|
25
|
-
|
|
25
|
+
const objC = {
|
|
26
26
|
type: "c",
|
|
27
27
|
};
|
|
28
|
-
|
|
28
|
+
const dSchema = index_1.z.object({
|
|
29
29
|
type: index_1.z.literal("d"),
|
|
30
30
|
});
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const double = index_1.z.union([aSchema, bSchema]);
|
|
32
|
+
const many = index_1.z.union([aSchema, bSchema, cSchema, dSchema]);
|
|
33
33
|
doubleSuite
|
|
34
|
-
.add("valid: a",
|
|
34
|
+
.add("valid: a", () => {
|
|
35
35
|
double.parse(objA);
|
|
36
36
|
})
|
|
37
|
-
.add("valid: b",
|
|
37
|
+
.add("valid: b", () => {
|
|
38
38
|
double.parse(objB);
|
|
39
39
|
})
|
|
40
|
-
.add("invalid: null",
|
|
40
|
+
.add("invalid: null", () => {
|
|
41
41
|
try {
|
|
42
42
|
double.parse(null);
|
|
43
43
|
}
|
|
44
44
|
catch (err) { }
|
|
45
45
|
})
|
|
46
|
-
.add("invalid: wrong shape",
|
|
46
|
+
.add("invalid: wrong shape", () => {
|
|
47
47
|
try {
|
|
48
48
|
double.parse(objC);
|
|
49
49
|
}
|
|
50
50
|
catch (err) { }
|
|
51
51
|
})
|
|
52
|
-
.on("cycle",
|
|
53
|
-
console.log(
|
|
52
|
+
.on("cycle", (e) => {
|
|
53
|
+
console.log(`${doubleSuite.name}: ${e.target}`);
|
|
54
54
|
});
|
|
55
55
|
manySuite
|
|
56
|
-
.add("valid: a",
|
|
56
|
+
.add("valid: a", () => {
|
|
57
57
|
many.parse(objA);
|
|
58
58
|
})
|
|
59
|
-
.add("valid: c",
|
|
59
|
+
.add("valid: c", () => {
|
|
60
60
|
many.parse(objC);
|
|
61
61
|
})
|
|
62
|
-
.add("invalid: null",
|
|
62
|
+
.add("invalid: null", () => {
|
|
63
63
|
try {
|
|
64
64
|
many.parse(null);
|
|
65
65
|
}
|
|
66
66
|
catch (err) { }
|
|
67
67
|
})
|
|
68
|
-
.add("invalid: wrong shape",
|
|
68
|
+
.add("invalid: wrong shape", () => {
|
|
69
69
|
try {
|
|
70
70
|
many.parse({ type: "unknown" });
|
|
71
71
|
}
|
|
72
72
|
catch (err) { }
|
|
73
73
|
})
|
|
74
|
-
.on("cycle",
|
|
75
|
-
console.log(
|
|
74
|
+
.on("cycle", (e) => {
|
|
75
|
+
console.log(`${manySuite.name}: ${e.target}`);
|
|
76
76
|
});
|
|
77
77
|
exports.default = {
|
|
78
78
|
suites: [doubleSuite, manySuite],
|
package/lib/helpers/errorUtil.js
CHANGED
|
@@ -3,10 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.errorUtil = void 0;
|
|
4
4
|
var errorUtil;
|
|
5
5
|
(function (errorUtil) {
|
|
6
|
-
errorUtil.errToObj =
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
errorUtil.toString = function (message) {
|
|
10
|
-
return typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
11
|
-
};
|
|
6
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
7
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
12
8
|
})(errorUtil = exports.errorUtil || (exports.errorUtil = {}));
|
|
@@ -22,7 +22,7 @@ export declare const ZodParsedType: {
|
|
|
22
22
|
set: "set";
|
|
23
23
|
};
|
|
24
24
|
export declare type ZodParsedType = keyof typeof ZodParsedType;
|
|
25
|
-
export declare const getParsedType: (data: any
|
|
25
|
+
export declare const getParsedType: (data: any) => ZodParsedType;
|
|
26
26
|
export declare const makeIssue: (params: {
|
|
27
27
|
data: any;
|
|
28
28
|
path: (string | number)[];
|
|
@@ -38,13 +38,15 @@ export declare type ParsePathComponent = string | number;
|
|
|
38
38
|
export declare type ParsePath = ParsePathComponent[];
|
|
39
39
|
export declare const EMPTY_PATH: ParsePath;
|
|
40
40
|
export interface ParseContext {
|
|
41
|
+
readonly common: {
|
|
42
|
+
readonly issues: ZodIssue[];
|
|
43
|
+
readonly contextualErrorMap?: ZodErrorMap;
|
|
44
|
+
readonly async: boolean;
|
|
45
|
+
readonly typeCache: Map<any, ZodParsedType> | undefined;
|
|
46
|
+
};
|
|
41
47
|
readonly path: ParsePath;
|
|
42
|
-
readonly issues: ZodIssue[];
|
|
43
48
|
readonly schemaErrorMap?: ZodErrorMap;
|
|
44
|
-
readonly contextualErrorMap?: ZodErrorMap;
|
|
45
|
-
readonly async: boolean;
|
|
46
49
|
readonly parent: ParseContext | null;
|
|
47
|
-
readonly typeCache: Map<any, ZodParsedType> | undefined;
|
|
48
50
|
readonly data: any;
|
|
49
51
|
readonly parsedType: ZodParsedType;
|
|
50
52
|
}
|