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/README.md
CHANGED
|
@@ -30,9 +30,9 @@ These docs have been translated into [Chinese](./README_ZH.md).
|
|
|
30
30
|
|
|
31
31
|
# Table of contents
|
|
32
32
|
|
|
33
|
-
The full documentation is available both on the [official documentation site](https://zod.js.org/) (recommended) and in `README.md`.
|
|
33
|
+
<!-- The full documentation is available both on the [official documentation site](https://zod.js.org/) (recommended) and in `README.md`.
|
|
34
34
|
|
|
35
|
-
### Go to [zod.js.org](https://zod.js.org) >>
|
|
35
|
+
### Go to [zod.js.org](https://zod.js.org) >> -->
|
|
36
36
|
|
|
37
37
|
- [What is Zod](#what-is-zod)
|
|
38
38
|
- [Installation](#installation)
|
|
@@ -114,7 +114,7 @@ Zod is designed to be as developer-friendly as possible. The goal is to eliminat
|
|
|
114
114
|
Some other great aspects:
|
|
115
115
|
|
|
116
116
|
- Zero dependencies
|
|
117
|
-
- Works in Node.js and
|
|
117
|
+
- Works in Node.js and all modern browsers
|
|
118
118
|
- Tiny: 8kb minified + zipped
|
|
119
119
|
- Immutable: methods (i.e. `.optional()`) return a new instance
|
|
120
120
|
- Concise, chainable interface
|
|
@@ -246,6 +246,7 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
246
246
|
|
|
247
247
|
- [`react-hook-form`](https://github.com/react-hook-form/resolvers#zod): A first-party Zod resolver for React Hook Form
|
|
248
248
|
- [`zod-formik-adapter`](https://github.com/robertLichtnow/zod-formik-adapter): A community-maintained Formik adapter for Zod
|
|
249
|
+
- [`react-zorm`](https://github.com/esamattis/react-zorm): Standalone `<form>` generation and validation for React using Zod
|
|
249
250
|
|
|
250
251
|
# Basic usage
|
|
251
252
|
|
package/lib/ZodError.js
CHANGED
|
@@ -1,58 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __values = (this && this.__values) || function(o) {
|
|
18
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
19
|
-
if (m) return m.call(o);
|
|
20
|
-
if (o && typeof o.length === "number") return {
|
|
21
|
-
next: function () {
|
|
22
|
-
if (o && i >= o.length) o = void 0;
|
|
23
|
-
return { value: o && o[i++], done: !o };
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
27
|
-
};
|
|
28
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
29
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
30
|
-
if (!m) return o;
|
|
31
|
-
var i = m.call(o), r, ar = [], e;
|
|
32
|
-
try {
|
|
33
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
34
|
-
}
|
|
35
|
-
catch (error) { e = { error: error }; }
|
|
36
|
-
finally {
|
|
37
|
-
try {
|
|
38
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
39
|
-
}
|
|
40
|
-
finally { if (e) throw e.error; }
|
|
41
|
-
}
|
|
42
|
-
return ar;
|
|
43
|
-
};
|
|
44
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
45
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
46
|
-
if (ar || !(i in from)) {
|
|
47
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
48
|
-
ar[i] = from[i];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
52
|
-
};
|
|
53
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
3
|
exports.setErrorMap = exports.overrideErrorMap = exports.defaultErrorMap = exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;
|
|
55
|
-
|
|
4
|
+
const util_1 = require("./helpers/util");
|
|
56
5
|
exports.ZodIssueCode = util_1.util.arrayToEnum([
|
|
57
6
|
"invalid_type",
|
|
58
7
|
"custom",
|
|
@@ -69,238 +18,194 @@ exports.ZodIssueCode = util_1.util.arrayToEnum([
|
|
|
69
18
|
"invalid_intersection_types",
|
|
70
19
|
"not_multiple_of",
|
|
71
20
|
]);
|
|
72
|
-
|
|
73
|
-
|
|
21
|
+
const quotelessJson = (obj) => {
|
|
22
|
+
const json = JSON.stringify(obj, null, 2);
|
|
74
23
|
return json.replace(/"([^"]+)":/g, "$1:");
|
|
75
24
|
};
|
|
76
25
|
exports.quotelessJson = quotelessJson;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
while (i < issue.path.length) {
|
|
106
|
-
var el = issue.path[i];
|
|
107
|
-
var terminal = i === issue.path.length - 1;
|
|
108
|
-
if (!terminal) {
|
|
109
|
-
if (typeof el === "string") {
|
|
110
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
111
|
-
}
|
|
112
|
-
else if (typeof el === "number") {
|
|
113
|
-
var errorArray = [];
|
|
114
|
-
errorArray._errors = [];
|
|
115
|
-
curr[el] = curr[el] || errorArray;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
26
|
+
class ZodError extends Error {
|
|
27
|
+
constructor(issues) {
|
|
28
|
+
super();
|
|
29
|
+
this.issues = [];
|
|
30
|
+
this.format = () => {
|
|
31
|
+
const fieldErrors = { _errors: [] };
|
|
32
|
+
const processError = (error) => {
|
|
33
|
+
for (const issue of error.issues) {
|
|
34
|
+
if (issue.code === "invalid_union") {
|
|
35
|
+
issue.unionErrors.map(processError);
|
|
36
|
+
}
|
|
37
|
+
else if (issue.code === "invalid_return_type") {
|
|
38
|
+
processError(issue.returnTypeError);
|
|
39
|
+
}
|
|
40
|
+
else if (issue.code === "invalid_arguments") {
|
|
41
|
+
processError(issue.argumentsError);
|
|
42
|
+
}
|
|
43
|
+
else if (issue.path.length === 0) {
|
|
44
|
+
fieldErrors._errors.push(issue.message);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
let curr = fieldErrors;
|
|
48
|
+
let i = 0;
|
|
49
|
+
while (i < issue.path.length) {
|
|
50
|
+
const el = issue.path[i];
|
|
51
|
+
const terminal = i === issue.path.length - 1;
|
|
52
|
+
if (!terminal) {
|
|
53
|
+
if (typeof el === "string") {
|
|
119
54
|
curr[el] = curr[el] || { _errors: [] };
|
|
120
|
-
curr[el]._errors.push(issue.message);
|
|
121
55
|
}
|
|
122
|
-
|
|
123
|
-
|
|
56
|
+
else if (typeof el === "number") {
|
|
57
|
+
const errorArray = [];
|
|
58
|
+
errorArray._errors = [];
|
|
59
|
+
curr[el] = curr[el] || errorArray;
|
|
60
|
+
}
|
|
124
61
|
}
|
|
62
|
+
else {
|
|
63
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
64
|
+
curr[el]._errors.push(issue.message);
|
|
65
|
+
}
|
|
66
|
+
curr = curr[el];
|
|
67
|
+
i++;
|
|
125
68
|
}
|
|
126
69
|
}
|
|
127
70
|
}
|
|
128
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
129
|
-
finally {
|
|
130
|
-
try {
|
|
131
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
132
|
-
}
|
|
133
|
-
finally { if (e_1) throw e_1.error; }
|
|
134
|
-
}
|
|
135
71
|
};
|
|
136
|
-
processError(
|
|
72
|
+
processError(this);
|
|
137
73
|
return fieldErrors;
|
|
138
74
|
};
|
|
139
|
-
|
|
140
|
-
|
|
75
|
+
this.addIssue = (sub) => {
|
|
76
|
+
this.issues = [...this.issues, sub];
|
|
141
77
|
};
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
_this.issues = __spreadArray(__spreadArray([], __read(_this.issues), false), __read(subs), false);
|
|
78
|
+
this.addIssues = (subs = []) => {
|
|
79
|
+
this.issues = [...this.issues, ...subs];
|
|
145
80
|
};
|
|
146
|
-
|
|
81
|
+
const actualProto = new.target.prototype;
|
|
147
82
|
if (Object.setPrototypeOf) {
|
|
148
83
|
// eslint-disable-next-line ban/ban
|
|
149
|
-
Object.setPrototypeOf(
|
|
84
|
+
Object.setPrototypeOf(this, actualProto);
|
|
150
85
|
}
|
|
151
86
|
else {
|
|
152
|
-
|
|
87
|
+
this.__proto__ = actualProto;
|
|
153
88
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
89
|
+
this.name = "ZodError";
|
|
90
|
+
this.issues = issues;
|
|
91
|
+
}
|
|
92
|
+
get errors() {
|
|
93
|
+
return this.issues;
|
|
157
94
|
}
|
|
158
|
-
|
|
159
|
-
get: function () {
|
|
160
|
-
return this.issues;
|
|
161
|
-
},
|
|
162
|
-
enumerable: false,
|
|
163
|
-
configurable: true
|
|
164
|
-
});
|
|
165
|
-
ZodError.prototype.toString = function () {
|
|
95
|
+
toString() {
|
|
166
96
|
return this.message;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
});
|
|
182
|
-
ZodError.prototype.flatten = function (mapper) {
|
|
183
|
-
var e_2, _a;
|
|
184
|
-
if (mapper === void 0) { mapper = function (issue) { return issue.message; }; }
|
|
185
|
-
var fieldErrors = {};
|
|
186
|
-
var formErrors = [];
|
|
187
|
-
try {
|
|
188
|
-
for (var _b = __values(this.issues), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
189
|
-
var sub = _c.value;
|
|
190
|
-
if (sub.path.length > 0) {
|
|
191
|
-
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
192
|
-
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
193
|
-
}
|
|
194
|
-
else {
|
|
195
|
-
formErrors.push(mapper(sub));
|
|
196
|
-
}
|
|
97
|
+
}
|
|
98
|
+
get message() {
|
|
99
|
+
return JSON.stringify(this.issues, null, 2);
|
|
100
|
+
}
|
|
101
|
+
get isEmpty() {
|
|
102
|
+
return this.issues.length === 0;
|
|
103
|
+
}
|
|
104
|
+
flatten(mapper = (issue) => issue.message) {
|
|
105
|
+
const fieldErrors = {};
|
|
106
|
+
const formErrors = [];
|
|
107
|
+
for (const sub of this.issues) {
|
|
108
|
+
if (sub.path.length > 0) {
|
|
109
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
110
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
197
111
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
finally {
|
|
201
|
-
try {
|
|
202
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
112
|
+
else {
|
|
113
|
+
formErrors.push(mapper(sub));
|
|
203
114
|
}
|
|
204
|
-
finally { if (e_2) throw e_2.error; }
|
|
205
115
|
}
|
|
206
|
-
return { formErrors
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
enumerable: false,
|
|
213
|
-
configurable: true
|
|
214
|
-
});
|
|
215
|
-
ZodError.create = function (issues) {
|
|
216
|
-
var error = new ZodError(issues);
|
|
217
|
-
return error;
|
|
218
|
-
};
|
|
219
|
-
return ZodError;
|
|
220
|
-
}(Error));
|
|
116
|
+
return { formErrors, fieldErrors };
|
|
117
|
+
}
|
|
118
|
+
get formErrors() {
|
|
119
|
+
return this.flatten();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
221
122
|
exports.ZodError = ZodError;
|
|
222
|
-
|
|
223
|
-
|
|
123
|
+
ZodError.create = (issues) => {
|
|
124
|
+
const error = new ZodError(issues);
|
|
125
|
+
return error;
|
|
126
|
+
};
|
|
127
|
+
const defaultErrorMap = (issue, _ctx) => {
|
|
128
|
+
let message;
|
|
224
129
|
switch (issue.code) {
|
|
225
130
|
case exports.ZodIssueCode.invalid_type:
|
|
226
131
|
if (issue.received === "undefined") {
|
|
227
132
|
message = "Required";
|
|
228
133
|
}
|
|
229
134
|
else {
|
|
230
|
-
message =
|
|
135
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
231
136
|
}
|
|
232
137
|
break;
|
|
233
138
|
case exports.ZodIssueCode.unrecognized_keys:
|
|
234
|
-
message =
|
|
235
|
-
.map(
|
|
236
|
-
.join(", ")
|
|
139
|
+
message = `Unrecognized key(s) in object: ${issue.keys
|
|
140
|
+
.map((k) => `'${k}'`)
|
|
141
|
+
.join(", ")}`;
|
|
237
142
|
break;
|
|
238
143
|
case exports.ZodIssueCode.invalid_union:
|
|
239
|
-
message =
|
|
144
|
+
message = `Invalid input`;
|
|
240
145
|
break;
|
|
241
146
|
case exports.ZodIssueCode.invalid_union_discriminator:
|
|
242
|
-
message =
|
|
243
|
-
.map(
|
|
244
|
-
.join(" | ")
|
|
147
|
+
message = `Invalid discriminator value. Expected ${issue.options
|
|
148
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
149
|
+
.join(" | ")}`;
|
|
245
150
|
break;
|
|
246
151
|
case exports.ZodIssueCode.invalid_enum_value:
|
|
247
|
-
message =
|
|
248
|
-
.map(
|
|
249
|
-
.join(" | ")
|
|
152
|
+
message = `Invalid enum value. Expected ${issue.options
|
|
153
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
154
|
+
.join(" | ")}`;
|
|
250
155
|
break;
|
|
251
156
|
case exports.ZodIssueCode.invalid_arguments:
|
|
252
|
-
message =
|
|
157
|
+
message = `Invalid function arguments`;
|
|
253
158
|
break;
|
|
254
159
|
case exports.ZodIssueCode.invalid_return_type:
|
|
255
|
-
message =
|
|
160
|
+
message = `Invalid function return type`;
|
|
256
161
|
break;
|
|
257
162
|
case exports.ZodIssueCode.invalid_date:
|
|
258
|
-
message =
|
|
163
|
+
message = `Invalid date`;
|
|
259
164
|
break;
|
|
260
165
|
case exports.ZodIssueCode.invalid_string:
|
|
261
166
|
if (issue.validation !== "regex")
|
|
262
|
-
message =
|
|
167
|
+
message = `Invalid ${issue.validation}`;
|
|
263
168
|
else
|
|
264
169
|
message = "Invalid";
|
|
265
170
|
break;
|
|
266
171
|
case exports.ZodIssueCode.too_small:
|
|
267
172
|
if (issue.type === "array")
|
|
268
|
-
message =
|
|
173
|
+
message = `Array must contain ${issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
269
174
|
else if (issue.type === "string")
|
|
270
|
-
message =
|
|
175
|
+
message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
271
176
|
else if (issue.type === "number")
|
|
272
|
-
message =
|
|
177
|
+
message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
|
|
273
178
|
else
|
|
274
179
|
message = "Invalid input";
|
|
275
180
|
break;
|
|
276
181
|
case exports.ZodIssueCode.too_big:
|
|
277
182
|
if (issue.type === "array")
|
|
278
|
-
message =
|
|
183
|
+
message = `Array must contain ${issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
279
184
|
else if (issue.type === "string")
|
|
280
|
-
message =
|
|
185
|
+
message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
281
186
|
else if (issue.type === "number")
|
|
282
|
-
message =
|
|
187
|
+
message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
|
|
283
188
|
else
|
|
284
189
|
message = "Invalid input";
|
|
285
190
|
break;
|
|
286
191
|
case exports.ZodIssueCode.custom:
|
|
287
|
-
message =
|
|
192
|
+
message = `Invalid input`;
|
|
288
193
|
break;
|
|
289
194
|
case exports.ZodIssueCode.invalid_intersection_types:
|
|
290
|
-
message =
|
|
195
|
+
message = `Intersection results could not be merged`;
|
|
291
196
|
break;
|
|
292
197
|
case exports.ZodIssueCode.not_multiple_of:
|
|
293
|
-
message =
|
|
198
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
294
199
|
break;
|
|
295
200
|
default:
|
|
296
201
|
message = _ctx.defaultError;
|
|
297
202
|
util_1.util.assertNever(issue);
|
|
298
203
|
}
|
|
299
|
-
return { message
|
|
204
|
+
return { message };
|
|
300
205
|
};
|
|
301
206
|
exports.defaultErrorMap = defaultErrorMap;
|
|
302
207
|
exports.overrideErrorMap = exports.defaultErrorMap;
|
|
303
|
-
|
|
208
|
+
const setErrorMap = (map) => {
|
|
304
209
|
exports.overrideErrorMap = map;
|
|
305
210
|
};
|
|
306
211
|
exports.setErrorMap = setErrorMap;
|
|
@@ -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.discriminatedUnion: double");
|
|
9
|
+
const manySuite = new benchmark_1.default.Suite("z.discriminatedUnion: 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.discriminatedUnion("type", [aSchema, bSchema]);
|
|
32
|
+
const many = index_1.z.discriminatedUnion("type", [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/benchmarks/index.js
CHANGED
|
@@ -1,59 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
-
if (!m) return o;
|
|
5
|
-
var i = m.call(o), r, ar = [], e;
|
|
6
|
-
try {
|
|
7
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
-
}
|
|
9
|
-
catch (error) { e = { error: error }; }
|
|
10
|
-
finally {
|
|
11
|
-
try {
|
|
12
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
-
}
|
|
14
|
-
finally { if (e) throw e.error; }
|
|
15
|
-
}
|
|
16
|
-
return ar;
|
|
17
|
-
};
|
|
18
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
-
if (ar || !(i in from)) {
|
|
21
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
-
ar[i] = from[i];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
-
};
|
|
27
|
-
var __values = (this && this.__values) || function(o) {
|
|
28
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
29
|
-
if (m) return m.call(o);
|
|
30
|
-
if (o && typeof o.length === "number") return {
|
|
31
|
-
next: function () {
|
|
32
|
-
if (o && i >= o.length) o = void 0;
|
|
33
|
-
return { value: o && o[i++], done: !o };
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
37
|
-
};
|
|
38
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
|
-
var e_1, _a;
|
|
42
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
57
|
-
}
|
|
58
|
-
finally { if (e_1) throw e_1.error; }
|
|
6
|
+
const discriminatedUnion_1 = __importDefault(require("./discriminatedUnion"));
|
|
7
|
+
const object_1 = __importDefault(require("./object"));
|
|
8
|
+
const realworld_1 = __importDefault(require("./realworld"));
|
|
9
|
+
const string_1 = __importDefault(require("./string"));
|
|
10
|
+
const union_1 = __importDefault(require("./union"));
|
|
11
|
+
for (const suite of [
|
|
12
|
+
...realworld_1.default.suites,
|
|
13
|
+
...string_1.default.suites,
|
|
14
|
+
...object_1.default.suites,
|
|
15
|
+
...union_1.default.suites,
|
|
16
|
+
...discriminatedUnion_1.default.suites,
|
|
17
|
+
]) {
|
|
18
|
+
suite.run();
|
|
59
19
|
}
|