zod 3.14.0 → 3.14.1
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/lib/benchmarks/index.js +2 -0
- package/lib/benchmarks/primitives.d.ts +5 -0
- package/lib/benchmarks/primitives.js +78 -0
- package/lib/helpers/parseUtil.d.ts +6 -3
- package/lib/helpers/parseUtil.js +2 -2
- package/lib/index.mjs +152 -99
- package/lib/types.d.ts +2 -0
- package/lib/types.js +150 -97
- package/package.json +1 -1
package/lib/benchmarks/index.js
CHANGED
|
@@ -5,11 +5,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const discriminatedUnion_1 = __importDefault(require("./discriminatedUnion"));
|
|
7
7
|
const object_1 = __importDefault(require("./object"));
|
|
8
|
+
const primitives_1 = __importDefault(require("./primitives"));
|
|
8
9
|
const realworld_1 = __importDefault(require("./realworld"));
|
|
9
10
|
const string_1 = __importDefault(require("./string"));
|
|
10
11
|
const union_1 = __importDefault(require("./union"));
|
|
11
12
|
for (const suite of [
|
|
12
13
|
...realworld_1.default.suites,
|
|
14
|
+
...primitives_1.default.suites,
|
|
13
15
|
...string_1.default.suites,
|
|
14
16
|
...object_1.default.suites,
|
|
15
17
|
...union_1.default.suites,
|
|
@@ -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
|
+
};
|
|
@@ -38,11 +38,14 @@ 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
50
|
readonly data: any;
|
|
48
51
|
readonly parsedType: ZodParsedType;
|
package/lib/helpers/parseUtil.js
CHANGED
|
@@ -97,13 +97,13 @@ function addIssueToContext(ctx, issueData) {
|
|
|
97
97
|
data: ctx.data,
|
|
98
98
|
path: ctx.path,
|
|
99
99
|
errorMaps: [
|
|
100
|
-
ctx.contextualErrorMap,
|
|
100
|
+
ctx.common.contextualErrorMap,
|
|
101
101
|
ctx.schemaErrorMap,
|
|
102
102
|
ZodError_1.overrideErrorMap,
|
|
103
103
|
ZodError_1.defaultErrorMap, // then global default map
|
|
104
104
|
].filter((x) => !!x),
|
|
105
105
|
});
|
|
106
|
-
ctx.issues.push(issue);
|
|
106
|
+
ctx.common.issues.push(issue);
|
|
107
107
|
}
|
|
108
108
|
exports.addIssueToContext = addIssueToContext;
|
|
109
109
|
class ParseStatus {
|