zod 3.9.7 → 3.10.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/README.md +4 -2
- package/lib/ZodError.d.ts +1 -0
- package/lib/ZodError.d.ts.map +1 -1
- package/lib/ZodError.js +115 -195
- package/lib/ZodError.js.map +1 -1
- package/lib/benchmarks/index.js +9 -47
- package/lib/benchmarks/index.js.map +1 -1
- package/lib/benchmarks/object.js +23 -23
- package/lib/benchmarks/object.js.map +1 -1
- package/lib/benchmarks/string.js +16 -16
- package/lib/benchmarks/string.js.map +1 -1
- package/lib/benchmarks/union.js +25 -25
- package/lib/benchmarks/union.js.map +1 -1
- package/lib/helpers/errorUtil.d.ts +2 -6
- package/lib/helpers/errorUtil.d.ts.map +1 -1
- package/lib/helpers/errorUtil.js +2 -6
- package/lib/helpers/errorUtil.js.map +1 -1
- package/lib/helpers/parseUtil.d.ts +49 -38
- package/lib/helpers/parseUtil.d.ts.map +1 -1
- package/lib/helpers/parseUtil.js +126 -172
- package/lib/helpers/parseUtil.js.map +1 -1
- package/lib/helpers/util.js +20 -66
- package/lib/helpers/util.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1 -1
- package/lib/index.mjs.map +1 -1
- package/lib/types.d.ts +51 -62
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +1578 -1801
- package/lib/types.js.map +1 -1
- package/package.json +2 -2
- package/CHANGELOG.md +0 -247
- package/lib/.DS_Store +0 -0
package/lib/types.js
CHANGED
|
@@ -1,145 +1,33 @@
|
|
|
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
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
15
|
-
var __assign = (this && this.__assign) || function () {
|
|
16
|
-
__assign = Object.assign || function(t) {
|
|
17
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
18
|
-
s = arguments[i];
|
|
19
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
20
|
-
t[p] = s[p];
|
|
21
|
-
}
|
|
22
|
-
return t;
|
|
23
|
-
};
|
|
24
|
-
return __assign.apply(this, arguments);
|
|
25
|
-
};
|
|
26
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
27
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
28
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
29
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
30
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
31
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
32
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
33
|
-
});
|
|
34
|
-
};
|
|
35
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
36
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
37
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
38
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
39
|
-
function step(op) {
|
|
40
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
41
|
-
while (_) try {
|
|
42
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
43
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
44
|
-
switch (op[0]) {
|
|
45
|
-
case 0: case 1: t = op; break;
|
|
46
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
47
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
48
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
49
|
-
default:
|
|
50
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
51
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
52
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
53
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
54
|
-
if (t[2]) _.ops.pop();
|
|
55
|
-
_.trys.pop(); continue;
|
|
56
|
-
}
|
|
57
|
-
op = body.call(thisArg, _);
|
|
58
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
59
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
var __values = (this && this.__values) || function(o) {
|
|
63
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
64
|
-
if (m) return m.call(o);
|
|
65
|
-
if (o && typeof o.length === "number") return {
|
|
66
|
-
next: function () {
|
|
67
|
-
if (o && i >= o.length) o = void 0;
|
|
68
|
-
return { value: o && o[i++], done: !o };
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
72
|
-
};
|
|
73
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
74
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
75
|
-
if (!m) return o;
|
|
76
|
-
var i = m.call(o), r, ar = [], e;
|
|
77
|
-
try {
|
|
78
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
79
|
-
}
|
|
80
|
-
catch (error) { e = { error: error }; }
|
|
81
|
-
finally {
|
|
82
|
-
try {
|
|
83
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
84
|
-
}
|
|
85
|
-
finally { if (e) throw e.error; }
|
|
86
|
-
}
|
|
87
|
-
return ar;
|
|
88
|
-
};
|
|
89
|
-
var __spread = (this && this.__spread) || function () {
|
|
90
|
-
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
91
|
-
return ar;
|
|
92
|
-
};
|
|
93
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
94
|
-
exports.literal = exports.lazy = exports.intersection = exports.instanceof = exports.function = exports.enum = exports.effect = exports.date = exports.boolean = exports.bigint = exports.array = exports.any = exports.ZodFirstPartyTypeKind = exports.late = exports.ZodSchema = exports.Schema = exports.custom = exports.ZodDefault = exports.ZodNullable = exports.ZodOptional = exports.ZodTransformer = exports.ZodEffects = exports.ZodPromise = exports.ZodNativeEnum = exports.ZodEnum = exports.ZodLiteral = exports.ZodLazy = exports.ZodFunction = exports.ZodSet = exports.ZodMap = exports.ZodRecord = exports.ZodTuple = exports.ZodIntersection = exports.ZodUnion = exports.ZodObject = exports.
|
|
95
|
-
exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum =
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// if (iss.code === ZodIssueCode.invalid_type) {
|
|
104
|
-
// return { message: arg };
|
|
105
|
-
// }
|
|
106
|
-
// return { message: ctx.defaultError };
|
|
107
|
-
// };
|
|
108
|
-
// }
|
|
109
|
-
// }
|
|
110
|
-
var createRootContext = function (params) {
|
|
111
|
-
var _a;
|
|
112
|
-
return new parseUtil_1.ParseContext({
|
|
113
|
-
path: parseUtil_1.pathFromArray(params.path || []),
|
|
114
|
-
issues: [],
|
|
115
|
-
errorMap: params.errorMap,
|
|
116
|
-
async: (_a = params.async) !== null && _a !== void 0 ? _a : false,
|
|
117
|
-
});
|
|
118
|
-
};
|
|
119
|
-
var handleResult = function (ctx, result) {
|
|
120
|
-
if (parseUtil_1.isOk(result) && !ctx.issues.length) {
|
|
3
|
+
exports.map = exports.literal = exports.lazy = exports.intersection = exports.instanceof = exports.function = exports.enum = exports.effect = exports.date = exports.boolean = exports.bigint = exports.array = exports.any = exports.ZodFirstPartyTypeKind = exports.late = exports.ZodSchema = exports.Schema = exports.custom = exports.ZodDefault = exports.ZodNullable = exports.ZodOptional = exports.ZodTransformer = exports.ZodEffects = exports.ZodPromise = exports.ZodNativeEnum = exports.ZodEnum = exports.ZodLiteral = exports.ZodLazy = exports.ZodFunction = exports.ZodSet = exports.ZodMap = exports.ZodRecord = exports.ZodTuple = exports.ZodIntersection = exports.ZodUnion = exports.ZodObject = exports.objectUtil = exports.ZodArray = exports.ZodVoid = exports.ZodNever = exports.ZodUnknown = exports.ZodAny = exports.ZodNull = exports.ZodUndefined = exports.ZodDate = exports.ZodBoolean = exports.ZodBigInt = exports.ZodNumber = exports.ZodString = exports.ZodType = void 0;
|
|
4
|
+
exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum = void 0;
|
|
5
|
+
const _1 = require(".");
|
|
6
|
+
const errorUtil_1 = require("./helpers/errorUtil");
|
|
7
|
+
const parseUtil_1 = require("./helpers/parseUtil");
|
|
8
|
+
const util_1 = require("./helpers/util");
|
|
9
|
+
const ZodError_1 = require("./ZodError");
|
|
10
|
+
const handleResult = (ctx, result) => {
|
|
11
|
+
if ((0, parseUtil_1.isValid)(result)) {
|
|
121
12
|
return { success: true, data: result.value };
|
|
122
13
|
}
|
|
123
14
|
else {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
// const error = new ZodError(ctx.issues);
|
|
131
|
-
// return { success: false, error };
|
|
132
|
-
// }
|
|
15
|
+
if (!ctx.issues.length) {
|
|
16
|
+
throw new Error("Validation failed but no issues detected.");
|
|
17
|
+
}
|
|
18
|
+
const error = new ZodError_1.ZodError(ctx.issues);
|
|
19
|
+
return { success: false, error };
|
|
20
|
+
}
|
|
133
21
|
};
|
|
134
22
|
function processCreateParams(params) {
|
|
135
23
|
if (!params)
|
|
136
24
|
return {};
|
|
137
25
|
if (params.errorMap && (params.invalid_type_error || params.required_error)) {
|
|
138
|
-
throw new Error(
|
|
26
|
+
throw new Error(`Can't use "invalid" or "required" in conjunction with custom error map.`);
|
|
139
27
|
}
|
|
140
28
|
if (params.errorMap)
|
|
141
29
|
return { errorMap: params.errorMap };
|
|
142
|
-
|
|
30
|
+
const customMap = (iss, ctx) => {
|
|
143
31
|
if (iss.code !== "invalid_type")
|
|
144
32
|
return { message: ctx.defaultError };
|
|
145
33
|
if (typeof ctx.data === "undefined" && params.required_error)
|
|
@@ -150,8 +38,8 @@ function processCreateParams(params) {
|
|
|
150
38
|
};
|
|
151
39
|
return { errorMap: customMap };
|
|
152
40
|
}
|
|
153
|
-
|
|
154
|
-
|
|
41
|
+
class ZodType {
|
|
42
|
+
constructor(def) {
|
|
155
43
|
/** Alias of safeParseAsync */
|
|
156
44
|
this.spa = this.safeParseAsync;
|
|
157
45
|
this.superRefine = this._refinement;
|
|
@@ -159,73 +47,79 @@ var ZodType = /** @class */ (function () {
|
|
|
159
47
|
this.transform = this.transform.bind(this);
|
|
160
48
|
this.default = this.default.bind(this);
|
|
161
49
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
50
|
+
_processInputParams(input) {
|
|
51
|
+
return {
|
|
52
|
+
status: new parseUtil_1.ParseStatus(),
|
|
53
|
+
ctx: {
|
|
54
|
+
...input.parent,
|
|
55
|
+
data: input.data,
|
|
56
|
+
parsedType: (0, parseUtil_1.getParsedType)(input.data, input.parent.typeCache),
|
|
57
|
+
schemaErrorMap: this._def.errorMap,
|
|
58
|
+
path: input.path,
|
|
59
|
+
parent: input.parent,
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
_parseSync(input) {
|
|
64
|
+
const result = this._parse(input);
|
|
65
|
+
if ((0, parseUtil_1.isAsync)(result)) {
|
|
170
66
|
throw new Error("Synchronous parse encountered promise.");
|
|
171
67
|
}
|
|
172
68
|
return result;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
// if (isAsync(result)) {
|
|
177
|
-
// return result;
|
|
178
|
-
// }
|
|
69
|
+
}
|
|
70
|
+
_parseAsync(input) {
|
|
71
|
+
const result = this._parse(input);
|
|
179
72
|
return Promise.resolve(result);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
73
|
+
}
|
|
74
|
+
parse(data, params) {
|
|
75
|
+
const result = this.safeParse(data, params);
|
|
183
76
|
if (result.success)
|
|
184
77
|
return result.data;
|
|
185
78
|
throw result.error;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
79
|
+
}
|
|
80
|
+
safeParse(data, params) {
|
|
81
|
+
const ctx = {
|
|
82
|
+
path: params?.path || [],
|
|
83
|
+
issues: [],
|
|
84
|
+
contextualErrorMap: params?.errorMap,
|
|
85
|
+
schemaErrorMap: this._def.errorMap,
|
|
86
|
+
async: params?.async ?? false,
|
|
87
|
+
typeCache: new Map(),
|
|
88
|
+
parent: null,
|
|
89
|
+
data,
|
|
90
|
+
parsedType: (0, parseUtil_1.getParsedType)(data),
|
|
91
|
+
};
|
|
92
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
190
93
|
return handleResult(ctx, result);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
return
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
case 1:
|
|
219
|
-
result = _a.sent();
|
|
220
|
-
return [2 /*return*/, handleResult(ctx, result)];
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
};
|
|
225
|
-
ZodType.prototype.refine = function (check, message) {
|
|
226
|
-
var getIssueProperties = function (val) {
|
|
94
|
+
}
|
|
95
|
+
async parseAsync(data, params) {
|
|
96
|
+
const result = await this.safeParseAsync(data, params);
|
|
97
|
+
if (result.success)
|
|
98
|
+
return result.data;
|
|
99
|
+
throw result.error;
|
|
100
|
+
}
|
|
101
|
+
async safeParseAsync(data, params) {
|
|
102
|
+
const ctx = {
|
|
103
|
+
path: params?.path || [],
|
|
104
|
+
issues: [],
|
|
105
|
+
contextualErrorMap: params?.errorMap,
|
|
106
|
+
schemaErrorMap: this._def.errorMap,
|
|
107
|
+
async: true,
|
|
108
|
+
typeCache: new Map(),
|
|
109
|
+
parent: null,
|
|
110
|
+
data,
|
|
111
|
+
parsedType: (0, parseUtil_1.getParsedType)(data),
|
|
112
|
+
};
|
|
113
|
+
const maybeAsyncResult = this._parse({ data, path: [], parent: ctx });
|
|
114
|
+
const result = await ((0, parseUtil_1.isAsync)(maybeAsyncResult)
|
|
115
|
+
? maybeAsyncResult
|
|
116
|
+
: Promise.resolve(maybeAsyncResult));
|
|
117
|
+
return handleResult(ctx, result);
|
|
118
|
+
}
|
|
119
|
+
refine(check, message) {
|
|
120
|
+
const getIssueProperties = (val) => {
|
|
227
121
|
if (typeof message === "string" || typeof message === "undefined") {
|
|
228
|
-
return { message
|
|
122
|
+
return { message };
|
|
229
123
|
}
|
|
230
124
|
else if (typeof message === "function") {
|
|
231
125
|
return message(val);
|
|
@@ -234,13 +128,14 @@ var ZodType = /** @class */ (function () {
|
|
|
234
128
|
return message;
|
|
235
129
|
}
|
|
236
130
|
};
|
|
237
|
-
return this._refinement(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
131
|
+
return this._refinement((val, ctx) => {
|
|
132
|
+
const result = check(val);
|
|
133
|
+
const setError = () => ctx.addIssue({
|
|
134
|
+
code: ZodError_1.ZodIssueCode.custom,
|
|
135
|
+
...getIssueProperties(val),
|
|
136
|
+
});
|
|
242
137
|
if (result instanceof Promise) {
|
|
243
|
-
return result.then(
|
|
138
|
+
return result.then((data) => {
|
|
244
139
|
if (!data) {
|
|
245
140
|
setError();
|
|
246
141
|
return false;
|
|
@@ -258,9 +153,9 @@ var ZodType = /** @class */ (function () {
|
|
|
258
153
|
return true;
|
|
259
154
|
}
|
|
260
155
|
});
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return this._refinement(
|
|
156
|
+
}
|
|
157
|
+
refinement(check, refinementData) {
|
|
158
|
+
return this._refinement((val, ctx) => {
|
|
264
159
|
if (!check(val)) {
|
|
265
160
|
ctx.addIssue(typeof refinementData === "function"
|
|
266
161
|
? refinementData(val, ctx)
|
|
@@ -271,790 +166,723 @@ var ZodType = /** @class */ (function () {
|
|
|
271
166
|
return true;
|
|
272
167
|
}
|
|
273
168
|
});
|
|
274
|
-
}
|
|
275
|
-
|
|
169
|
+
}
|
|
170
|
+
_refinement(refinement) {
|
|
276
171
|
return new ZodEffects({
|
|
277
172
|
schema: this,
|
|
278
173
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
279
|
-
effect: { type: "refinement", refinement
|
|
174
|
+
effect: { type: "refinement", refinement },
|
|
280
175
|
});
|
|
281
|
-
}
|
|
282
|
-
|
|
176
|
+
}
|
|
177
|
+
optional() {
|
|
283
178
|
return ZodOptional.create(this);
|
|
284
|
-
}
|
|
285
|
-
|
|
179
|
+
}
|
|
180
|
+
nullable() {
|
|
286
181
|
return ZodNullable.create(this);
|
|
287
|
-
}
|
|
288
|
-
|
|
182
|
+
}
|
|
183
|
+
nullish() {
|
|
289
184
|
return this.optional().nullable();
|
|
290
|
-
}
|
|
291
|
-
|
|
185
|
+
}
|
|
186
|
+
array() {
|
|
292
187
|
return ZodArray.create(this);
|
|
293
|
-
}
|
|
294
|
-
|
|
188
|
+
}
|
|
189
|
+
promise() {
|
|
295
190
|
return ZodPromise.create(this);
|
|
296
|
-
}
|
|
297
|
-
|
|
191
|
+
}
|
|
192
|
+
or(option) {
|
|
298
193
|
return ZodUnion.create([this, option]);
|
|
299
|
-
}
|
|
300
|
-
|
|
194
|
+
}
|
|
195
|
+
and(incoming) {
|
|
301
196
|
return ZodIntersection.create(this, incoming);
|
|
302
|
-
}
|
|
303
|
-
|
|
197
|
+
}
|
|
198
|
+
transform(transform) {
|
|
304
199
|
return new ZodEffects({
|
|
305
200
|
schema: this,
|
|
306
201
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
307
|
-
effect: { type: "transform", transform
|
|
202
|
+
effect: { type: "transform", transform },
|
|
308
203
|
});
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
// if (this instanceof ZodOptional) {
|
|
313
|
-
// return new ZodOptional({
|
|
314
|
-
// ...this._def,
|
|
315
|
-
// defaultValue: defaultValueFunc,
|
|
316
|
-
// }) as any;
|
|
317
|
-
// }
|
|
204
|
+
}
|
|
205
|
+
default(def) {
|
|
206
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
318
207
|
return new ZodDefault({
|
|
319
208
|
innerType: this,
|
|
320
209
|
defaultValue: defaultValueFunc,
|
|
321
210
|
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
322
211
|
});
|
|
323
|
-
}
|
|
324
|
-
|
|
212
|
+
}
|
|
213
|
+
isOptional() {
|
|
325
214
|
return this.safeParse(undefined).success;
|
|
326
|
-
}
|
|
327
|
-
|
|
215
|
+
}
|
|
216
|
+
isNullable() {
|
|
328
217
|
return this.safeParse(null).success;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
}());
|
|
218
|
+
}
|
|
219
|
+
}
|
|
332
220
|
exports.ZodType = ZodType;
|
|
333
221
|
exports.Schema = ZodType;
|
|
334
222
|
exports.ZodSchema = ZodType;
|
|
335
|
-
|
|
336
|
-
|
|
223
|
+
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
224
|
+
const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
337
225
|
// from https://stackoverflow.com/a/46181/1550155
|
|
338
226
|
// old version: too slow, didn't support unicode
|
|
339
227
|
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
|
340
228
|
// eslint-disable-next-line
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
229
|
+
const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
|
|
230
|
+
class ZodString extends ZodType {
|
|
231
|
+
constructor() {
|
|
232
|
+
super(...arguments);
|
|
233
|
+
this._regex = (regex, validation, message) => this.refinement((data) => regex.test(data), {
|
|
234
|
+
validation,
|
|
235
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
236
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
237
|
+
});
|
|
349
238
|
/**
|
|
350
239
|
* Deprecated.
|
|
351
240
|
* Use z.string().min(1) instead.
|
|
352
241
|
*/
|
|
353
|
-
|
|
354
|
-
return _this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
355
|
-
};
|
|
356
|
-
return _this;
|
|
242
|
+
this.nonempty = (message) => this.min(1, errorUtil_1.errorUtil.errToObj(message));
|
|
357
243
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
if (parsedType !== parseUtil_1.ZodParsedType.string) {
|
|
361
|
-
|
|
244
|
+
_parse(input) {
|
|
245
|
+
const { status, ctx } = this._processInputParams(input);
|
|
246
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.string) {
|
|
247
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
362
248
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
363
249
|
expected: parseUtil_1.ZodParsedType.string,
|
|
364
|
-
received: parsedType,
|
|
365
|
-
}
|
|
250
|
+
received: ctx.parsedType,
|
|
251
|
+
}
|
|
252
|
+
//
|
|
253
|
+
);
|
|
366
254
|
return parseUtil_1.INVALID;
|
|
367
255
|
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
inclusive: true,
|
|
380
|
-
message: check.message,
|
|
381
|
-
}, { data: data });
|
|
382
|
-
}
|
|
256
|
+
for (const check of this._def.checks) {
|
|
257
|
+
if (check.kind === "min") {
|
|
258
|
+
if (ctx.data.length < check.value) {
|
|
259
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
260
|
+
code: ZodError_1.ZodIssueCode.too_small,
|
|
261
|
+
minimum: check.value,
|
|
262
|
+
type: "string",
|
|
263
|
+
inclusive: true,
|
|
264
|
+
message: check.message,
|
|
265
|
+
});
|
|
266
|
+
status.dirty();
|
|
383
267
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
268
|
+
}
|
|
269
|
+
else if (check.kind === "max") {
|
|
270
|
+
if (ctx.data.length > check.value) {
|
|
271
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
272
|
+
code: ZodError_1.ZodIssueCode.too_big,
|
|
273
|
+
maximum: check.value,
|
|
274
|
+
type: "string",
|
|
275
|
+
inclusive: true,
|
|
276
|
+
message: check.message,
|
|
277
|
+
});
|
|
278
|
+
status.dirty();
|
|
395
279
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
280
|
+
}
|
|
281
|
+
else if (check.kind === "email") {
|
|
282
|
+
if (!emailRegex.test(ctx.data)) {
|
|
283
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
284
|
+
validation: "email",
|
|
285
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
286
|
+
message: check.message,
|
|
287
|
+
});
|
|
288
|
+
status.dirty();
|
|
405
289
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
290
|
+
}
|
|
291
|
+
else if (check.kind === "uuid") {
|
|
292
|
+
if (!uuidRegex.test(ctx.data)) {
|
|
293
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
294
|
+
validation: "uuid",
|
|
295
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
296
|
+
message: check.message,
|
|
297
|
+
});
|
|
298
|
+
status.dirty();
|
|
415
299
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
300
|
+
}
|
|
301
|
+
else if (check.kind === "cuid") {
|
|
302
|
+
if (!cuidRegex.test(ctx.data)) {
|
|
303
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
304
|
+
validation: "cuid",
|
|
305
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
306
|
+
message: check.message,
|
|
307
|
+
});
|
|
308
|
+
status.dirty();
|
|
425
309
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
catch (_d) {
|
|
431
|
-
invalid = true;
|
|
432
|
-
this.addIssue(ctx, {
|
|
433
|
-
validation: "url",
|
|
434
|
-
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
435
|
-
message: check.message,
|
|
436
|
-
}, { data: data });
|
|
437
|
-
}
|
|
310
|
+
}
|
|
311
|
+
else if (check.kind === "url") {
|
|
312
|
+
try {
|
|
313
|
+
new URL(ctx.data);
|
|
438
314
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
validation: "regex",
|
|
447
|
-
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
448
|
-
message: check.message,
|
|
449
|
-
}, { data: data });
|
|
450
|
-
}
|
|
315
|
+
catch {
|
|
316
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
317
|
+
validation: "url",
|
|
318
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
319
|
+
message: check.message,
|
|
320
|
+
});
|
|
321
|
+
status.dirty();
|
|
451
322
|
}
|
|
452
323
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
324
|
+
else if (check.kind === "regex") {
|
|
325
|
+
check.regex.lastIndex = 0;
|
|
326
|
+
const testResult = check.regex.test(ctx.data);
|
|
327
|
+
if (!testResult) {
|
|
328
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
329
|
+
validation: "regex",
|
|
330
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
331
|
+
message: check.message,
|
|
332
|
+
});
|
|
333
|
+
status.dirty();
|
|
334
|
+
}
|
|
458
335
|
}
|
|
459
|
-
finally { if (e_1) throw e_1.error; }
|
|
460
336
|
}
|
|
461
|
-
return
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
return new ZodString(
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
return this._addCheck(
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
return this._addCheck(
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
return this._addCheck(
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
return this._addCheck(
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
return this._addCheck(
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
337
|
+
return { status: status.value, value: ctx.data };
|
|
338
|
+
}
|
|
339
|
+
_addCheck(check) {
|
|
340
|
+
return new ZodString({
|
|
341
|
+
...this._def,
|
|
342
|
+
checks: [...this._def.checks, check],
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
email(message) {
|
|
346
|
+
return this._addCheck({ kind: "email", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
347
|
+
}
|
|
348
|
+
url(message) {
|
|
349
|
+
return this._addCheck({ kind: "url", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
350
|
+
}
|
|
351
|
+
uuid(message) {
|
|
352
|
+
return this._addCheck({ kind: "uuid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
353
|
+
}
|
|
354
|
+
cuid(message) {
|
|
355
|
+
return this._addCheck({ kind: "cuid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
356
|
+
}
|
|
357
|
+
regex(regex, message) {
|
|
358
|
+
return this._addCheck({
|
|
359
|
+
kind: "regex",
|
|
360
|
+
regex: regex,
|
|
361
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
min(minLength, message) {
|
|
365
|
+
return this._addCheck({
|
|
366
|
+
kind: "min",
|
|
367
|
+
value: minLength,
|
|
368
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
max(maxLength, message) {
|
|
372
|
+
return this._addCheck({
|
|
373
|
+
kind: "max",
|
|
374
|
+
value: maxLength,
|
|
375
|
+
...errorUtil_1.errorUtil.errToObj(message),
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
length(len, message) {
|
|
488
379
|
return this.min(len, message).max(len, message);
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
enumerable: false,
|
|
509
|
-
configurable: true
|
|
510
|
-
});
|
|
511
|
-
Object.defineProperty(ZodString.prototype, "isCUID", {
|
|
512
|
-
get: function () {
|
|
513
|
-
return !!this._def.checks.find(function (ch) { return ch.kind === "cuid"; });
|
|
514
|
-
},
|
|
515
|
-
enumerable: false,
|
|
516
|
-
configurable: true
|
|
517
|
-
});
|
|
518
|
-
Object.defineProperty(ZodString.prototype, "minLength", {
|
|
519
|
-
get: function () {
|
|
520
|
-
var min = -Infinity;
|
|
521
|
-
this._def.checks.map(function (ch) {
|
|
522
|
-
if (ch.kind === "min") {
|
|
523
|
-
if (min === null || ch.value > min) {
|
|
524
|
-
min = ch.value;
|
|
525
|
-
}
|
|
380
|
+
}
|
|
381
|
+
get isEmail() {
|
|
382
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
383
|
+
}
|
|
384
|
+
get isURL() {
|
|
385
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
386
|
+
}
|
|
387
|
+
get isUUID() {
|
|
388
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
389
|
+
}
|
|
390
|
+
get isCUID() {
|
|
391
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
392
|
+
}
|
|
393
|
+
get minLength() {
|
|
394
|
+
let min = -Infinity;
|
|
395
|
+
this._def.checks.map((ch) => {
|
|
396
|
+
if (ch.kind === "min") {
|
|
397
|
+
if (min === null || ch.value > min) {
|
|
398
|
+
min = ch.value;
|
|
526
399
|
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
if (ch.kind === "max") {
|
|
538
|
-
if (max === null || ch.value < max) {
|
|
539
|
-
max = ch.value;
|
|
540
|
-
}
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
return min;
|
|
403
|
+
}
|
|
404
|
+
get maxLength() {
|
|
405
|
+
let max = null;
|
|
406
|
+
this._def.checks.map((ch) => {
|
|
407
|
+
if (ch.kind === "max") {
|
|
408
|
+
if (max === null || ch.value < max) {
|
|
409
|
+
max = ch.value;
|
|
541
410
|
}
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
});
|
|
548
|
-
ZodString.create = function (params) {
|
|
549
|
-
return new ZodString(__assign({ checks: [], typeName: ZodFirstPartyTypeKind.ZodString }, processCreateParams(params)));
|
|
550
|
-
};
|
|
551
|
-
return ZodString;
|
|
552
|
-
}(ZodType));
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
return max;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
553
416
|
exports.ZodString = ZodString;
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
417
|
+
ZodString.create = (params) => {
|
|
418
|
+
return new ZodString({
|
|
419
|
+
checks: [],
|
|
420
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
421
|
+
...processCreateParams(params),
|
|
422
|
+
});
|
|
423
|
+
};
|
|
424
|
+
class ZodNumber extends ZodType {
|
|
425
|
+
constructor() {
|
|
426
|
+
super(...arguments);
|
|
427
|
+
this.min = this.gte;
|
|
428
|
+
this.max = this.lte;
|
|
429
|
+
this.step = this.multipleOf;
|
|
430
|
+
}
|
|
431
|
+
_parse(input) {
|
|
432
|
+
const { status, ctx } = this._processInputParams(input);
|
|
433
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.number) {
|
|
434
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
567
435
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
568
436
|
expected: parseUtil_1.ZodParsedType.number,
|
|
569
|
-
received: parsedType,
|
|
570
|
-
}
|
|
437
|
+
received: ctx.parsedType,
|
|
438
|
+
});
|
|
571
439
|
return parseUtil_1.INVALID;
|
|
572
440
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
received: "float",
|
|
584
|
-
message: check.message,
|
|
585
|
-
}, { data: data });
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
else if (check.kind === "min") {
|
|
589
|
-
// const MIN = check.value;
|
|
590
|
-
var tooSmall = check.inclusive
|
|
591
|
-
? data < check.value
|
|
592
|
-
: data <= check.value;
|
|
593
|
-
if (tooSmall) {
|
|
594
|
-
invalid = true;
|
|
595
|
-
this.addIssue(ctx, {
|
|
596
|
-
code: ZodError_1.ZodIssueCode.too_small,
|
|
597
|
-
minimum: check.value,
|
|
598
|
-
type: "number",
|
|
599
|
-
inclusive: check.inclusive,
|
|
600
|
-
message: check.message,
|
|
601
|
-
}, { data: data });
|
|
602
|
-
}
|
|
441
|
+
for (const check of this._def.checks) {
|
|
442
|
+
if (check.kind === "int") {
|
|
443
|
+
if (!util_1.util.isInteger(ctx.data)) {
|
|
444
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
445
|
+
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
446
|
+
expected: "integer",
|
|
447
|
+
received: "float",
|
|
448
|
+
message: check.message,
|
|
449
|
+
});
|
|
450
|
+
status.dirty();
|
|
603
451
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
452
|
+
}
|
|
453
|
+
else if (check.kind === "min") {
|
|
454
|
+
const tooSmall = check.inclusive
|
|
455
|
+
? ctx.data < check.value
|
|
456
|
+
: ctx.data <= check.value;
|
|
457
|
+
if (tooSmall) {
|
|
458
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
459
|
+
code: ZodError_1.ZodIssueCode.too_small,
|
|
460
|
+
minimum: check.value,
|
|
461
|
+
type: "number",
|
|
462
|
+
inclusive: check.inclusive,
|
|
463
|
+
message: check.message,
|
|
464
|
+
});
|
|
465
|
+
status.dirty();
|
|
618
466
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
467
|
+
}
|
|
468
|
+
else if (check.kind === "max") {
|
|
469
|
+
const tooBig = check.inclusive
|
|
470
|
+
? ctx.data > check.value
|
|
471
|
+
: ctx.data >= check.value;
|
|
472
|
+
if (tooBig) {
|
|
473
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
474
|
+
code: ZodError_1.ZodIssueCode.too_big,
|
|
475
|
+
maximum: check.value,
|
|
476
|
+
type: "number",
|
|
477
|
+
inclusive: check.inclusive,
|
|
478
|
+
message: check.message,
|
|
479
|
+
});
|
|
480
|
+
status.dirty();
|
|
628
481
|
}
|
|
629
|
-
|
|
630
|
-
|
|
482
|
+
}
|
|
483
|
+
else if (check.kind === "multipleOf") {
|
|
484
|
+
if (ctx.data % check.value !== 0) {
|
|
485
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
486
|
+
code: ZodError_1.ZodIssueCode.not_multiple_of,
|
|
487
|
+
multipleOf: check.value,
|
|
488
|
+
message: check.message,
|
|
489
|
+
});
|
|
490
|
+
status.dirty();
|
|
631
491
|
}
|
|
632
492
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
finally {
|
|
636
|
-
try {
|
|
637
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
493
|
+
else {
|
|
494
|
+
util_1.util.assertNever(check);
|
|
638
495
|
}
|
|
639
|
-
finally { if (e_2) throw e_2.error; }
|
|
640
496
|
}
|
|
641
|
-
return
|
|
642
|
-
}
|
|
643
|
-
|
|
497
|
+
return { status: status.value, value: ctx.data };
|
|
498
|
+
}
|
|
499
|
+
gte(value, message) {
|
|
644
500
|
return this.setLimit("min", value, true, errorUtil_1.errorUtil.toString(message));
|
|
645
|
-
}
|
|
646
|
-
|
|
501
|
+
}
|
|
502
|
+
gt(value, message) {
|
|
647
503
|
return this.setLimit("min", value, false, errorUtil_1.errorUtil.toString(message));
|
|
648
|
-
}
|
|
649
|
-
|
|
504
|
+
}
|
|
505
|
+
lte(value, message) {
|
|
650
506
|
return this.setLimit("max", value, true, errorUtil_1.errorUtil.toString(message));
|
|
651
|
-
}
|
|
652
|
-
|
|
507
|
+
}
|
|
508
|
+
lt(value, message) {
|
|
653
509
|
return this.setLimit("max", value, false, errorUtil_1.errorUtil.toString(message));
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
return new ZodNumber(
|
|
510
|
+
}
|
|
511
|
+
setLimit(kind, value, inclusive, message) {
|
|
512
|
+
return new ZodNumber({
|
|
513
|
+
...this._def,
|
|
514
|
+
checks: [
|
|
515
|
+
...this._def.checks,
|
|
657
516
|
{
|
|
658
|
-
kind
|
|
659
|
-
value
|
|
660
|
-
inclusive
|
|
517
|
+
kind,
|
|
518
|
+
value,
|
|
519
|
+
inclusive,
|
|
661
520
|
message: errorUtil_1.errorUtil.toString(message),
|
|
662
521
|
},
|
|
663
|
-
]
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
522
|
+
],
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
_addCheck(check) {
|
|
526
|
+
return new ZodNumber({
|
|
527
|
+
...this._def,
|
|
528
|
+
checks: [...this._def.checks, check],
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
int(message) {
|
|
669
532
|
return this._addCheck({
|
|
670
533
|
kind: "int",
|
|
671
534
|
message: errorUtil_1.errorUtil.toString(message),
|
|
672
535
|
});
|
|
673
|
-
}
|
|
674
|
-
|
|
536
|
+
}
|
|
537
|
+
positive(message) {
|
|
675
538
|
return this._addCheck({
|
|
676
539
|
kind: "min",
|
|
677
540
|
value: 0,
|
|
678
541
|
inclusive: false,
|
|
679
542
|
message: errorUtil_1.errorUtil.toString(message),
|
|
680
543
|
});
|
|
681
|
-
}
|
|
682
|
-
|
|
544
|
+
}
|
|
545
|
+
negative(message) {
|
|
683
546
|
return this._addCheck({
|
|
684
547
|
kind: "max",
|
|
685
548
|
value: 0,
|
|
686
549
|
inclusive: false,
|
|
687
550
|
message: errorUtil_1.errorUtil.toString(message),
|
|
688
551
|
});
|
|
689
|
-
}
|
|
690
|
-
|
|
552
|
+
}
|
|
553
|
+
nonpositive(message) {
|
|
691
554
|
return this._addCheck({
|
|
692
555
|
kind: "max",
|
|
693
556
|
value: 0,
|
|
694
557
|
inclusive: true,
|
|
695
558
|
message: errorUtil_1.errorUtil.toString(message),
|
|
696
559
|
});
|
|
697
|
-
}
|
|
698
|
-
|
|
560
|
+
}
|
|
561
|
+
nonnegative(message) {
|
|
699
562
|
return this._addCheck({
|
|
700
563
|
kind: "min",
|
|
701
564
|
value: 0,
|
|
702
565
|
inclusive: true,
|
|
703
566
|
message: errorUtil_1.errorUtil.toString(message),
|
|
704
567
|
});
|
|
705
|
-
}
|
|
706
|
-
|
|
568
|
+
}
|
|
569
|
+
multipleOf(value, message) {
|
|
707
570
|
return this._addCheck({
|
|
708
571
|
kind: "multipleOf",
|
|
709
572
|
value: value,
|
|
710
573
|
message: errorUtil_1.errorUtil.toString(message),
|
|
711
574
|
});
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
var ch = _c.value;
|
|
720
|
-
if (ch.kind === "min") {
|
|
721
|
-
if (min === null || ch.value > min)
|
|
722
|
-
min = ch.value;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
727
|
-
finally {
|
|
728
|
-
try {
|
|
729
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
730
|
-
}
|
|
731
|
-
finally { if (e_3) throw e_3.error; }
|
|
732
|
-
}
|
|
733
|
-
return min;
|
|
734
|
-
},
|
|
735
|
-
enumerable: false,
|
|
736
|
-
configurable: true
|
|
737
|
-
});
|
|
738
|
-
Object.defineProperty(ZodNumber.prototype, "maxValue", {
|
|
739
|
-
get: function () {
|
|
740
|
-
var e_4, _a;
|
|
741
|
-
var max = null;
|
|
742
|
-
try {
|
|
743
|
-
for (var _b = __values(this._def.checks), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
744
|
-
var ch = _c.value;
|
|
745
|
-
if (ch.kind === "max") {
|
|
746
|
-
if (max === null || ch.value < max)
|
|
747
|
-
max = ch.value;
|
|
748
|
-
}
|
|
749
|
-
}
|
|
575
|
+
}
|
|
576
|
+
get minValue() {
|
|
577
|
+
let min = null;
|
|
578
|
+
for (const ch of this._def.checks) {
|
|
579
|
+
if (ch.kind === "min") {
|
|
580
|
+
if (min === null || ch.value > min)
|
|
581
|
+
min = ch.value;
|
|
750
582
|
}
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
583
|
+
}
|
|
584
|
+
return min;
|
|
585
|
+
}
|
|
586
|
+
get maxValue() {
|
|
587
|
+
let max = null;
|
|
588
|
+
for (const ch of this._def.checks) {
|
|
589
|
+
if (ch.kind === "max") {
|
|
590
|
+
if (max === null || ch.value < max)
|
|
591
|
+
max = ch.value;
|
|
757
592
|
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
return !!this._def.checks.find(function (ch) { return ch.kind === "int"; });
|
|
766
|
-
},
|
|
767
|
-
enumerable: false,
|
|
768
|
-
configurable: true
|
|
769
|
-
});
|
|
770
|
-
ZodNumber.create = function (params) {
|
|
771
|
-
return new ZodNumber(__assign(__assign({ checks: [], typeName: ZodFirstPartyTypeKind.ZodNumber }, processCreateParams(params)), processCreateParams(params)));
|
|
772
|
-
};
|
|
773
|
-
return ZodNumber;
|
|
774
|
-
}(ZodType));
|
|
593
|
+
}
|
|
594
|
+
return max;
|
|
595
|
+
}
|
|
596
|
+
get isInt() {
|
|
597
|
+
return !!this._def.checks.find((ch) => ch.kind === "int");
|
|
598
|
+
}
|
|
599
|
+
}
|
|
775
600
|
exports.ZodNumber = ZodNumber;
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
601
|
+
ZodNumber.create = (params) => {
|
|
602
|
+
return new ZodNumber({
|
|
603
|
+
checks: [],
|
|
604
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
605
|
+
...processCreateParams(params),
|
|
606
|
+
...processCreateParams(params),
|
|
607
|
+
});
|
|
608
|
+
};
|
|
609
|
+
class ZodBigInt extends ZodType {
|
|
610
|
+
_parse(input) {
|
|
611
|
+
const { ctx } = this._processInputParams(input);
|
|
612
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.bigint) {
|
|
613
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
784
614
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
785
615
|
expected: parseUtil_1.ZodParsedType.bigint,
|
|
786
|
-
received: parsedType,
|
|
787
|
-
}
|
|
616
|
+
received: ctx.parsedType,
|
|
617
|
+
});
|
|
788
618
|
return parseUtil_1.INVALID;
|
|
789
619
|
}
|
|
790
|
-
return parseUtil_1.OK(data);
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
return new ZodBigInt(__assign({ typeName: ZodFirstPartyTypeKind.ZodBigInt }, processCreateParams(params)));
|
|
794
|
-
};
|
|
795
|
-
return ZodBigInt;
|
|
796
|
-
}(ZodType));
|
|
620
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
797
623
|
exports.ZodBigInt = ZodBigInt;
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
624
|
+
ZodBigInt.create = (params) => {
|
|
625
|
+
return new ZodBigInt({
|
|
626
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
627
|
+
...processCreateParams(params),
|
|
628
|
+
});
|
|
629
|
+
};
|
|
630
|
+
class ZodBoolean extends ZodType {
|
|
631
|
+
_parse(input) {
|
|
632
|
+
const { ctx } = this._processInputParams(input);
|
|
633
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.boolean) {
|
|
634
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
806
635
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
807
636
|
expected: parseUtil_1.ZodParsedType.boolean,
|
|
808
|
-
received: parsedType,
|
|
809
|
-
}
|
|
637
|
+
received: ctx.parsedType,
|
|
638
|
+
});
|
|
810
639
|
return parseUtil_1.INVALID;
|
|
811
640
|
}
|
|
812
|
-
return parseUtil_1.OK(data);
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
return new ZodBoolean(__assign({ typeName: ZodFirstPartyTypeKind.ZodBoolean }, processCreateParams(params)));
|
|
816
|
-
};
|
|
817
|
-
return ZodBoolean;
|
|
818
|
-
}(ZodType));
|
|
641
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
819
644
|
exports.ZodBoolean = ZodBoolean;
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
645
|
+
ZodBoolean.create = (params) => {
|
|
646
|
+
return new ZodBoolean({
|
|
647
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
648
|
+
...processCreateParams(params),
|
|
649
|
+
});
|
|
650
|
+
};
|
|
651
|
+
class ZodDate extends ZodType {
|
|
652
|
+
_parse(input) {
|
|
653
|
+
const { status, ctx } = this._processInputParams(input);
|
|
654
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.date) {
|
|
655
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
828
656
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
829
657
|
expected: parseUtil_1.ZodParsedType.date,
|
|
830
|
-
received: parsedType,
|
|
831
|
-
}
|
|
658
|
+
received: ctx.parsedType,
|
|
659
|
+
});
|
|
832
660
|
return parseUtil_1.INVALID;
|
|
833
661
|
}
|
|
834
|
-
if (isNaN(data.getTime())) {
|
|
835
|
-
|
|
662
|
+
if (isNaN(ctx.data.getTime())) {
|
|
663
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
836
664
|
code: ZodError_1.ZodIssueCode.invalid_date,
|
|
837
|
-
}
|
|
665
|
+
});
|
|
838
666
|
return parseUtil_1.INVALID;
|
|
839
667
|
}
|
|
840
|
-
return
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
}(ZodType));
|
|
668
|
+
return {
|
|
669
|
+
status: status.value,
|
|
670
|
+
value: new Date(ctx.data.getTime()),
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
}
|
|
847
674
|
exports.ZodDate = ZodDate;
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
675
|
+
ZodDate.create = (params) => {
|
|
676
|
+
return new ZodDate({
|
|
677
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
678
|
+
...processCreateParams(params),
|
|
679
|
+
});
|
|
680
|
+
};
|
|
681
|
+
class ZodUndefined extends ZodType {
|
|
682
|
+
_parse(input) {
|
|
683
|
+
const { ctx } = this._processInputParams(input);
|
|
684
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.undefined) {
|
|
685
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
856
686
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
857
687
|
expected: parseUtil_1.ZodParsedType.undefined,
|
|
858
|
-
received: parsedType,
|
|
859
|
-
}
|
|
688
|
+
received: ctx.parsedType,
|
|
689
|
+
});
|
|
860
690
|
return parseUtil_1.INVALID;
|
|
861
691
|
}
|
|
862
|
-
return parseUtil_1.OK(data);
|
|
863
|
-
}
|
|
864
|
-
|
|
865
|
-
return new ZodUndefined(__assign({ typeName: ZodFirstPartyTypeKind.ZodUndefined }, processCreateParams(params)));
|
|
866
|
-
};
|
|
867
|
-
return ZodUndefined;
|
|
868
|
-
}(ZodType));
|
|
692
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
869
695
|
exports.ZodUndefined = ZodUndefined;
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
696
|
+
ZodUndefined.create = (params) => {
|
|
697
|
+
return new ZodUndefined({
|
|
698
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
699
|
+
...processCreateParams(params),
|
|
700
|
+
});
|
|
701
|
+
};
|
|
702
|
+
class ZodNull extends ZodType {
|
|
703
|
+
_parse(input) {
|
|
704
|
+
const { ctx } = this._processInputParams(input);
|
|
705
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.null) {
|
|
706
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
878
707
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
879
708
|
expected: parseUtil_1.ZodParsedType.null,
|
|
880
|
-
received: parsedType,
|
|
881
|
-
}
|
|
709
|
+
received: ctx.parsedType,
|
|
710
|
+
});
|
|
882
711
|
return parseUtil_1.INVALID;
|
|
883
712
|
}
|
|
884
|
-
return parseUtil_1.OK(data);
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
return new ZodNull(__assign({ typeName: ZodFirstPartyTypeKind.ZodNull }, processCreateParams(params)));
|
|
888
|
-
};
|
|
889
|
-
return ZodNull;
|
|
890
|
-
}(ZodType));
|
|
713
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
891
716
|
exports.ZodNull = ZodNull;
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
717
|
+
ZodNull.create = (params) => {
|
|
718
|
+
return new ZodNull({
|
|
719
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
720
|
+
...processCreateParams(params),
|
|
721
|
+
});
|
|
722
|
+
};
|
|
723
|
+
class ZodAny extends ZodType {
|
|
724
|
+
constructor() {
|
|
725
|
+
super(...arguments);
|
|
896
726
|
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
897
|
-
|
|
898
|
-
return _this;
|
|
727
|
+
this._any = true;
|
|
899
728
|
}
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
};
|
|
906
|
-
return ZodAny;
|
|
907
|
-
}(ZodType));
|
|
729
|
+
_parse(input) {
|
|
730
|
+
const { ctx } = this._processInputParams(input);
|
|
731
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
908
734
|
exports.ZodAny = ZodAny;
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
735
|
+
ZodAny.create = (params) => {
|
|
736
|
+
return new ZodAny({
|
|
737
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
738
|
+
...processCreateParams(params),
|
|
739
|
+
});
|
|
740
|
+
};
|
|
741
|
+
class ZodUnknown extends ZodType {
|
|
742
|
+
constructor() {
|
|
743
|
+
super(...arguments);
|
|
913
744
|
// required
|
|
914
|
-
|
|
915
|
-
return _this;
|
|
745
|
+
this._unknown = true;
|
|
916
746
|
}
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
ZodUnknown.create = function (params) {
|
|
921
|
-
return new ZodUnknown(__assign({ typeName: ZodFirstPartyTypeKind.ZodUnknown }, processCreateParams(params)));
|
|
922
|
-
};
|
|
923
|
-
return ZodUnknown;
|
|
924
|
-
}(ZodType));
|
|
925
|
-
exports.ZodUnknown = ZodUnknown;
|
|
926
|
-
var ZodNever = /** @class */ (function (_super) {
|
|
927
|
-
__extends(ZodNever, _super);
|
|
928
|
-
function ZodNever() {
|
|
929
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
747
|
+
_parse(input) {
|
|
748
|
+
const { ctx } = this._processInputParams(input);
|
|
749
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
930
750
|
}
|
|
931
|
-
|
|
932
|
-
|
|
751
|
+
}
|
|
752
|
+
exports.ZodUnknown = ZodUnknown;
|
|
753
|
+
ZodUnknown.create = (params) => {
|
|
754
|
+
return new ZodUnknown({
|
|
755
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
756
|
+
...processCreateParams(params),
|
|
757
|
+
});
|
|
758
|
+
};
|
|
759
|
+
class ZodNever extends ZodType {
|
|
760
|
+
_parse(input) {
|
|
761
|
+
const { ctx } = this._processInputParams(input);
|
|
762
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
933
763
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
934
764
|
expected: parseUtil_1.ZodParsedType.never,
|
|
935
|
-
received: parsedType,
|
|
936
|
-
}
|
|
765
|
+
received: ctx.parsedType,
|
|
766
|
+
});
|
|
937
767
|
return parseUtil_1.INVALID;
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
return new ZodNever(__assign({ typeName: ZodFirstPartyTypeKind.ZodNever }, processCreateParams(params)));
|
|
941
|
-
};
|
|
942
|
-
return ZodNever;
|
|
943
|
-
}(ZodType));
|
|
768
|
+
}
|
|
769
|
+
}
|
|
944
770
|
exports.ZodNever = ZodNever;
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
}
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
771
|
+
ZodNever.create = (params) => {
|
|
772
|
+
return new ZodNever({
|
|
773
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
774
|
+
...processCreateParams(params),
|
|
775
|
+
});
|
|
776
|
+
};
|
|
777
|
+
class ZodVoid extends ZodType {
|
|
778
|
+
_parse(input) {
|
|
779
|
+
const { ctx } = this._processInputParams(input);
|
|
780
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.undefined) {
|
|
781
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
953
782
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
954
783
|
expected: parseUtil_1.ZodParsedType.void,
|
|
955
|
-
received: parsedType,
|
|
956
|
-
}
|
|
784
|
+
received: ctx.parsedType,
|
|
785
|
+
});
|
|
957
786
|
return parseUtil_1.INVALID;
|
|
958
787
|
}
|
|
959
|
-
return parseUtil_1.OK(data);
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
return new ZodVoid(__assign({ typeName: ZodFirstPartyTypeKind.ZodVoid }, processCreateParams(params)));
|
|
963
|
-
};
|
|
964
|
-
return ZodVoid;
|
|
965
|
-
}(ZodType));
|
|
788
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
966
791
|
exports.ZodVoid = ZodVoid;
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
792
|
+
ZodVoid.create = (params) => {
|
|
793
|
+
return new ZodVoid({
|
|
794
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
795
|
+
...processCreateParams(params),
|
|
796
|
+
});
|
|
797
|
+
};
|
|
798
|
+
class ZodArray extends ZodType {
|
|
799
|
+
_parse(input) {
|
|
800
|
+
const { status, ctx } = this._processInputParams(input);
|
|
801
|
+
const def = this._def;
|
|
802
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.array) {
|
|
803
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
977
804
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
978
805
|
expected: parseUtil_1.ZodParsedType.array,
|
|
979
|
-
received: parsedType,
|
|
980
|
-
}
|
|
806
|
+
received: ctx.parsedType,
|
|
807
|
+
});
|
|
981
808
|
return parseUtil_1.INVALID;
|
|
982
809
|
}
|
|
983
|
-
var data = _data;
|
|
984
|
-
var invalid = false;
|
|
985
810
|
if (def.minLength !== null) {
|
|
986
|
-
if (data.length < def.minLength.value) {
|
|
987
|
-
|
|
988
|
-
this.addIssue(ctx, {
|
|
811
|
+
if (ctx.data.length < def.minLength.value) {
|
|
812
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
989
813
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
990
814
|
minimum: def.minLength.value,
|
|
991
815
|
type: "array",
|
|
992
816
|
inclusive: true,
|
|
993
817
|
message: def.minLength.message,
|
|
994
|
-
}
|
|
818
|
+
});
|
|
819
|
+
status.dirty();
|
|
995
820
|
}
|
|
996
821
|
}
|
|
997
822
|
if (def.maxLength !== null) {
|
|
998
|
-
if (data.length > def.maxLength.value) {
|
|
999
|
-
|
|
1000
|
-
this.addIssue(ctx, {
|
|
823
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
824
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1001
825
|
code: ZodError_1.ZodIssueCode.too_big,
|
|
1002
826
|
maximum: def.maxLength.value,
|
|
1003
827
|
type: "array",
|
|
1004
828
|
inclusive: true,
|
|
1005
829
|
message: def.maxLength.message,
|
|
1006
|
-
}
|
|
830
|
+
});
|
|
831
|
+
status.dirty();
|
|
1007
832
|
}
|
|
1008
833
|
}
|
|
1009
|
-
var tasks = [];
|
|
1010
|
-
var result = new Array(data.length);
|
|
1011
|
-
var type = def.type;
|
|
1012
|
-
var handleParsed = function (index, parsedItem) {
|
|
1013
|
-
if (parseUtil_1.isOk(parsedItem)) {
|
|
1014
|
-
result[index] = parsedItem.value;
|
|
1015
|
-
}
|
|
1016
|
-
else if (parseUtil_1.isInvalid(parsedItem)) {
|
|
1017
|
-
invalid = true;
|
|
1018
|
-
}
|
|
1019
|
-
else {
|
|
1020
|
-
tasks.push(parsedItem.then(function (parsed) { return handleParsed(index, parsed); }));
|
|
1021
|
-
}
|
|
1022
|
-
};
|
|
1023
|
-
data.forEach(function (item, index) {
|
|
1024
|
-
handleParsed(index, type._parse(ctx.stepInto(index), item, parseUtil_1.getParsedType(item)));
|
|
1025
|
-
});
|
|
1026
834
|
if (ctx.async) {
|
|
1027
|
-
return Promise.all(
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
835
|
+
return Promise.all(ctx.data.map((item, i) => {
|
|
836
|
+
return def.type._parseAsync({
|
|
837
|
+
parent: ctx,
|
|
838
|
+
path: [...ctx.path, i],
|
|
839
|
+
data: item,
|
|
840
|
+
});
|
|
841
|
+
})).then((result) => {
|
|
842
|
+
return parseUtil_1.ParseStatus.mergeArray(status, result);
|
|
843
|
+
});
|
|
1031
844
|
}
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
845
|
+
const result = ctx.data.map((item, i) => {
|
|
846
|
+
return def.type._parseSync({
|
|
847
|
+
parent: ctx,
|
|
848
|
+
path: [...ctx.path, i],
|
|
849
|
+
data: item,
|
|
850
|
+
});
|
|
851
|
+
});
|
|
852
|
+
return parseUtil_1.ParseStatus.mergeArray(status, result);
|
|
853
|
+
}
|
|
854
|
+
get element() {
|
|
855
|
+
return this._def.type;
|
|
856
|
+
}
|
|
857
|
+
min(minLength, message) {
|
|
858
|
+
return new ZodArray({
|
|
859
|
+
...this._def,
|
|
860
|
+
minLength: { value: minLength, message: errorUtil_1.errorUtil.toString(message) },
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
max(maxLength, message) {
|
|
864
|
+
return new ZodArray({
|
|
865
|
+
...this._def,
|
|
866
|
+
maxLength: { value: maxLength, message: errorUtil_1.errorUtil.toString(message) },
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
length(len, message) {
|
|
1047
870
|
return this.min(len, message).max(len, message);
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
return this.min(1, message);
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
return new ZodArray(__assign({ type: schema, minLength: null, maxLength: null, typeName: ZodFirstPartyTypeKind.ZodArray }, processCreateParams(params)));
|
|
1054
|
-
};
|
|
1055
|
-
return ZodArray;
|
|
1056
|
-
}(ZodType));
|
|
871
|
+
}
|
|
872
|
+
nonempty(message) {
|
|
873
|
+
return this.min(1, message);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
1057
876
|
exports.ZodArray = ZodArray;
|
|
877
|
+
ZodArray.create = (schema, params) => {
|
|
878
|
+
return new ZodArray({
|
|
879
|
+
type: schema,
|
|
880
|
+
minLength: null,
|
|
881
|
+
maxLength: null,
|
|
882
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
883
|
+
...processCreateParams(params),
|
|
884
|
+
});
|
|
885
|
+
};
|
|
1058
886
|
/////////////////////////////////////////
|
|
1059
887
|
/////////////////////////////////////////
|
|
1060
888
|
////////// //////////
|
|
@@ -1064,54 +892,33 @@ exports.ZodArray = ZodArray;
|
|
|
1064
892
|
/////////////////////////////////////////
|
|
1065
893
|
var objectUtil;
|
|
1066
894
|
(function (objectUtil) {
|
|
1067
|
-
objectUtil.mergeShapes =
|
|
1068
|
-
return
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
var firstKeys = util_1.util.objectKeys(first);
|
|
1073
|
-
var secondKeys = util_1.util.objectKeys(second);
|
|
1074
|
-
var sharedKeys = firstKeys.filter(function (k) { return secondKeys.indexOf(k) !== -1; });
|
|
1075
|
-
var sharedShape = {};
|
|
1076
|
-
try {
|
|
1077
|
-
for (var sharedKeys_1 = __values(sharedKeys), sharedKeys_1_1 = sharedKeys_1.next(); !sharedKeys_1_1.done; sharedKeys_1_1 = sharedKeys_1.next()) {
|
|
1078
|
-
var k = sharedKeys_1_1.value;
|
|
1079
|
-
sharedShape[k] = ZodIntersection.create(first[k], second[k]);
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
1083
|
-
finally {
|
|
1084
|
-
try {
|
|
1085
|
-
if (sharedKeys_1_1 && !sharedKeys_1_1.done && (_a = sharedKeys_1.return)) _a.call(sharedKeys_1);
|
|
1086
|
-
}
|
|
1087
|
-
finally { if (e_5) throw e_5.error; }
|
|
1088
|
-
}
|
|
1089
|
-
return __assign(__assign(__assign({}, first), second), sharedShape);
|
|
895
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
896
|
+
return {
|
|
897
|
+
...first,
|
|
898
|
+
...second, // second overwrites first
|
|
899
|
+
};
|
|
1090
900
|
};
|
|
1091
901
|
})(objectUtil = exports.objectUtil || (exports.objectUtil = {}));
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
902
|
+
const AugmentFactory = (def) => (augmentation) => {
|
|
903
|
+
return new ZodObject({
|
|
904
|
+
...def,
|
|
905
|
+
shape: () => ({
|
|
906
|
+
...def.shape(),
|
|
907
|
+
...augmentation,
|
|
908
|
+
}),
|
|
1100
909
|
});
|
|
1101
|
-
|
|
1102
|
-
}; };
|
|
1103
|
-
exports.mergeObjects = mergeObjects;
|
|
1104
|
-
var AugmentFactory = function (def) { return function (augmentation) {
|
|
1105
|
-
return new ZodObject(__assign(__assign({}, def), { shape: function () { return (__assign(__assign({}, def.shape()), augmentation)); } }));
|
|
1106
|
-
}; };
|
|
910
|
+
};
|
|
1107
911
|
function deepPartialify(schema) {
|
|
1108
912
|
if (schema instanceof ZodObject) {
|
|
1109
|
-
|
|
1110
|
-
for (
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
}
|
|
1114
|
-
return new ZodObject(
|
|
913
|
+
const newShape = {};
|
|
914
|
+
for (const key in schema.shape) {
|
|
915
|
+
const fieldSchema = schema.shape[key];
|
|
916
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
917
|
+
}
|
|
918
|
+
return new ZodObject({
|
|
919
|
+
...schema._def,
|
|
920
|
+
shape: () => newShape,
|
|
921
|
+
});
|
|
1115
922
|
}
|
|
1116
923
|
else if (schema instanceof ZodArray) {
|
|
1117
924
|
return ZodArray.create(deepPartialify(schema.element));
|
|
@@ -1123,708 +930,711 @@ function deepPartialify(schema) {
|
|
|
1123
930
|
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
1124
931
|
}
|
|
1125
932
|
else if (schema instanceof ZodTuple) {
|
|
1126
|
-
return ZodTuple.create(schema.items.map(
|
|
933
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
1127
934
|
}
|
|
1128
935
|
else {
|
|
1129
936
|
return schema;
|
|
1130
937
|
}
|
|
1131
938
|
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
_this._cached = null;
|
|
939
|
+
class ZodObject extends ZodType {
|
|
940
|
+
constructor() {
|
|
941
|
+
super(...arguments);
|
|
942
|
+
this._cached = null;
|
|
1137
943
|
/**
|
|
1138
944
|
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
1139
945
|
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
1140
946
|
*/
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
return _this;
|
|
947
|
+
this.nonstrict = this.passthrough;
|
|
948
|
+
this.augment = AugmentFactory(this._def);
|
|
949
|
+
this.extend = AugmentFactory(this._def);
|
|
1145
950
|
}
|
|
1146
|
-
|
|
951
|
+
_getCached() {
|
|
1147
952
|
if (this._cached !== null)
|
|
1148
953
|
return this._cached;
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
return (this._cached = { shape
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
if (parsedType !== parseUtil_1.ZodParsedType.object) {
|
|
1156
|
-
|
|
954
|
+
const shape = this._def.shape();
|
|
955
|
+
const keys = util_1.util.objectKeys(shape);
|
|
956
|
+
return (this._cached = { shape, keys });
|
|
957
|
+
}
|
|
958
|
+
_parse(input) {
|
|
959
|
+
const { status, ctx } = this._processInputParams(input);
|
|
960
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.object) {
|
|
961
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1157
962
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1158
963
|
expected: parseUtil_1.ZodParsedType.object,
|
|
1159
|
-
received: parsedType,
|
|
1160
|
-
}
|
|
964
|
+
received: ctx.parsedType,
|
|
965
|
+
});
|
|
1161
966
|
return parseUtil_1.INVALID;
|
|
1162
967
|
}
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
}
|
|
1180
|
-
else {
|
|
1181
|
-
tasks.push(parsedValue.then(function (parsed) { return handleParsed(key, parsed); }));
|
|
1182
|
-
}
|
|
1183
|
-
};
|
|
1184
|
-
try {
|
|
1185
|
-
for (var shapeKeys_1 = __values(shapeKeys), shapeKeys_1_1 = shapeKeys_1.next(); !shapeKeys_1_1.done; shapeKeys_1_1 = shapeKeys_1.next()) {
|
|
1186
|
-
var key = shapeKeys_1_1.value;
|
|
1187
|
-
var keyValidator = shape[key];
|
|
1188
|
-
var value = data[key];
|
|
1189
|
-
handleParsed(key, keyValidator._parse(ctx.stepInto(key), value, parseUtil_1.getParsedType(value)));
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
1193
|
-
finally {
|
|
1194
|
-
try {
|
|
1195
|
-
if (shapeKeys_1_1 && !shapeKeys_1_1.done && (_a = shapeKeys_1.return)) _a.call(shapeKeys_1);
|
|
1196
|
-
}
|
|
1197
|
-
finally { if (e_6) throw e_6.error; }
|
|
968
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
969
|
+
const dataKeys = util_1.util.objectKeys(ctx.data);
|
|
970
|
+
const extraKeys = dataKeys.filter((k) => !(k in shape));
|
|
971
|
+
const pairs = [];
|
|
972
|
+
for (const key of shapeKeys) {
|
|
973
|
+
const keyValidator = shape[key];
|
|
974
|
+
const value = ctx.data[key];
|
|
975
|
+
pairs.push({
|
|
976
|
+
key: { status: "valid", value: key },
|
|
977
|
+
value: keyValidator._parse({
|
|
978
|
+
parent: ctx,
|
|
979
|
+
data: value,
|
|
980
|
+
path: [...ctx.path, key],
|
|
981
|
+
}),
|
|
982
|
+
alwaysSet: key in ctx.data,
|
|
983
|
+
});
|
|
1198
984
|
}
|
|
1199
985
|
if (this._def.catchall instanceof ZodNever) {
|
|
1200
|
-
|
|
986
|
+
const unknownKeys = this._def.unknownKeys;
|
|
1201
987
|
if (unknownKeys === "passthrough") {
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
resultObject[key] = data[key];
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
1211
|
-
finally {
|
|
1212
|
-
try {
|
|
1213
|
-
if (extraKeys_1_1 && !extraKeys_1_1.done && (_b = extraKeys_1.return)) _b.call(extraKeys_1);
|
|
1214
|
-
}
|
|
1215
|
-
finally { if (e_7) throw e_7.error; }
|
|
988
|
+
for (const key of extraKeys) {
|
|
989
|
+
pairs.push({
|
|
990
|
+
key: { status: "valid", value: key },
|
|
991
|
+
value: { status: "valid", value: ctx.data[key] },
|
|
992
|
+
});
|
|
1216
993
|
}
|
|
1217
994
|
}
|
|
1218
995
|
else if (unknownKeys === "strict") {
|
|
1219
|
-
var dataKeys = util_1.util.objectKeys(data);
|
|
1220
|
-
var extraKeys = dataKeys.filter(function (k) { return !(k in shape); });
|
|
1221
996
|
if (extraKeys.length > 0) {
|
|
1222
|
-
|
|
1223
|
-
this.addIssue(ctx, {
|
|
997
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1224
998
|
code: ZodError_1.ZodIssueCode.unrecognized_keys,
|
|
1225
999
|
keys: extraKeys,
|
|
1226
|
-
}
|
|
1000
|
+
});
|
|
1001
|
+
status.dirty();
|
|
1227
1002
|
}
|
|
1228
1003
|
}
|
|
1229
1004
|
else if (unknownKeys === "strip") {
|
|
1230
1005
|
}
|
|
1231
1006
|
else {
|
|
1232
|
-
throw new Error(
|
|
1007
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
1233
1008
|
}
|
|
1234
1009
|
}
|
|
1235
1010
|
else {
|
|
1236
1011
|
// run catchall validation
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
}
|
|
1246
|
-
}
|
|
1247
|
-
catch (e_8_1) { e_8 = { error: e_8_1 }; }
|
|
1248
|
-
finally {
|
|
1249
|
-
try {
|
|
1250
|
-
if (extraKeys_2_1 && !extraKeys_2_1.done && (_c = extraKeys_2.return)) _c.call(extraKeys_2);
|
|
1251
|
-
}
|
|
1252
|
-
finally { if (e_8) throw e_8.error; }
|
|
1012
|
+
const catchall = this._def.catchall;
|
|
1013
|
+
for (const key of extraKeys) {
|
|
1014
|
+
const value = ctx.data[key];
|
|
1015
|
+
pairs.push({
|
|
1016
|
+
key: { status: "valid", value: key },
|
|
1017
|
+
value: catchall._parse({ parent: ctx, path: [...ctx.path, key], data: value } //, ctx.child(key), value, getParsedType(value)
|
|
1018
|
+
),
|
|
1019
|
+
alwaysSet: key in ctx.data,
|
|
1020
|
+
});
|
|
1253
1021
|
}
|
|
1254
1022
|
}
|
|
1255
1023
|
if (ctx.async) {
|
|
1256
|
-
return Promise.
|
|
1257
|
-
|
|
1024
|
+
return Promise.resolve()
|
|
1025
|
+
.then(async () => {
|
|
1026
|
+
const syncPairs = [];
|
|
1027
|
+
for (const pair of pairs) {
|
|
1028
|
+
const key = await pair.key;
|
|
1029
|
+
syncPairs.push({
|
|
1030
|
+
key,
|
|
1031
|
+
value: await pair.value,
|
|
1032
|
+
alwaysSet: pair.alwaysSet,
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
return syncPairs;
|
|
1036
|
+
})
|
|
1037
|
+
.then((syncPairs) => {
|
|
1038
|
+
return parseUtil_1.ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1258
1039
|
});
|
|
1259
1040
|
}
|
|
1260
1041
|
else {
|
|
1261
|
-
return
|
|
1042
|
+
return parseUtil_1.ParseStatus.mergeObjectSync(status, pairs);
|
|
1262
1043
|
}
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1044
|
+
}
|
|
1045
|
+
get shape() {
|
|
1046
|
+
return this._def.shape();
|
|
1047
|
+
}
|
|
1048
|
+
strict(message) {
|
|
1049
|
+
errorUtil_1.errorUtil.errToObj;
|
|
1050
|
+
return new ZodObject({
|
|
1051
|
+
...this._def,
|
|
1052
|
+
unknownKeys: "strict",
|
|
1053
|
+
...(message !== undefined
|
|
1054
|
+
? {
|
|
1055
|
+
errorMap: (issue, ctx) => {
|
|
1056
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
1057
|
+
if (issue.code === "unrecognized_keys")
|
|
1058
|
+
return {
|
|
1059
|
+
message: errorUtil_1.errorUtil.errToObj(message).message ?? defaultError,
|
|
1060
|
+
};
|
|
1061
|
+
return {
|
|
1062
|
+
message: defaultError,
|
|
1063
|
+
};
|
|
1064
|
+
},
|
|
1065
|
+
}
|
|
1066
|
+
: {}),
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
strip() {
|
|
1070
|
+
return new ZodObject({
|
|
1071
|
+
...this._def,
|
|
1072
|
+
unknownKeys: "strip",
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
passthrough() {
|
|
1076
|
+
return new ZodObject({
|
|
1077
|
+
...this._def,
|
|
1078
|
+
unknownKeys: "passthrough",
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
setKey(key, schema) {
|
|
1082
|
+
return this.augment({ [key]: schema });
|
|
1083
|
+
}
|
|
1284
1084
|
/**
|
|
1285
1085
|
* Prior to zod@1.0.12 there was a bug in the
|
|
1286
1086
|
* inferred type of merged objects. Please
|
|
1287
1087
|
* upgrade if you are experiencing issues.
|
|
1288
1088
|
*/
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
// effects: [], // wipe all refinements
|
|
1089
|
+
merge(merging) {
|
|
1090
|
+
const mergedShape = objectUtil.mergeShapes(this._def.shape(), merging._def.shape());
|
|
1091
|
+
const merged = new ZodObject({
|
|
1293
1092
|
unknownKeys: merging._def.unknownKeys,
|
|
1294
1093
|
catchall: merging._def.catchall,
|
|
1295
|
-
shape:
|
|
1094
|
+
shape: () => mergedShape,
|
|
1296
1095
|
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1297
1096
|
});
|
|
1298
1097
|
return merged;
|
|
1299
|
-
}
|
|
1300
|
-
|
|
1301
|
-
return new ZodObject(
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
var _this = this;
|
|
1305
|
-
var shape = {};
|
|
1306
|
-
util_1.util.objectKeys(mask).map(function (key) {
|
|
1307
|
-
shape[key] = _this.shape[key];
|
|
1098
|
+
}
|
|
1099
|
+
catchall(index) {
|
|
1100
|
+
return new ZodObject({
|
|
1101
|
+
...this._def,
|
|
1102
|
+
catchall: index,
|
|
1308
1103
|
});
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1104
|
+
}
|
|
1105
|
+
pick(mask) {
|
|
1106
|
+
const shape = {};
|
|
1107
|
+
util_1.util.objectKeys(mask).map((key) => {
|
|
1108
|
+
shape[key] = this.shape[key];
|
|
1109
|
+
});
|
|
1110
|
+
return new ZodObject({
|
|
1111
|
+
...this._def,
|
|
1112
|
+
shape: () => shape,
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
omit(mask) {
|
|
1116
|
+
const shape = {};
|
|
1117
|
+
util_1.util.objectKeys(this.shape).map((key) => {
|
|
1315
1118
|
if (util_1.util.objectKeys(mask).indexOf(key) === -1) {
|
|
1316
|
-
shape[key] =
|
|
1119
|
+
shape[key] = this.shape[key];
|
|
1317
1120
|
}
|
|
1318
1121
|
});
|
|
1319
|
-
return new ZodObject(
|
|
1320
|
-
|
|
1321
|
-
|
|
1122
|
+
return new ZodObject({
|
|
1123
|
+
...this._def,
|
|
1124
|
+
shape: () => shape,
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
deepPartial() {
|
|
1322
1128
|
return deepPartialify(this);
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
var newShape = {};
|
|
1129
|
+
}
|
|
1130
|
+
partial(mask) {
|
|
1131
|
+
const newShape = {};
|
|
1327
1132
|
if (mask) {
|
|
1328
|
-
|
|
1329
|
-
util_1.util.objectKeys(this.shape).map(function (key) {
|
|
1133
|
+
util_1.util.objectKeys(this.shape).map((key) => {
|
|
1330
1134
|
if (util_1.util.objectKeys(mask).indexOf(key) === -1) {
|
|
1331
|
-
newShape[key] =
|
|
1135
|
+
newShape[key] = this.shape[key];
|
|
1332
1136
|
}
|
|
1333
1137
|
else {
|
|
1334
|
-
newShape[key] =
|
|
1138
|
+
newShape[key] = this.shape[key].optional();
|
|
1335
1139
|
}
|
|
1336
1140
|
});
|
|
1337
|
-
return new ZodObject(
|
|
1141
|
+
return new ZodObject({
|
|
1142
|
+
...this._def,
|
|
1143
|
+
shape: () => newShape,
|
|
1144
|
+
});
|
|
1338
1145
|
}
|
|
1339
1146
|
else {
|
|
1340
|
-
for (
|
|
1341
|
-
|
|
1147
|
+
for (const key in this.shape) {
|
|
1148
|
+
const fieldSchema = this.shape[key];
|
|
1342
1149
|
newShape[key] = fieldSchema.optional();
|
|
1343
1150
|
}
|
|
1344
1151
|
}
|
|
1345
|
-
return new ZodObject(
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1152
|
+
return new ZodObject({
|
|
1153
|
+
...this._def,
|
|
1154
|
+
shape: () => newShape,
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
required() {
|
|
1158
|
+
const newShape = {};
|
|
1159
|
+
for (const key in this.shape) {
|
|
1160
|
+
const fieldSchema = this.shape[key];
|
|
1161
|
+
let newField = fieldSchema;
|
|
1352
1162
|
while (newField instanceof ZodOptional) {
|
|
1353
1163
|
newField = newField._def.innerType;
|
|
1354
1164
|
}
|
|
1355
1165
|
newShape[key] = newField;
|
|
1356
1166
|
}
|
|
1357
|
-
return new ZodObject(
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
}
|
|
1362
|
-
|
|
1363
|
-
return new ZodObject(__assign({ shape: function () { return shape; }, unknownKeys: "strict", catchall: ZodNever.create(), typeName: ZodFirstPartyTypeKind.ZodObject }, processCreateParams(params)));
|
|
1364
|
-
};
|
|
1365
|
-
ZodObject.lazycreate = function (shape, params) {
|
|
1366
|
-
return new ZodObject(__assign({ shape: shape, unknownKeys: "strip", catchall: ZodNever.create(), typeName: ZodFirstPartyTypeKind.ZodObject }, processCreateParams(params)));
|
|
1367
|
-
};
|
|
1368
|
-
return ZodObject;
|
|
1369
|
-
}(ZodType));
|
|
1167
|
+
return new ZodObject({
|
|
1168
|
+
...this._def,
|
|
1169
|
+
shape: () => newShape,
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1370
1173
|
exports.ZodObject = ZodObject;
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1174
|
+
ZodObject.create = (shape, params) => {
|
|
1175
|
+
return new ZodObject({
|
|
1176
|
+
shape: () => shape,
|
|
1177
|
+
unknownKeys: "strip",
|
|
1178
|
+
catchall: ZodNever.create(),
|
|
1179
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1180
|
+
...processCreateParams(params),
|
|
1181
|
+
});
|
|
1182
|
+
};
|
|
1183
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
1184
|
+
return new ZodObject({
|
|
1185
|
+
shape: () => shape,
|
|
1186
|
+
unknownKeys: "strict",
|
|
1187
|
+
catchall: ZodNever.create(),
|
|
1188
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1189
|
+
...processCreateParams(params),
|
|
1190
|
+
});
|
|
1191
|
+
};
|
|
1192
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
1193
|
+
return new ZodObject({
|
|
1194
|
+
shape,
|
|
1195
|
+
unknownKeys: "strip",
|
|
1196
|
+
catchall: ZodNever.create(),
|
|
1197
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1198
|
+
...processCreateParams(params),
|
|
1199
|
+
});
|
|
1200
|
+
};
|
|
1201
|
+
class ZodUnion extends ZodType {
|
|
1202
|
+
_parse(input) {
|
|
1203
|
+
const { ctx } = this._processInputParams(input);
|
|
1204
|
+
const options = this._def.options;
|
|
1205
|
+
function handleResults(results) {
|
|
1206
|
+
// return first issue-free validation if it exists
|
|
1207
|
+
for (const result of results) {
|
|
1208
|
+
if (result.result.status === "valid") {
|
|
1209
|
+
return result.result;
|
|
1210
|
+
}
|
|
1399
1211
|
}
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1212
|
+
for (const result of results) {
|
|
1213
|
+
if (result.result.status === "dirty") {
|
|
1214
|
+
// add issues from dirty option
|
|
1215
|
+
ctx.issues.push(...result.ctx.issues);
|
|
1216
|
+
return result.result;
|
|
1217
|
+
}
|
|
1405
1218
|
}
|
|
1219
|
+
// return invalid
|
|
1220
|
+
const unionErrors = results.map((result) => new ZodError_1.ZodError(result.ctx.issues));
|
|
1221
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1222
|
+
code: ZodError_1.ZodIssueCode.invalid_union,
|
|
1223
|
+
unionErrors,
|
|
1224
|
+
});
|
|
1406
1225
|
return parseUtil_1.INVALID;
|
|
1407
|
-
}
|
|
1226
|
+
}
|
|
1408
1227
|
if (ctx.async) {
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
}
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
try {
|
|
1425
|
-
if (parsedOptions_1_1 && !parsedOptions_1_1.done && (_a = parsedOptions_1.return)) _a.call(parsedOptions_1);
|
|
1426
|
-
}
|
|
1427
|
-
finally { if (e_10) throw e_10.error; }
|
|
1428
|
-
}
|
|
1429
|
-
return noMatch(contexts_1.map(function (ctx) { return ctx.issues; }));
|
|
1430
|
-
});
|
|
1228
|
+
return Promise.all(options.map(async (option) => {
|
|
1229
|
+
const childCtx = {
|
|
1230
|
+
...ctx,
|
|
1231
|
+
issues: [],
|
|
1232
|
+
parent: null,
|
|
1233
|
+
};
|
|
1234
|
+
return {
|
|
1235
|
+
result: await option._parseAsync({
|
|
1236
|
+
data: ctx.data,
|
|
1237
|
+
path: ctx.path,
|
|
1238
|
+
parent: childCtx,
|
|
1239
|
+
}),
|
|
1240
|
+
ctx: childCtx,
|
|
1241
|
+
};
|
|
1242
|
+
})).then(handleResults);
|
|
1431
1243
|
}
|
|
1432
1244
|
else {
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
}
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
try {
|
|
1450
|
-
if (options_1_1 && !options_1_1.done && (_a = options_1.return)) _a.call(options_1);
|
|
1451
|
-
}
|
|
1452
|
-
finally { if (e_9) throw e_9.error; }
|
|
1453
|
-
}
|
|
1454
|
-
return noMatch(allIssues);
|
|
1245
|
+
const optionResults = options.map((option) => {
|
|
1246
|
+
const childCtx = {
|
|
1247
|
+
...ctx,
|
|
1248
|
+
issues: [],
|
|
1249
|
+
parent: null,
|
|
1250
|
+
};
|
|
1251
|
+
return {
|
|
1252
|
+
result: option._parseSync({
|
|
1253
|
+
data: ctx.data,
|
|
1254
|
+
path: ctx.path,
|
|
1255
|
+
parent: childCtx,
|
|
1256
|
+
}),
|
|
1257
|
+
ctx: childCtx,
|
|
1258
|
+
};
|
|
1259
|
+
});
|
|
1260
|
+
return handleResults(optionResults);
|
|
1455
1261
|
}
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
enumerable: false,
|
|
1462
|
-
configurable: true
|
|
1463
|
-
});
|
|
1464
|
-
ZodUnion.create = function (types, params) {
|
|
1465
|
-
return new ZodUnion(__assign({ options: types, typeName: ZodFirstPartyTypeKind.ZodUnion }, processCreateParams(params)));
|
|
1466
|
-
};
|
|
1467
|
-
return ZodUnion;
|
|
1468
|
-
}(ZodType));
|
|
1262
|
+
}
|
|
1263
|
+
get options() {
|
|
1264
|
+
return this._def.options;
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1469
1267
|
exports.ZodUnion = ZodUnion;
|
|
1268
|
+
ZodUnion.create = (types, params) => {
|
|
1269
|
+
return new ZodUnion({
|
|
1270
|
+
options: types,
|
|
1271
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
1272
|
+
...processCreateParams(params),
|
|
1273
|
+
});
|
|
1274
|
+
};
|
|
1470
1275
|
function mergeValues(a, b) {
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
var bType = parseUtil_1.getParsedType(b);
|
|
1276
|
+
const aType = (0, parseUtil_1.getParsedType)(a);
|
|
1277
|
+
const bType = (0, parseUtil_1.getParsedType)(b);
|
|
1474
1278
|
if (a === b) {
|
|
1475
1279
|
return { valid: true, data: a };
|
|
1476
1280
|
}
|
|
1477
1281
|
else if (aType === parseUtil_1.ZodParsedType.object && bType === parseUtil_1.ZodParsedType.object) {
|
|
1478
|
-
|
|
1479
|
-
|
|
1282
|
+
const bKeys = util_1.util.objectKeys(b);
|
|
1283
|
+
const sharedKeys = util_1.util
|
|
1480
1284
|
.objectKeys(a)
|
|
1481
|
-
.filter(
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
if (!sharedValue.valid) {
|
|
1488
|
-
return { valid: false };
|
|
1489
|
-
}
|
|
1490
|
-
newObj[key] = sharedValue.data;
|
|
1285
|
+
.filter((key) => bKeys.indexOf(key) !== -1);
|
|
1286
|
+
const newObj = { ...a, ...b };
|
|
1287
|
+
for (const key of sharedKeys) {
|
|
1288
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
1289
|
+
if (!sharedValue.valid) {
|
|
1290
|
+
return { valid: false };
|
|
1491
1291
|
}
|
|
1292
|
+
newObj[key] = sharedValue.data;
|
|
1293
|
+
}
|
|
1294
|
+
return { valid: true, data: newObj };
|
|
1295
|
+
}
|
|
1296
|
+
else if (aType === parseUtil_1.ZodParsedType.array && bType === parseUtil_1.ZodParsedType.array) {
|
|
1297
|
+
if (a.length !== b.length) {
|
|
1298
|
+
return { valid: false };
|
|
1492
1299
|
}
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1300
|
+
const newArray = [];
|
|
1301
|
+
for (let index = 0; index < a.length; index++) {
|
|
1302
|
+
const itemA = a[index];
|
|
1303
|
+
const itemB = b[index];
|
|
1304
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
1305
|
+
if (!sharedValue.valid) {
|
|
1306
|
+
return { valid: false };
|
|
1497
1307
|
}
|
|
1498
|
-
|
|
1308
|
+
newArray.push(sharedValue.data);
|
|
1499
1309
|
}
|
|
1500
|
-
return { valid: true, data:
|
|
1310
|
+
return { valid: true, data: newArray };
|
|
1501
1311
|
}
|
|
1502
1312
|
else {
|
|
1503
1313
|
return { valid: false };
|
|
1504
1314
|
}
|
|
1505
1315
|
}
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
ZodIntersection.prototype._parse = function (ctx, data, parsedType) {
|
|
1512
|
-
var _this = this;
|
|
1513
|
-
var handleParsed = function (parsedLeft, parsedRight) {
|
|
1514
|
-
if (parseUtil_1.isInvalid(parsedLeft) || parseUtil_1.isInvalid(parsedRight)) {
|
|
1316
|
+
class ZodIntersection extends ZodType {
|
|
1317
|
+
_parse(input) {
|
|
1318
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1319
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
1320
|
+
if ((0, parseUtil_1.isAborted)(parsedLeft) || (0, parseUtil_1.isAborted)(parsedRight)) {
|
|
1515
1321
|
return parseUtil_1.INVALID;
|
|
1516
1322
|
}
|
|
1517
|
-
|
|
1323
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
1518
1324
|
if (!merged.valid) {
|
|
1519
|
-
|
|
1325
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1520
1326
|
code: ZodError_1.ZodIssueCode.invalid_intersection_types,
|
|
1521
|
-
}
|
|
1327
|
+
});
|
|
1522
1328
|
return parseUtil_1.INVALID;
|
|
1523
1329
|
}
|
|
1524
|
-
|
|
1330
|
+
if ((0, parseUtil_1.isDirty)(parsedLeft) || (0, parseUtil_1.isDirty)(parsedRight)) {
|
|
1331
|
+
status.dirty();
|
|
1332
|
+
}
|
|
1333
|
+
return { status: status.value, value: merged.data };
|
|
1525
1334
|
};
|
|
1526
1335
|
if (ctx.async) {
|
|
1527
1336
|
return Promise.all([
|
|
1528
|
-
this._def.left.
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1337
|
+
this._def.left._parseAsync({
|
|
1338
|
+
data: ctx.data,
|
|
1339
|
+
path: ctx.path,
|
|
1340
|
+
parent: ctx,
|
|
1341
|
+
}),
|
|
1342
|
+
this._def.right._parseAsync({
|
|
1343
|
+
data: ctx.data,
|
|
1344
|
+
path: ctx.path,
|
|
1345
|
+
parent: ctx,
|
|
1346
|
+
}),
|
|
1347
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
1534
1348
|
}
|
|
1535
1349
|
else {
|
|
1536
|
-
return handleParsed(this._def.left._parseSync(
|
|
1350
|
+
return handleParsed(this._def.left._parseSync({
|
|
1351
|
+
data: ctx.data,
|
|
1352
|
+
path: ctx.path,
|
|
1353
|
+
parent: ctx,
|
|
1354
|
+
}), this._def.right._parseSync({
|
|
1355
|
+
data: ctx.data,
|
|
1356
|
+
path: ctx.path,
|
|
1357
|
+
parent: ctx,
|
|
1358
|
+
}));
|
|
1537
1359
|
}
|
|
1538
|
-
}
|
|
1539
|
-
|
|
1540
|
-
return new ZodIntersection(__assign({ left: left, right: right, typeName: ZodFirstPartyTypeKind.ZodIntersection }, processCreateParams(params)));
|
|
1541
|
-
};
|
|
1542
|
-
return ZodIntersection;
|
|
1543
|
-
}(ZodType));
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1544
1362
|
exports.ZodIntersection = ZodIntersection;
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1363
|
+
ZodIntersection.create = (left, right, params) => {
|
|
1364
|
+
return new ZodIntersection({
|
|
1365
|
+
left: left,
|
|
1366
|
+
right: right,
|
|
1367
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
1368
|
+
...processCreateParams(params),
|
|
1369
|
+
});
|
|
1370
|
+
};
|
|
1371
|
+
class ZodTuple extends ZodType {
|
|
1372
|
+
_parse(input) {
|
|
1373
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1374
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.array) {
|
|
1375
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1553
1376
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1554
1377
|
expected: parseUtil_1.ZodParsedType.array,
|
|
1555
|
-
received: parsedType,
|
|
1556
|
-
}
|
|
1557
|
-
return parseUtil_1.INVALID;
|
|
1558
|
-
}
|
|
1559
|
-
var rest = this._def.rest;
|
|
1560
|
-
if (!rest && data.length > this._def.items.length) {
|
|
1561
|
-
this.addIssue(ctx, {
|
|
1562
|
-
code: ZodError_1.ZodIssueCode.too_big,
|
|
1563
|
-
maximum: this._def.items.length,
|
|
1564
|
-
inclusive: true,
|
|
1565
|
-
type: "array",
|
|
1566
|
-
}, { data: data });
|
|
1378
|
+
received: ctx.parsedType,
|
|
1379
|
+
});
|
|
1567
1380
|
return parseUtil_1.INVALID;
|
|
1568
1381
|
}
|
|
1569
|
-
if (data.length < this._def.items.length) {
|
|
1570
|
-
|
|
1382
|
+
if (ctx.data.length < this._def.items.length) {
|
|
1383
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1571
1384
|
code: ZodError_1.ZodIssueCode.too_small,
|
|
1572
1385
|
minimum: this._def.items.length,
|
|
1573
1386
|
inclusive: true,
|
|
1574
1387
|
type: "array",
|
|
1575
|
-
}
|
|
1388
|
+
});
|
|
1576
1389
|
return parseUtil_1.INVALID;
|
|
1577
1390
|
}
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
}
|
|
1586
|
-
else if (parseUtil_1.isInvalid(parsedItem)) {
|
|
1587
|
-
invalid = true;
|
|
1588
|
-
}
|
|
1589
|
-
else {
|
|
1590
|
-
tasks.push(parsedItem.then(function (parsed) { return handleParsed(index, parsed); }));
|
|
1591
|
-
}
|
|
1592
|
-
};
|
|
1593
|
-
items.forEach(function (item, index) {
|
|
1594
|
-
handleParsed(index, item._parse(ctx.stepInto(index), data[index], parseUtil_1.getParsedType(data[index])));
|
|
1595
|
-
});
|
|
1596
|
-
if (rest) {
|
|
1597
|
-
var restData = data.slice(items.length);
|
|
1598
|
-
restData.forEach(function (dataItem, _index) {
|
|
1599
|
-
var index = _index + items.length;
|
|
1600
|
-
handleParsed(index, rest._parse(ctx.stepInto(index), dataItem, parseUtil_1.getParsedType(dataItem)));
|
|
1391
|
+
const rest = this._def.rest;
|
|
1392
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
1393
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1394
|
+
code: ZodError_1.ZodIssueCode.too_big,
|
|
1395
|
+
maximum: this._def.items.length,
|
|
1396
|
+
inclusive: true,
|
|
1397
|
+
type: "array",
|
|
1601
1398
|
});
|
|
1602
|
-
|
|
1399
|
+
status.dirty();
|
|
1400
|
+
}
|
|
1401
|
+
const items = ctx.data
|
|
1402
|
+
.map((item, itemIndex) => {
|
|
1403
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
1404
|
+
if (!schema)
|
|
1405
|
+
return null;
|
|
1406
|
+
return schema._parse({
|
|
1407
|
+
data: item,
|
|
1408
|
+
path: [...ctx.path, itemIndex],
|
|
1409
|
+
parent: ctx,
|
|
1410
|
+
});
|
|
1411
|
+
})
|
|
1412
|
+
.filter((x) => !!x); // filter nulls
|
|
1603
1413
|
if (ctx.async) {
|
|
1604
|
-
return Promise.all(
|
|
1605
|
-
return
|
|
1414
|
+
return Promise.all(items).then((results) => {
|
|
1415
|
+
return parseUtil_1.ParseStatus.mergeArray(status, results);
|
|
1606
1416
|
});
|
|
1607
1417
|
}
|
|
1608
1418
|
else {
|
|
1609
|
-
return
|
|
1419
|
+
return parseUtil_1.ParseStatus.mergeArray(status, items);
|
|
1610
1420
|
}
|
|
1611
|
-
}
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
ZodTuple.create = function (schemas, params) {
|
|
1623
|
-
return new ZodTuple(__assign({ items: schemas, typeName: ZodFirstPartyTypeKind.ZodTuple, rest: null }, processCreateParams(params)));
|
|
1624
|
-
};
|
|
1625
|
-
return ZodTuple;
|
|
1626
|
-
}(ZodType));
|
|
1421
|
+
}
|
|
1422
|
+
get items() {
|
|
1423
|
+
return this._def.items;
|
|
1424
|
+
}
|
|
1425
|
+
rest(rest) {
|
|
1426
|
+
return new ZodTuple({
|
|
1427
|
+
...this._def,
|
|
1428
|
+
rest,
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1627
1432
|
exports.ZodTuple = ZodTuple;
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
get: function () {
|
|
1635
|
-
return this._def.keyType;
|
|
1636
|
-
},
|
|
1637
|
-
enumerable: false,
|
|
1638
|
-
configurable: true
|
|
1433
|
+
ZodTuple.create = (schemas, params) => {
|
|
1434
|
+
return new ZodTuple({
|
|
1435
|
+
items: schemas,
|
|
1436
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
1437
|
+
rest: null,
|
|
1438
|
+
...processCreateParams(params),
|
|
1639
1439
|
});
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1440
|
+
};
|
|
1441
|
+
class ZodRecord extends ZodType {
|
|
1442
|
+
get keySchema() {
|
|
1443
|
+
return this._def.keyType;
|
|
1444
|
+
}
|
|
1445
|
+
get valueSchema() {
|
|
1446
|
+
return this._def.valueType;
|
|
1447
|
+
}
|
|
1448
|
+
_parse(input) {
|
|
1449
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1450
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.object) {
|
|
1451
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1650
1452
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1651
1453
|
expected: parseUtil_1.ZodParsedType.object,
|
|
1652
|
-
received: parsedType,
|
|
1653
|
-
}
|
|
1454
|
+
received: ctx.parsedType,
|
|
1455
|
+
});
|
|
1654
1456
|
return parseUtil_1.INVALID;
|
|
1655
1457
|
}
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
invalid = true;
|
|
1673
|
-
}
|
|
1674
|
-
};
|
|
1675
|
-
for (var key in data) {
|
|
1676
|
-
handleParsed(keyType._parse(ctx.stepInto(key), key, parseUtil_1.getParsedType(key)), valueType._parse(ctx.stepInto(key), data[key], parseUtil_1.getParsedType(data[key])));
|
|
1458
|
+
const pairs = [];
|
|
1459
|
+
const keyType = this._def.keyType;
|
|
1460
|
+
const valueType = this._def.valueType;
|
|
1461
|
+
for (const key in ctx.data) {
|
|
1462
|
+
pairs.push({
|
|
1463
|
+
key: keyType._parse({
|
|
1464
|
+
data: key,
|
|
1465
|
+
path: [...ctx.path, key],
|
|
1466
|
+
parent: ctx,
|
|
1467
|
+
}),
|
|
1468
|
+
value: valueType._parse({
|
|
1469
|
+
data: ctx.data[key],
|
|
1470
|
+
path: [...ctx.path, key],
|
|
1471
|
+
parent: ctx,
|
|
1472
|
+
}),
|
|
1473
|
+
});
|
|
1677
1474
|
}
|
|
1678
1475
|
if (ctx.async) {
|
|
1679
|
-
return
|
|
1680
|
-
return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parseResult);
|
|
1681
|
-
});
|
|
1476
|
+
return parseUtil_1.ParseStatus.mergeObjectAsync(status, pairs);
|
|
1682
1477
|
}
|
|
1683
1478
|
else {
|
|
1684
|
-
return
|
|
1479
|
+
return parseUtil_1.ParseStatus.mergeObjectSync(status, pairs);
|
|
1685
1480
|
}
|
|
1686
|
-
}
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
enumerable: false,
|
|
1692
|
-
configurable: true
|
|
1693
|
-
});
|
|
1694
|
-
ZodRecord.create = function (first, second, third) {
|
|
1481
|
+
}
|
|
1482
|
+
get element() {
|
|
1483
|
+
return this._def.valueType;
|
|
1484
|
+
}
|
|
1485
|
+
static create(first, second, third) {
|
|
1695
1486
|
if (second instanceof ZodType) {
|
|
1696
|
-
return new ZodRecord(
|
|
1487
|
+
return new ZodRecord({
|
|
1488
|
+
keyType: first,
|
|
1489
|
+
valueType: second,
|
|
1490
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
1491
|
+
...processCreateParams(third),
|
|
1492
|
+
});
|
|
1697
1493
|
}
|
|
1698
|
-
return new ZodRecord(
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1494
|
+
return new ZodRecord({
|
|
1495
|
+
keyType: ZodString.create(),
|
|
1496
|
+
valueType: first,
|
|
1497
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
1498
|
+
...processCreateParams(second),
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1702
1502
|
exports.ZodRecord = ZodRecord;
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
ZodMap.prototype._parse = function (ctx, data, parsedType) {
|
|
1709
|
-
if (parsedType !== parseUtil_1.ZodParsedType.map) {
|
|
1710
|
-
this.addIssue(ctx, {
|
|
1503
|
+
class ZodMap extends ZodType {
|
|
1504
|
+
_parse(input) {
|
|
1505
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1506
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.map) {
|
|
1507
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1711
1508
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1712
1509
|
expected: parseUtil_1.ZodParsedType.map,
|
|
1713
|
-
received: parsedType,
|
|
1714
|
-
}
|
|
1510
|
+
received: ctx.parsedType,
|
|
1511
|
+
});
|
|
1715
1512
|
return parseUtil_1.INVALID;
|
|
1716
1513
|
}
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
}
|
|
1733
|
-
else {
|
|
1734
|
-
parseResult.set(parsedKey.value, parsedValue.value);
|
|
1735
|
-
}
|
|
1736
|
-
};
|
|
1737
|
-
__spread(dataMap.entries()).forEach(function (_a, index) {
|
|
1738
|
-
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
1739
|
-
var entryCtx = ctx.stepInto(index);
|
|
1740
|
-
var parsedKey = keyType._parse(entryCtx.stepInto("key"), key, parseUtil_1.getParsedType(key));
|
|
1741
|
-
var parsedValue = valueType._parse(entryCtx.stepInto("value"), value, parseUtil_1.getParsedType(value));
|
|
1742
|
-
handleParsed(parsedKey, parsedValue);
|
|
1514
|
+
const keyType = this._def.keyType;
|
|
1515
|
+
const valueType = this._def.valueType;
|
|
1516
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
1517
|
+
return {
|
|
1518
|
+
key: keyType._parse({
|
|
1519
|
+
data: key,
|
|
1520
|
+
path: [...ctx.path, index, "key"],
|
|
1521
|
+
parent: ctx,
|
|
1522
|
+
}),
|
|
1523
|
+
value: valueType._parse({
|
|
1524
|
+
data: value,
|
|
1525
|
+
path: [...ctx.path, index, "value"],
|
|
1526
|
+
parent: ctx,
|
|
1527
|
+
}),
|
|
1528
|
+
};
|
|
1743
1529
|
});
|
|
1744
1530
|
if (ctx.async) {
|
|
1745
|
-
|
|
1746
|
-
|
|
1531
|
+
const finalMap = new Map();
|
|
1532
|
+
return Promise.resolve().then(async () => {
|
|
1533
|
+
for (const pair of pairs) {
|
|
1534
|
+
const key = await pair.key;
|
|
1535
|
+
const value = await pair.value;
|
|
1536
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
1537
|
+
return parseUtil_1.INVALID;
|
|
1538
|
+
}
|
|
1539
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
1540
|
+
status.dirty();
|
|
1541
|
+
}
|
|
1542
|
+
finalMap.set(key.value, value.value);
|
|
1543
|
+
}
|
|
1544
|
+
return { status: status.value, value: finalMap };
|
|
1747
1545
|
});
|
|
1748
1546
|
}
|
|
1749
1547
|
else {
|
|
1750
|
-
|
|
1548
|
+
const finalMap = new Map();
|
|
1549
|
+
for (const pair of pairs) {
|
|
1550
|
+
const key = pair.key;
|
|
1551
|
+
const value = pair.value;
|
|
1552
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
1553
|
+
return parseUtil_1.INVALID;
|
|
1554
|
+
}
|
|
1555
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
1556
|
+
status.dirty();
|
|
1557
|
+
}
|
|
1558
|
+
finalMap.set(key.value, value.value);
|
|
1559
|
+
}
|
|
1560
|
+
return { status: status.value, value: finalMap };
|
|
1751
1561
|
}
|
|
1752
|
-
}
|
|
1753
|
-
|
|
1754
|
-
return new ZodMap(__assign({ valueType: valueType,
|
|
1755
|
-
keyType: keyType, typeName: ZodFirstPartyTypeKind.ZodMap }, processCreateParams(params)));
|
|
1756
|
-
};
|
|
1757
|
-
return ZodMap;
|
|
1758
|
-
}(ZodType));
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1759
1564
|
exports.ZodMap = ZodMap;
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1565
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
1566
|
+
return new ZodMap({
|
|
1567
|
+
valueType,
|
|
1568
|
+
keyType,
|
|
1569
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
1570
|
+
...processCreateParams(params),
|
|
1571
|
+
});
|
|
1572
|
+
};
|
|
1573
|
+
class ZodSet extends ZodType {
|
|
1574
|
+
_parse(input) {
|
|
1575
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1576
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.set) {
|
|
1577
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1768
1578
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1769
1579
|
expected: parseUtil_1.ZodParsedType.set,
|
|
1770
|
-
received: parsedType,
|
|
1771
|
-
}
|
|
1580
|
+
received: ctx.parsedType,
|
|
1581
|
+
});
|
|
1772
1582
|
return parseUtil_1.INVALID;
|
|
1773
1583
|
}
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
else if (parseUtil_1.isInvalid(parsedItem)) {
|
|
1784
|
-
invalid = true;
|
|
1785
|
-
}
|
|
1786
|
-
else {
|
|
1787
|
-
tasks.push(parsedItem.then(function (parsed) { return handleParsed(parsed); }));
|
|
1584
|
+
const valueType = this._def.valueType;
|
|
1585
|
+
function finalizeSet(elements) {
|
|
1586
|
+
const parsedSet = new Set();
|
|
1587
|
+
for (const element of elements) {
|
|
1588
|
+
if (element.status === "aborted")
|
|
1589
|
+
return parseUtil_1.INVALID;
|
|
1590
|
+
if (element.status === "dirty")
|
|
1591
|
+
status.dirty();
|
|
1592
|
+
parsedSet.add(element.value);
|
|
1788
1593
|
}
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
});
|
|
1594
|
+
return { status: status.value, value: parsedSet };
|
|
1595
|
+
}
|
|
1596
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
|
|
1793
1597
|
if (ctx.async) {
|
|
1794
|
-
return Promise.all(
|
|
1598
|
+
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
1795
1599
|
}
|
|
1796
1600
|
else {
|
|
1797
|
-
return
|
|
1601
|
+
return finalizeSet(elements);
|
|
1798
1602
|
}
|
|
1799
|
-
}
|
|
1800
|
-
|
|
1801
|
-
return new ZodSet(__assign({ valueType: valueType, typeName: ZodFirstPartyTypeKind.ZodSet }, processCreateParams(params)));
|
|
1802
|
-
};
|
|
1803
|
-
return ZodSet;
|
|
1804
|
-
}(ZodType));
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1805
1605
|
exports.ZodSet = ZodSet;
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1606
|
+
ZodSet.create = (valueType, params) => {
|
|
1607
|
+
return new ZodSet({
|
|
1608
|
+
valueType,
|
|
1609
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
1610
|
+
...processCreateParams(params),
|
|
1611
|
+
});
|
|
1612
|
+
};
|
|
1613
|
+
class ZodFunction extends ZodType {
|
|
1614
|
+
constructor() {
|
|
1615
|
+
super(...arguments);
|
|
1616
|
+
this.validate = this.implement;
|
|
1617
|
+
}
|
|
1618
|
+
_parse(input) {
|
|
1619
|
+
const { ctx } = this._processInputParams(input);
|
|
1620
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.function) {
|
|
1621
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1817
1622
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1818
1623
|
expected: parseUtil_1.ZodParsedType.function,
|
|
1819
|
-
received: parsedType,
|
|
1820
|
-
}
|
|
1624
|
+
received: ctx.parsedType,
|
|
1625
|
+
});
|
|
1821
1626
|
return parseUtil_1.INVALID;
|
|
1822
1627
|
}
|
|
1823
1628
|
function makeArgsIssue(args, error) {
|
|
1824
|
-
return parseUtil_1.makeIssue({
|
|
1629
|
+
return (0, parseUtil_1.makeIssue)({
|
|
1825
1630
|
data: args,
|
|
1826
|
-
path:
|
|
1827
|
-
errorMaps: [
|
|
1631
|
+
path: ctx.path,
|
|
1632
|
+
errorMaps: [
|
|
1633
|
+
ctx.contextualErrorMap,
|
|
1634
|
+
ctx.schemaErrorMap,
|
|
1635
|
+
ZodError_1.overrideErrorMap,
|
|
1636
|
+
ZodError_1.defaultErrorMap,
|
|
1637
|
+
].filter((x) => !!x),
|
|
1828
1638
|
issueData: {
|
|
1829
1639
|
code: ZodError_1.ZodIssueCode.invalid_arguments,
|
|
1830
1640
|
argumentsError: error,
|
|
@@ -1832,476 +1642,446 @@ var ZodFunction = /** @class */ (function (_super) {
|
|
|
1832
1642
|
});
|
|
1833
1643
|
}
|
|
1834
1644
|
function makeReturnsIssue(returns, error) {
|
|
1835
|
-
return parseUtil_1.makeIssue({
|
|
1645
|
+
return (0, parseUtil_1.makeIssue)({
|
|
1836
1646
|
data: returns,
|
|
1837
|
-
path:
|
|
1838
|
-
errorMaps: [
|
|
1647
|
+
path: ctx.path,
|
|
1648
|
+
errorMaps: [
|
|
1649
|
+
ctx.contextualErrorMap,
|
|
1650
|
+
ctx.schemaErrorMap,
|
|
1651
|
+
ZodError_1.overrideErrorMap,
|
|
1652
|
+
ZodError_1.defaultErrorMap,
|
|
1653
|
+
].filter((x) => !!x),
|
|
1839
1654
|
issueData: {
|
|
1840
1655
|
code: ZodError_1.ZodIssueCode.invalid_return_type,
|
|
1841
1656
|
returnTypeError: error,
|
|
1842
1657
|
},
|
|
1843
1658
|
});
|
|
1844
1659
|
}
|
|
1845
|
-
|
|
1846
|
-
|
|
1660
|
+
const params = { errorMap: ctx.contextualErrorMap };
|
|
1661
|
+
const fn = ctx.data;
|
|
1847
1662
|
if (this._def.returns instanceof ZodPromise) {
|
|
1848
|
-
return parseUtil_1.OK(
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
args
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
return __generator(this, function (_a) {
|
|
1856
|
-
switch (_a.label) {
|
|
1857
|
-
case 0:
|
|
1858
|
-
error = new ZodError_1.ZodError([]);
|
|
1859
|
-
return [4 /*yield*/, this._def.args
|
|
1860
|
-
.parseAsync(args, params)
|
|
1861
|
-
.catch(function (e) {
|
|
1862
|
-
error.addIssue(makeArgsIssue(args, e));
|
|
1863
|
-
throw error;
|
|
1864
|
-
})];
|
|
1865
|
-
case 1:
|
|
1866
|
-
parsedArgs = _a.sent();
|
|
1867
|
-
return [4 /*yield*/, fn.apply(void 0, __spread(parsedArgs))];
|
|
1868
|
-
case 2:
|
|
1869
|
-
result = _a.sent();
|
|
1870
|
-
return [4 /*yield*/, this._def.returns
|
|
1871
|
-
.parseAsync(result, params)
|
|
1872
|
-
.catch(function (e) {
|
|
1873
|
-
error.addIssue(makeReturnsIssue(result, e));
|
|
1874
|
-
throw error;
|
|
1875
|
-
})];
|
|
1876
|
-
case 3:
|
|
1877
|
-
parsedReturns = _a.sent();
|
|
1878
|
-
return [2 /*return*/, parsedReturns];
|
|
1879
|
-
}
|
|
1880
|
-
});
|
|
1663
|
+
return (0, parseUtil_1.OK)(async (...args) => {
|
|
1664
|
+
const error = new ZodError_1.ZodError([]);
|
|
1665
|
+
const parsedArgs = await this._def.args
|
|
1666
|
+
.parseAsync(args, params)
|
|
1667
|
+
.catch((e) => {
|
|
1668
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
1669
|
+
throw error;
|
|
1881
1670
|
});
|
|
1671
|
+
const result = await fn(...parsedArgs);
|
|
1672
|
+
const parsedReturns = await this._def
|
|
1673
|
+
.returns._def.type
|
|
1674
|
+
.parseAsync(result, params)
|
|
1675
|
+
.catch((e) => {
|
|
1676
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
1677
|
+
throw error;
|
|
1678
|
+
});
|
|
1679
|
+
return parsedReturns;
|
|
1882
1680
|
});
|
|
1883
1681
|
}
|
|
1884
1682
|
else {
|
|
1885
|
-
return parseUtil_1.OK(
|
|
1886
|
-
|
|
1887
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
1888
|
-
args[_i] = arguments[_i];
|
|
1889
|
-
}
|
|
1890
|
-
var parsedArgs = _this._def.args.safeParse(args, params);
|
|
1683
|
+
return (0, parseUtil_1.OK)((...args) => {
|
|
1684
|
+
const parsedArgs = this._def.args.safeParse(args, params);
|
|
1891
1685
|
if (!parsedArgs.success) {
|
|
1892
1686
|
throw new ZodError_1.ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
1893
1687
|
}
|
|
1894
|
-
|
|
1895
|
-
|
|
1688
|
+
const result = fn(...parsedArgs.data);
|
|
1689
|
+
const parsedReturns = this._def.returns.safeParse(result, params);
|
|
1896
1690
|
if (!parsedReturns.success) {
|
|
1897
1691
|
throw new ZodError_1.ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
1898
1692
|
}
|
|
1899
1693
|
return parsedReturns.data;
|
|
1900
1694
|
});
|
|
1901
1695
|
}
|
|
1902
|
-
}
|
|
1903
|
-
|
|
1696
|
+
}
|
|
1697
|
+
parameters() {
|
|
1904
1698
|
return this._def.args;
|
|
1905
|
-
}
|
|
1906
|
-
|
|
1699
|
+
}
|
|
1700
|
+
returnType() {
|
|
1907
1701
|
return this._def.returns;
|
|
1908
|
-
}
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
items
|
|
1913
|
-
}
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1702
|
+
}
|
|
1703
|
+
args(...items) {
|
|
1704
|
+
return new ZodFunction({
|
|
1705
|
+
...this._def,
|
|
1706
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create()),
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
returns(returnType) {
|
|
1710
|
+
return new ZodFunction({
|
|
1711
|
+
...this._def,
|
|
1712
|
+
returns: returnType,
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
implement(func) {
|
|
1716
|
+
const validatedFunc = this.parse(func);
|
|
1921
1717
|
return validatedFunc;
|
|
1922
|
-
}
|
|
1923
|
-
|
|
1924
|
-
|
|
1718
|
+
}
|
|
1719
|
+
strictImplement(func) {
|
|
1720
|
+
const validatedFunc = this.parse(func);
|
|
1925
1721
|
return validatedFunc;
|
|
1926
|
-
}
|
|
1927
|
-
|
|
1928
|
-
return new ZodFunction(__assign({ args: (args
|
|
1929
|
-
? args.rest(ZodUnknown.create())
|
|
1930
|
-
: ZodTuple.create([]).rest(ZodUnknown.create())), returns: returns || ZodUnknown.create(), typeName: ZodFirstPartyTypeKind.ZodFunction }, processCreateParams(params)));
|
|
1931
|
-
};
|
|
1932
|
-
return ZodFunction;
|
|
1933
|
-
}(ZodType));
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1934
1724
|
exports.ZodFunction = ZodFunction;
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
},
|
|
1944
|
-
enumerable: false,
|
|
1945
|
-
configurable: true
|
|
1725
|
+
ZodFunction.create = (args, returns, params) => {
|
|
1726
|
+
return new ZodFunction({
|
|
1727
|
+
args: (args
|
|
1728
|
+
? args.rest(ZodUnknown.create())
|
|
1729
|
+
: ZodTuple.create([]).rest(ZodUnknown.create())),
|
|
1730
|
+
returns: returns || ZodUnknown.create(),
|
|
1731
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
1732
|
+
...processCreateParams(params),
|
|
1946
1733
|
});
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
}
|
|
1734
|
+
};
|
|
1735
|
+
class ZodLazy extends ZodType {
|
|
1736
|
+
get schema() {
|
|
1737
|
+
return this._def.getter();
|
|
1738
|
+
}
|
|
1739
|
+
_parse(input) {
|
|
1740
|
+
const { ctx } = this._processInputParams(input);
|
|
1741
|
+
const lazySchema = this._def.getter();
|
|
1742
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1956
1745
|
exports.ZodLazy = ZodLazy;
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1746
|
+
ZodLazy.create = (getter, params) => {
|
|
1747
|
+
return new ZodLazy({
|
|
1748
|
+
getter: getter,
|
|
1749
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
1750
|
+
...processCreateParams(params),
|
|
1751
|
+
});
|
|
1752
|
+
};
|
|
1753
|
+
class ZodLiteral extends ZodType {
|
|
1754
|
+
_parse(input) {
|
|
1755
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1756
|
+
if (ctx.data !== this._def.value) {
|
|
1757
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
1965
1758
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
1966
1759
|
expected: this._def.value,
|
|
1967
|
-
received: data,
|
|
1968
|
-
}
|
|
1760
|
+
received: ctx.data,
|
|
1761
|
+
});
|
|
1969
1762
|
return parseUtil_1.INVALID;
|
|
1970
1763
|
}
|
|
1971
|
-
return
|
|
1972
|
-
}
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1977
|
-
enumerable: false,
|
|
1978
|
-
configurable: true
|
|
1979
|
-
});
|
|
1980
|
-
ZodLiteral.create = function (value, params) {
|
|
1981
|
-
return new ZodLiteral(__assign({ value: value, typeName: ZodFirstPartyTypeKind.ZodLiteral }, processCreateParams(params)));
|
|
1982
|
-
};
|
|
1983
|
-
return ZodLiteral;
|
|
1984
|
-
}(ZodType));
|
|
1764
|
+
return { status: status.value, value: ctx.data };
|
|
1765
|
+
}
|
|
1766
|
+
get value() {
|
|
1767
|
+
return this._def.value;
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1985
1770
|
exports.ZodLiteral = ZodLiteral;
|
|
1771
|
+
ZodLiteral.create = (value, params) => {
|
|
1772
|
+
return new ZodLiteral({
|
|
1773
|
+
value: value,
|
|
1774
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
1775
|
+
...processCreateParams(params),
|
|
1776
|
+
});
|
|
1777
|
+
};
|
|
1986
1778
|
function createZodEnum(values) {
|
|
1987
1779
|
return new ZodEnum({
|
|
1988
1780
|
values: values,
|
|
1989
1781
|
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
1990
1782
|
});
|
|
1991
1783
|
}
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
ZodEnum.prototype._parse = function (ctx, data, _parsedType) {
|
|
1998
|
-
if (this._def.values.indexOf(data) === -1) {
|
|
1999
|
-
this.addIssue(ctx, {
|
|
1784
|
+
class ZodEnum extends ZodType {
|
|
1785
|
+
_parse(input) {
|
|
1786
|
+
const { ctx } = this._processInputParams(input);
|
|
1787
|
+
if (this._def.values.indexOf(ctx.data) === -1) {
|
|
1788
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2000
1789
|
code: ZodError_1.ZodIssueCode.invalid_enum_value,
|
|
2001
1790
|
options: this._def.values,
|
|
2002
|
-
}
|
|
1791
|
+
});
|
|
2003
1792
|
return parseUtil_1.INVALID;
|
|
2004
1793
|
}
|
|
2005
|
-
return parseUtil_1.OK(data);
|
|
2006
|
-
}
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
},
|
|
2033
|
-
enumerable: false,
|
|
2034
|
-
configurable: true
|
|
2035
|
-
});
|
|
2036
|
-
Object.defineProperty(ZodEnum.prototype, "Values", {
|
|
2037
|
-
get: function () {
|
|
2038
|
-
var e_13, _a;
|
|
2039
|
-
var enumValues = {};
|
|
2040
|
-
try {
|
|
2041
|
-
for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2042
|
-
var val = _c.value;
|
|
2043
|
-
enumValues[val] = val;
|
|
2044
|
-
}
|
|
2045
|
-
}
|
|
2046
|
-
catch (e_13_1) { e_13 = { error: e_13_1 }; }
|
|
2047
|
-
finally {
|
|
2048
|
-
try {
|
|
2049
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2050
|
-
}
|
|
2051
|
-
finally { if (e_13) throw e_13.error; }
|
|
2052
|
-
}
|
|
2053
|
-
return enumValues;
|
|
2054
|
-
},
|
|
2055
|
-
enumerable: false,
|
|
2056
|
-
configurable: true
|
|
2057
|
-
});
|
|
2058
|
-
Object.defineProperty(ZodEnum.prototype, "Enum", {
|
|
2059
|
-
get: function () {
|
|
2060
|
-
var e_14, _a;
|
|
2061
|
-
var enumValues = {};
|
|
2062
|
-
try {
|
|
2063
|
-
for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
2064
|
-
var val = _c.value;
|
|
2065
|
-
enumValues[val] = val;
|
|
2066
|
-
}
|
|
2067
|
-
}
|
|
2068
|
-
catch (e_14_1) { e_14 = { error: e_14_1 }; }
|
|
2069
|
-
finally {
|
|
2070
|
-
try {
|
|
2071
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
2072
|
-
}
|
|
2073
|
-
finally { if (e_14) throw e_14.error; }
|
|
2074
|
-
}
|
|
2075
|
-
return enumValues;
|
|
2076
|
-
},
|
|
2077
|
-
enumerable: false,
|
|
2078
|
-
configurable: true
|
|
2079
|
-
});
|
|
2080
|
-
ZodEnum.create = createZodEnum;
|
|
2081
|
-
return ZodEnum;
|
|
2082
|
-
}(ZodType));
|
|
1794
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
1795
|
+
}
|
|
1796
|
+
get options() {
|
|
1797
|
+
return this._def.values;
|
|
1798
|
+
}
|
|
1799
|
+
get enum() {
|
|
1800
|
+
const enumValues = {};
|
|
1801
|
+
for (const val of this._def.values) {
|
|
1802
|
+
enumValues[val] = val;
|
|
1803
|
+
}
|
|
1804
|
+
return enumValues;
|
|
1805
|
+
}
|
|
1806
|
+
get Values() {
|
|
1807
|
+
const enumValues = {};
|
|
1808
|
+
for (const val of this._def.values) {
|
|
1809
|
+
enumValues[val] = val;
|
|
1810
|
+
}
|
|
1811
|
+
return enumValues;
|
|
1812
|
+
}
|
|
1813
|
+
get Enum() {
|
|
1814
|
+
const enumValues = {};
|
|
1815
|
+
for (const val of this._def.values) {
|
|
1816
|
+
enumValues[val] = val;
|
|
1817
|
+
}
|
|
1818
|
+
return enumValues;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
2083
1821
|
exports.ZodEnum = ZodEnum;
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
if (nativeEnumValues.indexOf(data) === -1) {
|
|
2092
|
-
this.addIssue(ctx, {
|
|
1822
|
+
ZodEnum.create = createZodEnum;
|
|
1823
|
+
class ZodNativeEnum extends ZodType {
|
|
1824
|
+
_parse(input) {
|
|
1825
|
+
const { ctx } = this._processInputParams(input);
|
|
1826
|
+
const nativeEnumValues = util_1.util.getValidEnumValues(this._def.values);
|
|
1827
|
+
if (nativeEnumValues.indexOf(ctx.data) === -1) {
|
|
1828
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2093
1829
|
code: ZodError_1.ZodIssueCode.invalid_enum_value,
|
|
2094
1830
|
options: util_1.util.objectValues(nativeEnumValues),
|
|
2095
|
-
}
|
|
1831
|
+
});
|
|
2096
1832
|
return parseUtil_1.INVALID;
|
|
2097
1833
|
}
|
|
2098
|
-
return parseUtil_1.OK(data);
|
|
2099
|
-
}
|
|
2100
|
-
|
|
2101
|
-
return new ZodNativeEnum(__assign({ values: values, typeName: ZodFirstPartyTypeKind.ZodNativeEnum }, processCreateParams(params)));
|
|
2102
|
-
};
|
|
2103
|
-
return ZodNativeEnum;
|
|
2104
|
-
}(ZodType));
|
|
1834
|
+
return (0, parseUtil_1.OK)(ctx.data);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
2105
1837
|
exports.ZodNativeEnum = ZodNativeEnum;
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
1838
|
+
ZodNativeEnum.create = (values, params) => {
|
|
1839
|
+
return new ZodNativeEnum({
|
|
1840
|
+
values: values,
|
|
1841
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
1842
|
+
...processCreateParams(params),
|
|
1843
|
+
});
|
|
1844
|
+
};
|
|
1845
|
+
class ZodPromise extends ZodType {
|
|
1846
|
+
_parse(input) {
|
|
1847
|
+
const { ctx } = this._processInputParams(input);
|
|
1848
|
+
if (ctx.parsedType !== parseUtil_1.ZodParsedType.promise && ctx.async === false) {
|
|
1849
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2115
1850
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
2116
1851
|
expected: parseUtil_1.ZodParsedType.promise,
|
|
2117
|
-
received: parsedType,
|
|
2118
|
-
}
|
|
1852
|
+
received: ctx.parsedType,
|
|
1853
|
+
});
|
|
2119
1854
|
return parseUtil_1.INVALID;
|
|
2120
1855
|
}
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
1856
|
+
const promisified = ctx.parsedType === parseUtil_1.ZodParsedType.promise
|
|
1857
|
+
? ctx.data
|
|
1858
|
+
: Promise.resolve(ctx.data);
|
|
1859
|
+
return (0, parseUtil_1.OK)(promisified.then((data) => {
|
|
1860
|
+
return this._def.type.parseAsync(data, {
|
|
1861
|
+
path: ctx.path,
|
|
1862
|
+
errorMap: ctx.contextualErrorMap,
|
|
2126
1863
|
});
|
|
2127
1864
|
}));
|
|
2128
|
-
};
|
|
2129
|
-
ZodPromise.create = function (schema, params) {
|
|
2130
|
-
return new ZodPromise(__assign({ type: schema, typeName: ZodFirstPartyTypeKind.ZodPromise }, processCreateParams(params)));
|
|
2131
|
-
};
|
|
2132
|
-
return ZodPromise;
|
|
2133
|
-
}(ZodType));
|
|
2134
|
-
exports.ZodPromise = ZodPromise;
|
|
2135
|
-
var ZodEffects = /** @class */ (function (_super) {
|
|
2136
|
-
__extends(ZodEffects, _super);
|
|
2137
|
-
function ZodEffects() {
|
|
2138
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2139
1865
|
}
|
|
2140
|
-
|
|
1866
|
+
}
|
|
1867
|
+
exports.ZodPromise = ZodPromise;
|
|
1868
|
+
ZodPromise.create = (schema, params) => {
|
|
1869
|
+
return new ZodPromise({
|
|
1870
|
+
type: schema,
|
|
1871
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
1872
|
+
...processCreateParams(params),
|
|
1873
|
+
});
|
|
1874
|
+
};
|
|
1875
|
+
class ZodEffects extends ZodType {
|
|
1876
|
+
innerType() {
|
|
2141
1877
|
return this._def.schema;
|
|
2142
|
-
}
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
var effect = this._def.effect || null;
|
|
2147
|
-
var data = initialData;
|
|
2148
|
-
var parsedType = initialParsedType;
|
|
1878
|
+
}
|
|
1879
|
+
_parse(input) {
|
|
1880
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1881
|
+
const effect = this._def.effect || null;
|
|
2149
1882
|
if (effect.type === "preprocess") {
|
|
2150
|
-
|
|
1883
|
+
const processed = effect.transform(ctx.data);
|
|
2151
1884
|
if (ctx.async) {
|
|
2152
|
-
return Promise.resolve(processed).then(
|
|
2153
|
-
return
|
|
1885
|
+
return Promise.resolve(processed).then((processed) => {
|
|
1886
|
+
return this._def.schema._parseAsync({
|
|
1887
|
+
data: processed,
|
|
1888
|
+
path: ctx.path,
|
|
1889
|
+
parent: ctx,
|
|
1890
|
+
});
|
|
2154
1891
|
});
|
|
2155
1892
|
}
|
|
2156
1893
|
else {
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
1894
|
+
return this._def.schema._parseSync({
|
|
1895
|
+
data: processed,
|
|
1896
|
+
path: ctx.path,
|
|
1897
|
+
parent: ctx,
|
|
1898
|
+
});
|
|
2161
1899
|
}
|
|
2162
1900
|
}
|
|
2163
1901
|
if (effect.type === "refinement") {
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
1902
|
+
const checkCtx = {
|
|
1903
|
+
addIssue: (arg) => {
|
|
1904
|
+
(0, parseUtil_1.addIssueToContext)(ctx, arg);
|
|
1905
|
+
if (_1.FATAL_CODES.includes(arg.code)) {
|
|
1906
|
+
status.abort();
|
|
2170
1907
|
}
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
var _addIssue = function (arg) {
|
|
2176
|
-
// don't abort early on refinement issues
|
|
2177
|
-
invalid_1 = true;
|
|
2178
|
-
_this.addIssue(ctx, arg, { data: data });
|
|
2179
|
-
};
|
|
2180
|
-
var checkCtx_1 = {
|
|
2181
|
-
addIssue: _addIssue,
|
|
1908
|
+
else {
|
|
1909
|
+
status.dirty();
|
|
1910
|
+
}
|
|
1911
|
+
},
|
|
2182
1912
|
get path() {
|
|
2183
|
-
return
|
|
1913
|
+
return ctx.path;
|
|
2184
1914
|
},
|
|
2185
1915
|
};
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
1916
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
1917
|
+
const executeRefinement = (acc
|
|
1918
|
+
// effect: RefinementEffect<any>
|
|
1919
|
+
) => {
|
|
1920
|
+
const result = effect.refinement(acc, checkCtx);
|
|
1921
|
+
if (ctx.async) {
|
|
1922
|
+
return Promise.resolve(result);
|
|
1923
|
+
}
|
|
1924
|
+
if (result instanceof Promise) {
|
|
1925
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
1926
|
+
}
|
|
1927
|
+
return acc;
|
|
1928
|
+
};
|
|
1929
|
+
if (ctx.async === false) {
|
|
1930
|
+
const base = this._def.schema._parseSync({
|
|
1931
|
+
data: ctx.data,
|
|
1932
|
+
path: ctx.path,
|
|
1933
|
+
parent: ctx,
|
|
1934
|
+
});
|
|
1935
|
+
if (base.status === "aborted")
|
|
2190
1936
|
return parseUtil_1.INVALID;
|
|
2191
|
-
|
|
2192
|
-
|
|
1937
|
+
if (base.status === "dirty")
|
|
1938
|
+
status.dirty();
|
|
1939
|
+
// return value is ignored
|
|
1940
|
+
executeRefinement(base.value);
|
|
1941
|
+
return { status: status.value, value: ctx.data };
|
|
2193
1942
|
}
|
|
2194
1943
|
else {
|
|
2195
1944
|
return this._def.schema
|
|
2196
|
-
._parseAsync(ctx,
|
|
2197
|
-
.then(
|
|
2198
|
-
if (
|
|
1945
|
+
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
1946
|
+
.then((result) => {
|
|
1947
|
+
if (result.status === "aborted")
|
|
2199
1948
|
return parseUtil_1.INVALID;
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
1949
|
+
if (result.status === "dirty")
|
|
1950
|
+
status.dirty();
|
|
1951
|
+
return executeRefinement(result.value).then(() => {
|
|
1952
|
+
return { status: status.value, value: ctx.data };
|
|
1953
|
+
});
|
|
1954
|
+
});
|
|
2203
1955
|
}
|
|
2204
1956
|
}
|
|
2205
1957
|
if (effect.type === "transform") {
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
1958
|
+
if (ctx.async === false) {
|
|
1959
|
+
const base = this._def.schema._parseSync({
|
|
1960
|
+
data: ctx.data,
|
|
1961
|
+
path: ctx.path,
|
|
1962
|
+
parent: ctx,
|
|
1963
|
+
});
|
|
1964
|
+
// if (base.status === "aborted") return INVALID;
|
|
1965
|
+
// if (base.status === "dirty") {
|
|
1966
|
+
// return { status: "dirty", value: base.value };
|
|
1967
|
+
// }
|
|
1968
|
+
if (!(0, parseUtil_1.isValid)(base))
|
|
1969
|
+
return base;
|
|
1970
|
+
const result = effect.transform(base.value);
|
|
1971
|
+
if (result instanceof Promise) {
|
|
1972
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
2211
1973
|
}
|
|
2212
|
-
return
|
|
2213
|
-
};
|
|
2214
|
-
if (isSync) {
|
|
2215
|
-
var base = this._def.schema._parseSync(ctx, data, parsedType);
|
|
2216
|
-
if (parseUtil_1.isInvalid(base))
|
|
2217
|
-
return parseUtil_1.INVALID;
|
|
2218
|
-
var result = applyTransform_1(base.value, effect);
|
|
2219
|
-
return invalid_2 ? parseUtil_1.INVALID : parseUtil_1.OK(result);
|
|
1974
|
+
return (0, parseUtil_1.OK)(result);
|
|
2220
1975
|
}
|
|
2221
1976
|
else {
|
|
2222
1977
|
return this._def.schema
|
|
2223
|
-
._parseAsync(ctx,
|
|
2224
|
-
.then(
|
|
2225
|
-
if (parseUtil_1.
|
|
2226
|
-
return
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
1978
|
+
._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx })
|
|
1979
|
+
.then((base) => {
|
|
1980
|
+
if (!(0, parseUtil_1.isValid)(base))
|
|
1981
|
+
return base;
|
|
1982
|
+
// if (base.status === "aborted") return INVALID;
|
|
1983
|
+
// if (base.status === "dirty") {
|
|
1984
|
+
// return { status: "dirty", value: base.value };
|
|
1985
|
+
// }
|
|
1986
|
+
return Promise.resolve(effect.transform(base.value)).then(parseUtil_1.OK);
|
|
1987
|
+
});
|
|
2230
1988
|
}
|
|
2231
1989
|
}
|
|
2232
1990
|
util_1.util.assertNever(effect);
|
|
2233
|
-
}
|
|
2234
|
-
|
|
2235
|
-
return new ZodEffects(__assign({ schema: schema, typeName: ZodFirstPartyTypeKind.ZodEffects, effect: effect }, processCreateParams(params)));
|
|
2236
|
-
};
|
|
2237
|
-
ZodEffects.createWithPreprocess = function (preprocess, schema, params) {
|
|
2238
|
-
return new ZodEffects(__assign({ schema: schema, effect: { type: "preprocess", transform: preprocess }, typeName: ZodFirstPartyTypeKind.ZodEffects }, processCreateParams(params)));
|
|
2239
|
-
};
|
|
2240
|
-
return ZodEffects;
|
|
2241
|
-
}(ZodType));
|
|
1991
|
+
}
|
|
1992
|
+
}
|
|
2242
1993
|
exports.ZodEffects = ZodEffects;
|
|
2243
1994
|
exports.ZodTransformer = ZodEffects;
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
1995
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
1996
|
+
return new ZodEffects({
|
|
1997
|
+
schema,
|
|
1998
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1999
|
+
effect,
|
|
2000
|
+
...processCreateParams(params),
|
|
2001
|
+
});
|
|
2002
|
+
};
|
|
2003
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
2004
|
+
return new ZodEffects({
|
|
2005
|
+
schema,
|
|
2006
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
2007
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2008
|
+
...processCreateParams(params),
|
|
2009
|
+
});
|
|
2010
|
+
};
|
|
2011
|
+
class ZodOptional extends ZodType {
|
|
2012
|
+
_parse(input) {
|
|
2013
|
+
const { ctx } = this._processInputParams(input);
|
|
2014
|
+
if (ctx.parsedType === parseUtil_1.ZodParsedType.undefined) {
|
|
2015
|
+
return (0, parseUtil_1.OK)(undefined);
|
|
2016
|
+
}
|
|
2017
|
+
return this._def.innerType._parse({
|
|
2018
|
+
data: ctx.data,
|
|
2019
|
+
path: ctx.path,
|
|
2020
|
+
parent: ctx,
|
|
2021
|
+
});
|
|
2248
2022
|
}
|
|
2249
|
-
|
|
2250
|
-
if (parsedType === parseUtil_1.ZodParsedType.undefined) {
|
|
2251
|
-
return parseUtil_1.OK(undefined);
|
|
2252
|
-
}
|
|
2253
|
-
return this._def.innerType._parse(ctx, data, parsedType);
|
|
2254
|
-
};
|
|
2255
|
-
ZodOptional.prototype.unwrap = function () {
|
|
2023
|
+
unwrap() {
|
|
2256
2024
|
return this._def.innerType;
|
|
2257
|
-
}
|
|
2258
|
-
|
|
2259
|
-
return new ZodOptional(__assign({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodOptional }, processCreateParams(params)));
|
|
2260
|
-
};
|
|
2261
|
-
return ZodOptional;
|
|
2262
|
-
}(ZodType));
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2263
2027
|
exports.ZodOptional = ZodOptional;
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2028
|
+
ZodOptional.create = (type, params) => {
|
|
2029
|
+
return new ZodOptional({
|
|
2030
|
+
innerType: type,
|
|
2031
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
2032
|
+
...processCreateParams(params),
|
|
2033
|
+
});
|
|
2034
|
+
};
|
|
2035
|
+
class ZodNullable extends ZodType {
|
|
2036
|
+
_parse(input) {
|
|
2037
|
+
const { ctx } = this._processInputParams(input);
|
|
2038
|
+
if (ctx.parsedType === parseUtil_1.ZodParsedType.null) {
|
|
2039
|
+
return (0, parseUtil_1.OK)(null);
|
|
2040
|
+
}
|
|
2041
|
+
return this._def.innerType._parse({
|
|
2042
|
+
data: ctx.data,
|
|
2043
|
+
path: ctx.path,
|
|
2044
|
+
parent: ctx,
|
|
2045
|
+
});
|
|
2268
2046
|
}
|
|
2269
|
-
|
|
2270
|
-
if (parsedType === parseUtil_1.ZodParsedType.null) {
|
|
2271
|
-
return parseUtil_1.OK(null);
|
|
2272
|
-
}
|
|
2273
|
-
return this._def.innerType._parse(ctx, data, parsedType);
|
|
2274
|
-
};
|
|
2275
|
-
ZodNullable.prototype.unwrap = function () {
|
|
2047
|
+
unwrap() {
|
|
2276
2048
|
return this._def.innerType;
|
|
2277
|
-
};
|
|
2278
|
-
ZodNullable.create = function (type, params) {
|
|
2279
|
-
return new ZodNullable(__assign({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodNullable }, processCreateParams(params)));
|
|
2280
|
-
};
|
|
2281
|
-
return ZodNullable;
|
|
2282
|
-
}(ZodType));
|
|
2283
|
-
exports.ZodNullable = ZodNullable;
|
|
2284
|
-
var ZodDefault = /** @class */ (function (_super) {
|
|
2285
|
-
__extends(ZodDefault, _super);
|
|
2286
|
-
function ZodDefault() {
|
|
2287
|
-
return _super !== null && _super.apply(this, arguments) || this;
|
|
2288
2049
|
}
|
|
2289
|
-
|
|
2290
|
-
|
|
2050
|
+
}
|
|
2051
|
+
exports.ZodNullable = ZodNullable;
|
|
2052
|
+
ZodNullable.create = (type, params) => {
|
|
2053
|
+
return new ZodNullable({
|
|
2054
|
+
innerType: type,
|
|
2055
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
2056
|
+
...processCreateParams(params),
|
|
2057
|
+
});
|
|
2058
|
+
};
|
|
2059
|
+
class ZodDefault extends ZodType {
|
|
2060
|
+
_parse(input) {
|
|
2061
|
+
const { ctx } = this._processInputParams(input);
|
|
2062
|
+
let data = ctx.data;
|
|
2063
|
+
if (ctx.parsedType === parseUtil_1.ZodParsedType.undefined) {
|
|
2291
2064
|
data = this._def.defaultValue();
|
|
2292
2065
|
}
|
|
2293
|
-
return this._def.innerType._parse(
|
|
2294
|
-
|
|
2295
|
-
|
|
2066
|
+
return this._def.innerType._parse({
|
|
2067
|
+
data,
|
|
2068
|
+
path: ctx.path,
|
|
2069
|
+
parent: ctx,
|
|
2070
|
+
});
|
|
2071
|
+
}
|
|
2072
|
+
removeDefault() {
|
|
2296
2073
|
return this._def.innerType;
|
|
2297
|
-
}
|
|
2298
|
-
|
|
2299
|
-
return new ZodOptional(__assign({ innerType: type, typeName: ZodFirstPartyTypeKind.ZodOptional }, processCreateParams(params)));
|
|
2300
|
-
};
|
|
2301
|
-
return ZodDefault;
|
|
2302
|
-
}(ZodType));
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2303
2076
|
exports.ZodDefault = ZodDefault;
|
|
2304
|
-
|
|
2077
|
+
ZodDefault.create = (type, params) => {
|
|
2078
|
+
return new ZodOptional({
|
|
2079
|
+
innerType: type,
|
|
2080
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
2081
|
+
...processCreateParams(params),
|
|
2082
|
+
});
|
|
2083
|
+
};
|
|
2084
|
+
const custom = (check, params) => {
|
|
2305
2085
|
if (check)
|
|
2306
2086
|
return ZodAny.create().refine(check, params);
|
|
2307
2087
|
return ZodAny.create();
|
|
@@ -2342,78 +2122,75 @@ var ZodFirstPartyTypeKind;
|
|
|
2342
2122
|
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
2343
2123
|
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
2344
2124
|
})(ZodFirstPartyTypeKind = exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
}; }
|
|
2349
|
-
return exports.custom(function (data) { return data instanceof cls; }, params);
|
|
2350
|
-
};
|
|
2125
|
+
const instanceOfType = (cls, params = {
|
|
2126
|
+
message: `Input not instance of ${cls.name}`,
|
|
2127
|
+
}) => (0, exports.custom)((data) => data instanceof cls, params);
|
|
2351
2128
|
exports.instanceof = instanceOfType;
|
|
2352
|
-
|
|
2129
|
+
const stringType = ZodString.create;
|
|
2353
2130
|
exports.string = stringType;
|
|
2354
|
-
|
|
2131
|
+
const numberType = ZodNumber.create;
|
|
2355
2132
|
exports.number = numberType;
|
|
2356
|
-
|
|
2133
|
+
const bigIntType = ZodBigInt.create;
|
|
2357
2134
|
exports.bigint = bigIntType;
|
|
2358
|
-
|
|
2135
|
+
const booleanType = ZodBoolean.create;
|
|
2359
2136
|
exports.boolean = booleanType;
|
|
2360
|
-
|
|
2137
|
+
const dateType = ZodDate.create;
|
|
2361
2138
|
exports.date = dateType;
|
|
2362
|
-
|
|
2139
|
+
const undefinedType = ZodUndefined.create;
|
|
2363
2140
|
exports.undefined = undefinedType;
|
|
2364
|
-
|
|
2141
|
+
const nullType = ZodNull.create;
|
|
2365
2142
|
exports.null = nullType;
|
|
2366
|
-
|
|
2143
|
+
const anyType = ZodAny.create;
|
|
2367
2144
|
exports.any = anyType;
|
|
2368
|
-
|
|
2145
|
+
const unknownType = ZodUnknown.create;
|
|
2369
2146
|
exports.unknown = unknownType;
|
|
2370
|
-
|
|
2147
|
+
const neverType = ZodNever.create;
|
|
2371
2148
|
exports.never = neverType;
|
|
2372
|
-
|
|
2149
|
+
const voidType = ZodVoid.create;
|
|
2373
2150
|
exports.void = voidType;
|
|
2374
|
-
|
|
2151
|
+
const arrayType = ZodArray.create;
|
|
2375
2152
|
exports.array = arrayType;
|
|
2376
|
-
|
|
2153
|
+
const objectType = ZodObject.create;
|
|
2377
2154
|
exports.object = objectType;
|
|
2378
|
-
|
|
2155
|
+
const strictObjectType = ZodObject.strictCreate;
|
|
2379
2156
|
exports.strictObject = strictObjectType;
|
|
2380
|
-
|
|
2157
|
+
const unionType = ZodUnion.create;
|
|
2381
2158
|
exports.union = unionType;
|
|
2382
|
-
|
|
2159
|
+
const intersectionType = ZodIntersection.create;
|
|
2383
2160
|
exports.intersection = intersectionType;
|
|
2384
|
-
|
|
2161
|
+
const tupleType = ZodTuple.create;
|
|
2385
2162
|
exports.tuple = tupleType;
|
|
2386
|
-
|
|
2163
|
+
const recordType = ZodRecord.create;
|
|
2387
2164
|
exports.record = recordType;
|
|
2388
|
-
|
|
2165
|
+
const mapType = ZodMap.create;
|
|
2389
2166
|
exports.map = mapType;
|
|
2390
|
-
|
|
2167
|
+
const setType = ZodSet.create;
|
|
2391
2168
|
exports.set = setType;
|
|
2392
|
-
|
|
2169
|
+
const functionType = ZodFunction.create;
|
|
2393
2170
|
exports.function = functionType;
|
|
2394
|
-
|
|
2171
|
+
const lazyType = ZodLazy.create;
|
|
2395
2172
|
exports.lazy = lazyType;
|
|
2396
|
-
|
|
2173
|
+
const literalType = ZodLiteral.create;
|
|
2397
2174
|
exports.literal = literalType;
|
|
2398
|
-
|
|
2175
|
+
const enumType = ZodEnum.create;
|
|
2399
2176
|
exports.enum = enumType;
|
|
2400
|
-
|
|
2177
|
+
const nativeEnumType = ZodNativeEnum.create;
|
|
2401
2178
|
exports.nativeEnum = nativeEnumType;
|
|
2402
|
-
|
|
2179
|
+
const promiseType = ZodPromise.create;
|
|
2403
2180
|
exports.promise = promiseType;
|
|
2404
|
-
|
|
2181
|
+
const effectsType = ZodEffects.create;
|
|
2405
2182
|
exports.effect = effectsType;
|
|
2406
2183
|
exports.transformer = effectsType;
|
|
2407
|
-
|
|
2184
|
+
const optionalType = ZodOptional.create;
|
|
2408
2185
|
exports.optional = optionalType;
|
|
2409
|
-
|
|
2186
|
+
const nullableType = ZodNullable.create;
|
|
2410
2187
|
exports.nullable = nullableType;
|
|
2411
|
-
|
|
2188
|
+
const preprocessType = ZodEffects.createWithPreprocess;
|
|
2412
2189
|
exports.preprocess = preprocessType;
|
|
2413
|
-
|
|
2190
|
+
const ostring = () => stringType().optional();
|
|
2414
2191
|
exports.ostring = ostring;
|
|
2415
|
-
|
|
2192
|
+
const onumber = () => numberType().optional();
|
|
2416
2193
|
exports.onumber = onumber;
|
|
2417
|
-
|
|
2194
|
+
const oboolean = () => booleanType().optional();
|
|
2418
2195
|
exports.oboolean = oboolean;
|
|
2419
2196
|
//# sourceMappingURL=types.js.map
|