zod 3.13.4 → 3.14.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/README.md +4 -3
- package/lib/ZodError.js +110 -205
- package/lib/benchmarks/discriminatedUnion.js +25 -25
- package/lib/benchmarks/index.js +13 -53
- package/lib/benchmarks/object.js +23 -23
- package/lib/benchmarks/realworld.d.ts +5 -0
- package/lib/benchmarks/realworld.js +56 -0
- package/lib/benchmarks/string.js +16 -16
- package/lib/benchmarks/union.js +25 -25
- package/lib/helpers/errorUtil.js +2 -6
- package/lib/helpers/parseUtil.d.ts +1 -2
- package/lib/helpers/parseUtil.js +82 -242
- package/lib/helpers/util.js +20 -66
- package/lib/index.js +1 -1
- package/lib/index.mjs +1527 -2101
- package/lib/types.d.ts +1 -1
- package/lib/types.js +1318 -1711
- package/package.json +2 -1
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,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,41 @@ 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
20
|
suite
|
|
21
|
-
.add("empty string",
|
|
21
|
+
.add("empty string", () => {
|
|
22
22
|
stringSchema.parse(empty);
|
|
23
23
|
})
|
|
24
|
-
.add("short string",
|
|
24
|
+
.add("short string", () => {
|
|
25
25
|
stringSchema.parse(short);
|
|
26
26
|
})
|
|
27
|
-
.add("long string",
|
|
27
|
+
.add("long string", () => {
|
|
28
28
|
stringSchema.parse(long);
|
|
29
29
|
})
|
|
30
|
-
.add("invalid: null",
|
|
30
|
+
.add("invalid: null", () => {
|
|
31
31
|
try {
|
|
32
32
|
stringSchema.parse(null);
|
|
33
33
|
}
|
|
34
34
|
catch (err) { }
|
|
35
35
|
})
|
|
36
|
-
.add("manual parser: long",
|
|
36
|
+
.add("manual parser: long", () => {
|
|
37
37
|
manual(long);
|
|
38
38
|
})
|
|
39
|
-
.on("cycle",
|
|
40
|
-
console.log(
|
|
39
|
+
.on("cycle", (e) => {
|
|
40
|
+
console.log(`${SUITE_NAME}: ${e.target}`);
|
|
41
41
|
});
|
|
42
42
|
exports.default = {
|
|
43
43
|
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)[];
|
|
@@ -44,7 +44,6 @@ export interface ParseContext {
|
|
|
44
44
|
readonly contextualErrorMap?: ZodErrorMap;
|
|
45
45
|
readonly async: boolean;
|
|
46
46
|
readonly parent: ParseContext | null;
|
|
47
|
-
readonly typeCache: Map<any, ZodParsedType> | undefined;
|
|
48
47
|
readonly data: any;
|
|
49
48
|
readonly parsedType: ZodParsedType;
|
|
50
49
|
}
|