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/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.mergeObjects = 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;
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 = exports.map = void 0;
96
- var errorUtil_1 = require("./helpers/errorUtil");
97
- var parseUtil_1 = require("./helpers/parseUtil");
98
- var util_1 = require("./helpers/util");
99
- var ZodError_1 = require("./ZodError");
100
- // function customErrorMap(arg: string | ZodErrorMap | undefined) {
101
- // if (typeof arg === "string") {
102
- // const map: ZodErrorMap = (iss, ctx) => {
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
- var error = new ZodError_1.ZodError(ctx.issues);
125
- return { success: false, error: error };
126
- }
127
- // if (isOk(result)) {
128
- // return { success: true, data: result.value };
129
- // } else {
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("Can't use \"invalid\" or \"required\" in conjunction with custom error map.");
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
- var customMap = function (iss, ctx) {
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
- var ZodType = /** @class */ (function () {
154
- function ZodType(def) {
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
- ZodType.prototype.addIssue = function (ctx, issueData, params) {
163
- ctx._addIssue(params.data, issueData, {
164
- schemaErrorMap: this._def.errorMap,
165
- });
166
- };
167
- ZodType.prototype._parseSync = function (_ctx, _data, _parsedType) {
168
- var result = this._parse(_ctx, _data, _parsedType);
169
- if (parseUtil_1.isAsync(result)) {
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
- ZodType.prototype._parseAsync = function (_ctx, _data, _parsedType) {
175
- var result = this._parse(_ctx, _data, _parsedType);
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
- ZodType.prototype.parse = function (data, params) {
182
- var result = this.safeParse(data, params);
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
- ZodType.prototype.safeParse = function (data, params) {
188
- var ctx = createRootContext(__assign(__assign({}, params), { async: false }));
189
- var result = this._parseSync(ctx, data, parseUtil_1.getParsedType(data));
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
- ZodType.prototype.parseAsync = function (data, params) {
193
- return __awaiter(this, void 0, void 0, function () {
194
- var result;
195
- return __generator(this, function (_a) {
196
- switch (_a.label) {
197
- case 0: return [4 /*yield*/, this.safeParseAsync(data, params)];
198
- case 1:
199
- result = _a.sent();
200
- if (result.success)
201
- return [2 /*return*/, result.data];
202
- throw result.error;
203
- }
204
- });
205
- });
206
- };
207
- ZodType.prototype.safeParseAsync = function (data, params) {
208
- return __awaiter(this, void 0, void 0, function () {
209
- var ctx, maybeAsyncResult, result;
210
- return __generator(this, function (_a) {
211
- switch (_a.label) {
212
- case 0:
213
- ctx = createRootContext(__assign(__assign({}, params), { async: true }));
214
- maybeAsyncResult = this._parse(ctx, data, parseUtil_1.getParsedType(data));
215
- return [4 /*yield*/, (parseUtil_1.isAsync(maybeAsyncResult)
216
- ? maybeAsyncResult
217
- : Promise.resolve(maybeAsyncResult))];
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: 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(function (val, ctx) {
238
- var result = check(val);
239
- var setError = function () {
240
- return ctx.addIssue(__assign({ code: ZodError_1.ZodIssueCode.custom }, getIssueProperties(val)));
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(function (data) {
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
- ZodType.prototype.refinement = function (check, refinementData) {
263
- return this._refinement(function (val, ctx) {
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
- ZodType.prototype._refinement = function (refinement) {
169
+ }
170
+ _refinement(refinement) {
276
171
  return new ZodEffects({
277
172
  schema: this,
278
173
  typeName: ZodFirstPartyTypeKind.ZodEffects,
279
- effect: { type: "refinement", refinement: refinement },
174
+ effect: { type: "refinement", refinement },
280
175
  });
281
- };
282
- ZodType.prototype.optional = function () {
176
+ }
177
+ optional() {
283
178
  return ZodOptional.create(this);
284
- };
285
- ZodType.prototype.nullable = function () {
179
+ }
180
+ nullable() {
286
181
  return ZodNullable.create(this);
287
- };
288
- ZodType.prototype.nullish = function () {
182
+ }
183
+ nullish() {
289
184
  return this.optional().nullable();
290
- };
291
- ZodType.prototype.array = function () {
185
+ }
186
+ array() {
292
187
  return ZodArray.create(this);
293
- };
294
- ZodType.prototype.promise = function () {
188
+ }
189
+ promise() {
295
190
  return ZodPromise.create(this);
296
- };
297
- ZodType.prototype.or = function (option) {
191
+ }
192
+ or(option) {
298
193
  return ZodUnion.create([this, option]);
299
- };
300
- ZodType.prototype.and = function (incoming) {
194
+ }
195
+ and(incoming) {
301
196
  return ZodIntersection.create(this, incoming);
302
- };
303
- ZodType.prototype.transform = function (transform) {
197
+ }
198
+ transform(transform) {
304
199
  return new ZodEffects({
305
200
  schema: this,
306
201
  typeName: ZodFirstPartyTypeKind.ZodEffects,
307
- effect: { type: "transform", transform: transform },
202
+ effect: { type: "transform", transform },
308
203
  });
309
- };
310
- ZodType.prototype.default = function (def) {
311
- var defaultValueFunc = typeof def === "function" ? def : function () { return def; };
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
- ZodType.prototype.isOptional = function () {
212
+ }
213
+ isOptional() {
325
214
  return this.safeParse(undefined).success;
326
- };
327
- ZodType.prototype.isNullable = function () {
215
+ }
216
+ isNullable() {
328
217
  return this.safeParse(null).success;
329
- };
330
- return ZodType;
331
- }());
218
+ }
219
+ }
332
220
  exports.ZodType = ZodType;
333
221
  exports.Schema = ZodType;
334
222
  exports.ZodSchema = ZodType;
335
- var cuidRegex = /^c[^\s-]{8,}$/i;
336
- var 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;
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
- var emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
342
- var ZodString = /** @class */ (function (_super) {
343
- __extends(ZodString, _super);
344
- function ZodString() {
345
- var _this = _super !== null && _super.apply(this, arguments) || this;
346
- _this._regex = function (regex, validation, message) {
347
- return _this.refinement(function (data) { return regex.test(data); }, __assign({ validation: validation, code: ZodError_1.ZodIssueCode.invalid_string }, errorUtil_1.errorUtil.errToObj(message)));
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
- _this.nonempty = function (message) {
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
- ZodString.prototype._parse = function (ctx, data, parsedType) {
359
- var e_1, _a;
360
- if (parsedType !== parseUtil_1.ZodParsedType.string) {
361
- this.addIssue(ctx, {
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
- }, { data: data });
250
+ received: ctx.parsedType,
251
+ }
252
+ //
253
+ );
366
254
  return parseUtil_1.INVALID;
367
255
  }
368
- var invalid = false;
369
- try {
370
- for (var _b = __values(this._def.checks), _c = _b.next(); !_c.done; _c = _b.next()) {
371
- var check = _c.value;
372
- if (check.kind === "min") {
373
- if (data.length < check.value) {
374
- invalid = true;
375
- this.addIssue(ctx, {
376
- code: ZodError_1.ZodIssueCode.too_small,
377
- minimum: check.value,
378
- type: "string",
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
- else if (check.kind === "max") {
385
- if (data.length > check.value) {
386
- invalid = true;
387
- this.addIssue(ctx, {
388
- code: ZodError_1.ZodIssueCode.too_big,
389
- maximum: check.value,
390
- type: "string",
391
- inclusive: true,
392
- message: check.message,
393
- }, { data: data });
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
- else if (check.kind === "email") {
397
- if (!emailRegex.test(data)) {
398
- invalid = true;
399
- this.addIssue(ctx, {
400
- validation: "email",
401
- code: ZodError_1.ZodIssueCode.invalid_string,
402
- message: check.message,
403
- }, { data: data });
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
- else if (check.kind === "uuid") {
407
- if (!uuidRegex.test(data)) {
408
- invalid = true;
409
- this.addIssue(ctx, {
410
- validation: "uuid",
411
- code: ZodError_1.ZodIssueCode.invalid_string,
412
- message: check.message,
413
- }, { data: data });
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
- else if (check.kind === "cuid") {
417
- if (!cuidRegex.test(data)) {
418
- invalid = true;
419
- this.addIssue(ctx, {
420
- validation: "cuid",
421
- code: ZodError_1.ZodIssueCode.invalid_string,
422
- message: check.message,
423
- }, { data: data });
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
- else if (check.kind === "url") {
427
- try {
428
- new URL(data);
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
- else if (check.kind === "regex") {
440
- // clear regex state
441
- check.regex.lastIndex = 0;
442
- var testResult = check.regex.test(data);
443
- if (!testResult) {
444
- invalid = true;
445
- this.addIssue(ctx, {
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
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
455
- finally {
456
- try {
457
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
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 invalid ? parseUtil_1.INVALID : parseUtil_1.OK(data);
462
- };
463
- ZodString.prototype._addCheck = function (check) {
464
- return new ZodString(__assign(__assign({}, this._def), { checks: __spread(this._def.checks, [check]) }));
465
- };
466
- ZodString.prototype.email = function (message) {
467
- return this._addCheck(__assign({ kind: "email" }, errorUtil_1.errorUtil.errToObj(message)));
468
- };
469
- ZodString.prototype.url = function (message) {
470
- return this._addCheck(__assign({ kind: "url" }, errorUtil_1.errorUtil.errToObj(message)));
471
- };
472
- ZodString.prototype.uuid = function (message) {
473
- return this._addCheck(__assign({ kind: "uuid" }, errorUtil_1.errorUtil.errToObj(message)));
474
- };
475
- ZodString.prototype.cuid = function (message) {
476
- return this._addCheck(__assign({ kind: "cuid" }, errorUtil_1.errorUtil.errToObj(message)));
477
- };
478
- ZodString.prototype.regex = function (regex, message) {
479
- return this._addCheck(__assign({ kind: "regex", regex: regex }, errorUtil_1.errorUtil.errToObj(message)));
480
- };
481
- ZodString.prototype.min = function (minLength, message) {
482
- return this._addCheck(__assign({ kind: "min", value: minLength }, errorUtil_1.errorUtil.errToObj(message)));
483
- };
484
- ZodString.prototype.max = function (maxLength, message) {
485
- return this._addCheck(__assign({ kind: "max", value: maxLength }, errorUtil_1.errorUtil.errToObj(message)));
486
- };
487
- ZodString.prototype.length = function (len, message) {
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
- Object.defineProperty(ZodString.prototype, "isEmail", {
491
- get: function () {
492
- return !!this._def.checks.find(function (ch) { return ch.kind === "email"; });
493
- },
494
- enumerable: false,
495
- configurable: true
496
- });
497
- Object.defineProperty(ZodString.prototype, "isURL", {
498
- get: function () {
499
- return !!this._def.checks.find(function (ch) { return ch.kind === "url"; });
500
- },
501
- enumerable: false,
502
- configurable: true
503
- });
504
- Object.defineProperty(ZodString.prototype, "isUUID", {
505
- get: function () {
506
- return !!this._def.checks.find(function (ch) { return ch.kind === "uuid"; });
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
- return min;
529
- },
530
- enumerable: false,
531
- configurable: true
532
- });
533
- Object.defineProperty(ZodString.prototype, "maxLength", {
534
- get: function () {
535
- var max = null;
536
- this._def.checks.map(function (ch) {
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
- return max;
544
- },
545
- enumerable: false,
546
- configurable: true
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
- var ZodNumber = /** @class */ (function (_super) {
555
- __extends(ZodNumber, _super);
556
- function ZodNumber() {
557
- var _this = _super !== null && _super.apply(this, arguments) || this;
558
- _this.min = _this.gte;
559
- _this.max = _this.lte;
560
- _this.step = _this.multipleOf;
561
- return _this;
562
- }
563
- ZodNumber.prototype._parse = function (ctx, data, parsedType) {
564
- var e_2, _a;
565
- if (parsedType !== parseUtil_1.ZodParsedType.number) {
566
- this.addIssue(ctx, {
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
- }, { data: data });
437
+ received: ctx.parsedType,
438
+ });
571
439
  return parseUtil_1.INVALID;
572
440
  }
573
- var invalid = false;
574
- try {
575
- for (var _b = __values(this._def.checks), _c = _b.next(); !_c.done; _c = _b.next()) {
576
- var check = _c.value;
577
- if (check.kind === "int") {
578
- if (!util_1.util.isInteger(data)) {
579
- invalid = true;
580
- this.addIssue(ctx, {
581
- code: ZodError_1.ZodIssueCode.invalid_type,
582
- expected: "integer",
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
- else if (check.kind === "max") {
605
- var tooBig = check.inclusive
606
- ? data > check.value
607
- : data >= check.value;
608
- if (tooBig) {
609
- invalid = true;
610
- this.addIssue(ctx, {
611
- code: ZodError_1.ZodIssueCode.too_big,
612
- maximum: check.value,
613
- type: "number",
614
- inclusive: check.inclusive,
615
- message: check.message,
616
- }, { data: data });
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
- else if (check.kind === "multipleOf") {
620
- if (data % check.value !== 0) {
621
- invalid = true;
622
- this.addIssue(ctx, {
623
- code: ZodError_1.ZodIssueCode.not_multiple_of,
624
- multipleOf: check.value,
625
- message: check.message,
626
- }, { data: data });
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
- else {
630
- util_1.util.assertNever(check);
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
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
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 invalid ? parseUtil_1.INVALID : parseUtil_1.OK(data);
642
- };
643
- ZodNumber.prototype.gte = function (value, message) {
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
- ZodNumber.prototype.gt = function (value, message) {
501
+ }
502
+ gt(value, message) {
647
503
  return this.setLimit("min", value, false, errorUtil_1.errorUtil.toString(message));
648
- };
649
- ZodNumber.prototype.lte = function (value, message) {
504
+ }
505
+ lte(value, message) {
650
506
  return this.setLimit("max", value, true, errorUtil_1.errorUtil.toString(message));
651
- };
652
- ZodNumber.prototype.lt = function (value, message) {
507
+ }
508
+ lt(value, message) {
653
509
  return this.setLimit("max", value, false, errorUtil_1.errorUtil.toString(message));
654
- };
655
- ZodNumber.prototype.setLimit = function (kind, value, inclusive, message) {
656
- return new ZodNumber(__assign(__assign({}, this._def), { checks: __spread(this._def.checks, [
510
+ }
511
+ setLimit(kind, value, inclusive, message) {
512
+ return new ZodNumber({
513
+ ...this._def,
514
+ checks: [
515
+ ...this._def.checks,
657
516
  {
658
- kind: kind,
659
- value: value,
660
- inclusive: inclusive,
517
+ kind,
518
+ value,
519
+ inclusive,
661
520
  message: errorUtil_1.errorUtil.toString(message),
662
521
  },
663
- ]) }));
664
- };
665
- ZodNumber.prototype._addCheck = function (check) {
666
- return new ZodNumber(__assign(__assign({}, this._def), { checks: __spread(this._def.checks, [check]) }));
667
- };
668
- ZodNumber.prototype.int = function (message) {
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
- ZodNumber.prototype.positive = function (message) {
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
- ZodNumber.prototype.negative = function (message) {
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
- ZodNumber.prototype.nonpositive = function (message) {
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
- ZodNumber.prototype.nonnegative = function (message) {
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
- ZodNumber.prototype.multipleOf = function (value, message) {
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
- Object.defineProperty(ZodNumber.prototype, "minValue", {
714
- get: function () {
715
- var e_3, _a;
716
- var min = null;
717
- try {
718
- for (var _b = __values(this._def.checks), _c = _b.next(); !_c.done; _c = _b.next()) {
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
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
752
- finally {
753
- try {
754
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
755
- }
756
- finally { if (e_4) throw e_4.error; }
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
- return max;
759
- },
760
- enumerable: false,
761
- configurable: true
762
- });
763
- Object.defineProperty(ZodNumber.prototype, "isInt", {
764
- get: function () {
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
- var ZodBigInt = /** @class */ (function (_super) {
777
- __extends(ZodBigInt, _super);
778
- function ZodBigInt() {
779
- return _super !== null && _super.apply(this, arguments) || this;
780
- }
781
- ZodBigInt.prototype._parse = function (ctx, data, parsedType) {
782
- if (parsedType !== parseUtil_1.ZodParsedType.bigint) {
783
- this.addIssue(ctx, {
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
- }, { data: data });
616
+ received: ctx.parsedType,
617
+ });
788
618
  return parseUtil_1.INVALID;
789
619
  }
790
- return parseUtil_1.OK(data);
791
- };
792
- ZodBigInt.create = function (params) {
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
- var ZodBoolean = /** @class */ (function (_super) {
799
- __extends(ZodBoolean, _super);
800
- function ZodBoolean() {
801
- return _super !== null && _super.apply(this, arguments) || this;
802
- }
803
- ZodBoolean.prototype._parse = function (ctx, data, parsedType) {
804
- if (parsedType !== parseUtil_1.ZodParsedType.boolean) {
805
- this.addIssue(ctx, {
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
- }, { data: data });
637
+ received: ctx.parsedType,
638
+ });
810
639
  return parseUtil_1.INVALID;
811
640
  }
812
- return parseUtil_1.OK(data);
813
- };
814
- ZodBoolean.create = function (params) {
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
- var ZodDate = /** @class */ (function (_super) {
821
- __extends(ZodDate, _super);
822
- function ZodDate() {
823
- return _super !== null && _super.apply(this, arguments) || this;
824
- }
825
- ZodDate.prototype._parse = function (ctx, data, parsedType) {
826
- if (parsedType !== parseUtil_1.ZodParsedType.date) {
827
- this.addIssue(ctx, {
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
- }, { data: data });
658
+ received: ctx.parsedType,
659
+ });
832
660
  return parseUtil_1.INVALID;
833
661
  }
834
- if (isNaN(data.getTime())) {
835
- this.addIssue(ctx, {
662
+ if (isNaN(ctx.data.getTime())) {
663
+ (0, parseUtil_1.addIssueToContext)(ctx, {
836
664
  code: ZodError_1.ZodIssueCode.invalid_date,
837
- }, { data: data });
665
+ });
838
666
  return parseUtil_1.INVALID;
839
667
  }
840
- return parseUtil_1.OK(new Date(data.getTime()));
841
- };
842
- ZodDate.create = function (params) {
843
- return new ZodDate(__assign({ typeName: ZodFirstPartyTypeKind.ZodDate }, processCreateParams(params)));
844
- };
845
- return ZodDate;
846
- }(ZodType));
668
+ return {
669
+ status: status.value,
670
+ value: new Date(ctx.data.getTime()),
671
+ };
672
+ }
673
+ }
847
674
  exports.ZodDate = ZodDate;
848
- var ZodUndefined = /** @class */ (function (_super) {
849
- __extends(ZodUndefined, _super);
850
- function ZodUndefined() {
851
- return _super !== null && _super.apply(this, arguments) || this;
852
- }
853
- ZodUndefined.prototype._parse = function (ctx, data, parsedType) {
854
- if (parsedType !== parseUtil_1.ZodParsedType.undefined) {
855
- this.addIssue(ctx, {
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
- }, { data: data });
688
+ received: ctx.parsedType,
689
+ });
860
690
  return parseUtil_1.INVALID;
861
691
  }
862
- return parseUtil_1.OK(data);
863
- };
864
- ZodUndefined.create = function (params) {
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
- var ZodNull = /** @class */ (function (_super) {
871
- __extends(ZodNull, _super);
872
- function ZodNull() {
873
- return _super !== null && _super.apply(this, arguments) || this;
874
- }
875
- ZodNull.prototype._parse = function (ctx, data, parsedType) {
876
- if (parsedType !== parseUtil_1.ZodParsedType.null) {
877
- this.addIssue(ctx, {
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
- }, { data: data });
709
+ received: ctx.parsedType,
710
+ });
882
711
  return parseUtil_1.INVALID;
883
712
  }
884
- return parseUtil_1.OK(data);
885
- };
886
- ZodNull.create = function (params) {
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
- var ZodAny = /** @class */ (function (_super) {
893
- __extends(ZodAny, _super);
894
- function ZodAny() {
895
- var _this = _super !== null && _super.apply(this, arguments) || this;
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
- _this._any = true;
898
- return _this;
727
+ this._any = true;
899
728
  }
900
- ZodAny.prototype._parse = function (_ctx, data, _parsedType) {
901
- return parseUtil_1.OK(data);
902
- };
903
- ZodAny.create = function (params) {
904
- return new ZodAny(__assign({ typeName: ZodFirstPartyTypeKind.ZodAny }, processCreateParams(params)));
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
- var ZodUnknown = /** @class */ (function (_super) {
910
- __extends(ZodUnknown, _super);
911
- function ZodUnknown() {
912
- var _this = _super !== null && _super.apply(this, arguments) || this;
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
- _this._unknown = true;
915
- return _this;
745
+ this._unknown = true;
916
746
  }
917
- ZodUnknown.prototype._parse = function (_ctx, data, _parsedType) {
918
- return parseUtil_1.OK(data);
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
- ZodNever.prototype._parse = function (ctx, data, parsedType) {
932
- this.addIssue(ctx, {
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
- }, { data: data });
765
+ received: ctx.parsedType,
766
+ });
937
767
  return parseUtil_1.INVALID;
938
- };
939
- ZodNever.create = function (params) {
940
- return new ZodNever(__assign({ typeName: ZodFirstPartyTypeKind.ZodNever }, processCreateParams(params)));
941
- };
942
- return ZodNever;
943
- }(ZodType));
768
+ }
769
+ }
944
770
  exports.ZodNever = ZodNever;
945
- var ZodVoid = /** @class */ (function (_super) {
946
- __extends(ZodVoid, _super);
947
- function ZodVoid() {
948
- return _super !== null && _super.apply(this, arguments) || this;
949
- }
950
- ZodVoid.prototype._parse = function (ctx, data, parsedType) {
951
- if (parsedType !== parseUtil_1.ZodParsedType.undefined) {
952
- this.addIssue(ctx, {
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
- }, { data: data });
784
+ received: ctx.parsedType,
785
+ });
957
786
  return parseUtil_1.INVALID;
958
787
  }
959
- return parseUtil_1.OK(data);
960
- };
961
- ZodVoid.create = function (params) {
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
- var ZodArray = /** @class */ (function (_super) {
968
- __extends(ZodArray, _super);
969
- function ZodArray() {
970
- return _super !== null && _super.apply(this, arguments) || this;
971
- }
972
- ZodArray.prototype._parse = function (ctx, _data, parsedType) {
973
- // return parseArray(ctx, data, parsedType, this._def) as any;
974
- var def = this._def;
975
- if (parsedType !== parseUtil_1.ZodParsedType.array) {
976
- this.addIssue(ctx, {
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
- }, { data: _data });
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
- invalid = true;
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
- }, { data: data });
818
+ });
819
+ status.dirty();
995
820
  }
996
821
  }
997
822
  if (def.maxLength !== null) {
998
- if (data.length > def.maxLength.value) {
999
- invalid = true;
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
- }, { data: data });
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(tasks).then(function () { return (invalid ? parseUtil_1.INVALID : parseUtil_1.OK(result)); });
1028
- }
1029
- else {
1030
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(result);
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
- Object.defineProperty(ZodArray.prototype, "element", {
1034
- get: function () {
1035
- return this._def.type;
1036
- },
1037
- enumerable: false,
1038
- configurable: true
1039
- });
1040
- ZodArray.prototype.min = function (minLength, message) {
1041
- return new ZodArray(__assign(__assign({}, this._def), { minLength: { value: minLength, message: errorUtil_1.errorUtil.toString(message) } }));
1042
- };
1043
- ZodArray.prototype.max = function (maxLength, message) {
1044
- return new ZodArray(__assign(__assign({}, this._def), { maxLength: { value: maxLength, message: errorUtil_1.errorUtil.toString(message) } }));
1045
- };
1046
- ZodArray.prototype.length = function (len, message) {
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
- ZodArray.prototype.nonempty = function (message) {
1050
- return this.min(1, message); // new ZodArray({ ...this._def, cardinality:"atleastone" });
1051
- };
1052
- ZodArray.create = function (schema, params) {
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 = function (first, second) {
1068
- return __assign(__assign({}, first), second);
1069
- };
1070
- objectUtil.intersectShapes = function (first, second) {
1071
- var e_5, _a;
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
- var mergeObjects = function (first) { return function (second) {
1093
- var mergedShape = objectUtil.mergeShapes(first._def.shape(), second._def.shape());
1094
- var merged = new ZodObject({
1095
- // effects: [...(first._def.effects || []), ...(second._def.effects || [])],
1096
- unknownKeys: first._def.unknownKeys,
1097
- catchall: first._def.catchall,
1098
- shape: function () { return mergedShape; },
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
- return merged;
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
- var newShape_1 = {};
1110
- for (var key in schema.shape) {
1111
- var fieldSchema = schema.shape[key];
1112
- newShape_1[key] = ZodOptional.create(deepPartialify(fieldSchema));
1113
- }
1114
- return new ZodObject(__assign(__assign({}, schema._def), { shape: function () { return newShape_1; } }));
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(function (item) { return deepPartialify(item); }));
933
+ return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
1127
934
  }
1128
935
  else {
1129
936
  return schema;
1130
937
  }
1131
938
  }
1132
- var ZodObject = /** @class */ (function (_super) {
1133
- __extends(ZodObject, _super);
1134
- function ZodObject() {
1135
- var _this = _super !== null && _super.apply(this, arguments) || this;
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
- _this.nonstrict = _this.passthrough;
1142
- _this.augment = AugmentFactory(_this._def);
1143
- _this.extend = AugmentFactory(_this._def);
1144
- return _this;
947
+ this.nonstrict = this.passthrough;
948
+ this.augment = AugmentFactory(this._def);
949
+ this.extend = AugmentFactory(this._def);
1145
950
  }
1146
- ZodObject.prototype._getCached = function () {
951
+ _getCached() {
1147
952
  if (this._cached !== null)
1148
953
  return this._cached;
1149
- var shape = this._def.shape();
1150
- var keys = util_1.util.objectKeys(shape);
1151
- return (this._cached = { shape: shape, keys: keys });
1152
- };
1153
- ZodObject.prototype._parse = function (ctx, data, parsedType) {
1154
- var e_6, _a, e_7, _b, e_8, _c;
1155
- if (parsedType !== parseUtil_1.ZodParsedType.object) {
1156
- this.addIssue(ctx, {
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
- }, { data: data });
964
+ received: ctx.parsedType,
965
+ });
1161
966
  return parseUtil_1.INVALID;
1162
967
  }
1163
- var _d = this._getCached(), shape = _d.shape, shapeKeys = _d.keys;
1164
- var invalid = false;
1165
- var tasks = [];
1166
- var resultObject = {};
1167
- var handleParsed = function (key, parsedValue) {
1168
- if (parseUtil_1.isOk(parsedValue)) {
1169
- var value = parsedValue.value;
1170
- if (typeof value !== "undefined" || key in data) {
1171
- // key was valid but result was undefined: add it to the result object
1172
- // only if key was in the input data object - if it wasn't, then it's
1173
- // an optional key that should not be added
1174
- resultObject[key] = value;
1175
- }
1176
- }
1177
- else if (parseUtil_1.isInvalid(parsedValue)) {
1178
- invalid = true;
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
- var unknownKeys = this._def.unknownKeys;
986
+ const unknownKeys = this._def.unknownKeys;
1201
987
  if (unknownKeys === "passthrough") {
1202
- var dataKeys = util_1.util.objectKeys(data);
1203
- var extraKeys = dataKeys.filter(function (k) { return !(k in shape); });
1204
- try {
1205
- for (var extraKeys_1 = __values(extraKeys), extraKeys_1_1 = extraKeys_1.next(); !extraKeys_1_1.done; extraKeys_1_1 = extraKeys_1.next()) {
1206
- var key = extraKeys_1_1.value;
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
- invalid = true;
1223
- this.addIssue(ctx, {
997
+ (0, parseUtil_1.addIssueToContext)(ctx, {
1224
998
  code: ZodError_1.ZodIssueCode.unrecognized_keys,
1225
999
  keys: extraKeys,
1226
- }, { data: data });
1000
+ });
1001
+ status.dirty();
1227
1002
  }
1228
1003
  }
1229
1004
  else if (unknownKeys === "strip") {
1230
1005
  }
1231
1006
  else {
1232
- throw new Error("Internal ZodObject error: invalid unknownKeys value.");
1007
+ throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
1233
1008
  }
1234
1009
  }
1235
1010
  else {
1236
1011
  // run catchall validation
1237
- var catchall = this._def.catchall;
1238
- var dataKeys = util_1.util.objectKeys(data);
1239
- var extraKeys = dataKeys.filter(function (k) { return !(k in shape); });
1240
- try {
1241
- for (var extraKeys_2 = __values(extraKeys), extraKeys_2_1 = extraKeys_2.next(); !extraKeys_2_1.done; extraKeys_2_1 = extraKeys_2.next()) {
1242
- var key = extraKeys_2_1.value;
1243
- var value = data[key];
1244
- handleParsed(key, catchall._parse(ctx.stepInto(key), value, parseUtil_1.getParsedType(value)));
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.all(tasks).then(function () {
1257
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(resultObject);
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 invalid ? parseUtil_1.INVALID : parseUtil_1.OK(resultObject);
1042
+ return parseUtil_1.ParseStatus.mergeObjectSync(status, pairs);
1262
1043
  }
1263
- };
1264
- Object.defineProperty(ZodObject.prototype, "shape", {
1265
- get: function () {
1266
- return this._def.shape();
1267
- },
1268
- enumerable: false,
1269
- configurable: true
1270
- });
1271
- ZodObject.prototype.strict = function () {
1272
- return new ZodObject(__assign(__assign({}, this._def), { unknownKeys: "strict" }));
1273
- };
1274
- ZodObject.prototype.strip = function () {
1275
- return new ZodObject(__assign(__assign({}, this._def), { unknownKeys: "strip" }));
1276
- };
1277
- ZodObject.prototype.passthrough = function () {
1278
- return new ZodObject(__assign(__assign({}, this._def), { unknownKeys: "passthrough" }));
1279
- };
1280
- ZodObject.prototype.setKey = function (key, schema) {
1281
- var _a;
1282
- return this.augment((_a = {}, _a[key] = schema, _a));
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
- ZodObject.prototype.merge = function (merging) {
1290
- var mergedShape = objectUtil.mergeShapes(this._def.shape(), merging._def.shape());
1291
- var merged = new ZodObject({
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: function () { return mergedShape; },
1094
+ shape: () => mergedShape,
1296
1095
  typeName: ZodFirstPartyTypeKind.ZodObject,
1297
1096
  });
1298
1097
  return merged;
1299
- };
1300
- ZodObject.prototype.catchall = function (index) {
1301
- return new ZodObject(__assign(__assign({}, this._def), { catchall: index }));
1302
- };
1303
- ZodObject.prototype.pick = function (mask) {
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
- return new ZodObject(__assign(__assign({}, this._def), { shape: function () { return shape; } }));
1310
- };
1311
- ZodObject.prototype.omit = function (mask) {
1312
- var _this = this;
1313
- var shape = {};
1314
- util_1.util.objectKeys(this.shape).map(function (key) {
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] = _this.shape[key];
1119
+ shape[key] = this.shape[key];
1317
1120
  }
1318
1121
  });
1319
- return new ZodObject(__assign(__assign({}, this._def), { shape: function () { return shape; } }));
1320
- };
1321
- ZodObject.prototype.deepPartial = function () {
1122
+ return new ZodObject({
1123
+ ...this._def,
1124
+ shape: () => shape,
1125
+ });
1126
+ }
1127
+ deepPartial() {
1322
1128
  return deepPartialify(this);
1323
- };
1324
- ZodObject.prototype.partial = function (mask) {
1325
- var _this = this;
1326
- var newShape = {};
1129
+ }
1130
+ partial(mask) {
1131
+ const newShape = {};
1327
1132
  if (mask) {
1328
- // const newShape: any = {};
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] = _this.shape[key];
1135
+ newShape[key] = this.shape[key];
1332
1136
  }
1333
1137
  else {
1334
- newShape[key] = _this.shape[key].optional();
1138
+ newShape[key] = this.shape[key].optional();
1335
1139
  }
1336
1140
  });
1337
- return new ZodObject(__assign(__assign({}, this._def), { shape: function () { return newShape; } }));
1141
+ return new ZodObject({
1142
+ ...this._def,
1143
+ shape: () => newShape,
1144
+ });
1338
1145
  }
1339
1146
  else {
1340
- for (var key in this.shape) {
1341
- var fieldSchema = this.shape[key];
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(__assign(__assign({}, this._def), { shape: function () { return newShape; } }));
1346
- };
1347
- ZodObject.prototype.required = function () {
1348
- var newShape = {};
1349
- for (var key in this.shape) {
1350
- var fieldSchema = this.shape[key];
1351
- var newField = fieldSchema;
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(__assign(__assign({}, this._def), { shape: function () { return newShape; } }));
1358
- };
1359
- ZodObject.create = function (shape, params) {
1360
- return new ZodObject(__assign({ shape: function () { return shape; }, unknownKeys: "strip", catchall: ZodNever.create(), typeName: ZodFirstPartyTypeKind.ZodObject }, processCreateParams(params)));
1361
- };
1362
- ZodObject.strictCreate = function (shape, params) {
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
- // export type toOpts<T> = T extends ZodUnionOptions ? T : never;
1372
- // export type ZodUnionType<
1373
- // A extends ZodTypeAny,
1374
- // B extends ZodTypeAny
1375
- // > = A extends ZodUnion<infer AOpts>
1376
- // ? B extends ZodUnion<infer BOpts>
1377
- // ? ZodUnion<toOpts<[...AOpts, ...BOpts]>>
1378
- // : ZodUnion<toOpts<[...AOpts, B]>>
1379
- // : B extends ZodUnion<infer BOpts>
1380
- // ? ZodUnion<toOpts<[A, ...BOpts]>>
1381
- // : ZodUnion<toOpts<[A, B]>>;
1382
- var ZodUnion = /** @class */ (function (_super) {
1383
- __extends(ZodUnion, _super);
1384
- function ZodUnion() {
1385
- return _super !== null && _super.apply(this, arguments) || this;
1386
- }
1387
- ZodUnion.prototype._parse = function (ctx, data, parsedType) {
1388
- var e_9, _a;
1389
- var _this = this;
1390
- var options = this._def.options;
1391
- var noMatch = function (allIssues) {
1392
- var unionErrors = allIssues.map(function (issues) { return new ZodError_1.ZodError(issues); });
1393
- var nonTypeErrors = unionErrors.filter(function (err) {
1394
- return err.issues[0].code !== "invalid_type";
1395
- });
1396
- if (nonTypeErrors.length === 1) {
1397
- // TODO encapsulate
1398
- nonTypeErrors[0].issues.forEach(function (issue) { return ctx.issues.push(issue); });
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
- else {
1401
- _this.addIssue(ctx, {
1402
- code: ZodError_1.ZodIssueCode.invalid_union,
1403
- unionErrors: unionErrors,
1404
- }, { data: data });
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
- var contexts_1 = options.map(function () { return new parseUtil_1.ParseContext(__assign(__assign({}, ctx.def), { issues: [] })); });
1410
- return Promise.all(options.map(function (option, index) {
1411
- return option._parse(contexts_1[index], data, parsedType);
1412
- })).then(function (parsedOptions) {
1413
- var e_10, _a;
1414
- try {
1415
- for (var parsedOptions_1 = __values(parsedOptions), parsedOptions_1_1 = parsedOptions_1.next(); !parsedOptions_1_1.done; parsedOptions_1_1 = parsedOptions_1.next()) {
1416
- var parsedOption = parsedOptions_1_1.value;
1417
- if (parseUtil_1.isOk(parsedOption)) {
1418
- return parsedOption;
1419
- }
1420
- }
1421
- }
1422
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
1423
- finally {
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
- var allIssues = [];
1434
- try {
1435
- for (var options_1 = __values(options), options_1_1 = options_1.next(); !options_1_1.done; options_1_1 = options_1.next()) {
1436
- var option = options_1_1.value;
1437
- var optionCtx = new parseUtil_1.ParseContext(__assign(__assign({}, ctx.def), { issues: [] }));
1438
- var parsedOption = option._parseSync(optionCtx, data, parsedType);
1439
- if (parseUtil_1.isInvalid(parsedOption)) {
1440
- allIssues.push(optionCtx.issues);
1441
- }
1442
- else {
1443
- return parsedOption;
1444
- }
1445
- }
1446
- }
1447
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
1448
- finally {
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
- Object.defineProperty(ZodUnion.prototype, "options", {
1458
- get: function () {
1459
- return this._def.options;
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
- var e_11, _a;
1472
- var aType = parseUtil_1.getParsedType(a);
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
- var bKeys_1 = util_1.util.objectKeys(b);
1479
- var sharedKeys = util_1.util
1282
+ const bKeys = util_1.util.objectKeys(b);
1283
+ const sharedKeys = util_1.util
1480
1284
  .objectKeys(a)
1481
- .filter(function (key) { return bKeys_1.indexOf(key) !== -1; });
1482
- var newObj = __assign(__assign({}, a), b);
1483
- try {
1484
- for (var sharedKeys_2 = __values(sharedKeys), sharedKeys_2_1 = sharedKeys_2.next(); !sharedKeys_2_1.done; sharedKeys_2_1 = sharedKeys_2.next()) {
1485
- var key = sharedKeys_2_1.value;
1486
- var sharedValue = mergeValues(a[key], b[key]);
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
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
1494
- finally {
1495
- try {
1496
- if (sharedKeys_2_1 && !sharedKeys_2_1.done && (_a = sharedKeys_2.return)) _a.call(sharedKeys_2);
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
- finally { if (e_11) throw e_11.error; }
1308
+ newArray.push(sharedValue.data);
1499
1309
  }
1500
- return { valid: true, data: newObj };
1310
+ return { valid: true, data: newArray };
1501
1311
  }
1502
1312
  else {
1503
1313
  return { valid: false };
1504
1314
  }
1505
1315
  }
1506
- var ZodIntersection = /** @class */ (function (_super) {
1507
- __extends(ZodIntersection, _super);
1508
- function ZodIntersection() {
1509
- return _super !== null && _super.apply(this, arguments) || this;
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
- var merged = mergeValues(parsedLeft.value, parsedRight.value);
1323
+ const merged = mergeValues(parsedLeft.value, parsedRight.value);
1518
1324
  if (!merged.valid) {
1519
- _this.addIssue(ctx, {
1325
+ (0, parseUtil_1.addIssueToContext)(ctx, {
1520
1326
  code: ZodError_1.ZodIssueCode.invalid_intersection_types,
1521
- }, { data: data });
1327
+ });
1522
1328
  return parseUtil_1.INVALID;
1523
1329
  }
1524
- return parseUtil_1.OK(merged.data);
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._parse(ctx, data, parsedType),
1529
- this._def.right._parse(ctx, data, parsedType),
1530
- ]).then(function (_a) {
1531
- var _b = __read(_a, 2), left = _b[0], right = _b[1];
1532
- return handleParsed(left, right);
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(ctx, data, parsedType), this._def.right._parseSync(ctx, data, parsedType));
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
- ZodIntersection.create = function (left, right, params) {
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
- var ZodTuple = /** @class */ (function (_super) {
1546
- __extends(ZodTuple, _super);
1547
- function ZodTuple() {
1548
- return _super !== null && _super.apply(this, arguments) || this;
1549
- }
1550
- ZodTuple.prototype._parse = function (ctx, data, parsedType) {
1551
- if (parsedType !== parseUtil_1.ZodParsedType.array) {
1552
- this.addIssue(ctx, {
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
- }, { data: data });
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
- this.addIssue(ctx, {
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
- }, { data: data });
1388
+ });
1576
1389
  return parseUtil_1.INVALID;
1577
1390
  }
1578
- var tasks = [];
1579
- var items = this._def.items;
1580
- var parseResult = new Array(data.length);
1581
- var invalid = false;
1582
- var handleParsed = function (index, parsedItem) {
1583
- if (parseUtil_1.isOk(parsedItem)) {
1584
- parseResult[index] = parsedItem.value;
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(tasks).then(function () {
1605
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parseResult);
1414
+ return Promise.all(items).then((results) => {
1415
+ return parseUtil_1.ParseStatus.mergeArray(status, results);
1606
1416
  });
1607
1417
  }
1608
1418
  else {
1609
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parseResult);
1419
+ return parseUtil_1.ParseStatus.mergeArray(status, items);
1610
1420
  }
1611
- };
1612
- Object.defineProperty(ZodTuple.prototype, "items", {
1613
- get: function () {
1614
- return this._def.items;
1615
- },
1616
- enumerable: false,
1617
- configurable: true
1618
- });
1619
- ZodTuple.prototype.rest = function (rest) {
1620
- return new ZodTuple(__assign(__assign({}, this._def), { rest: rest }));
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
- var ZodRecord = /** @class */ (function (_super) {
1629
- __extends(ZodRecord, _super);
1630
- function ZodRecord() {
1631
- return _super !== null && _super.apply(this, arguments) || this;
1632
- }
1633
- Object.defineProperty(ZodRecord.prototype, "keySchema", {
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
- Object.defineProperty(ZodRecord.prototype, "valueSchema", {
1641
- get: function () {
1642
- return this._def.valueType;
1643
- },
1644
- enumerable: false,
1645
- configurable: true
1646
- });
1647
- ZodRecord.prototype._parse = function (ctx, data, parsedType) {
1648
- if (parsedType !== parseUtil_1.ZodParsedType.object) {
1649
- this.addIssue(ctx, {
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
- }, { data: data });
1454
+ received: ctx.parsedType,
1455
+ });
1654
1456
  return parseUtil_1.INVALID;
1655
1457
  }
1656
- var tasks = [];
1657
- var keyType = this._def.keyType;
1658
- var valueType = this._def.valueType;
1659
- var parseResult = {};
1660
- var invalid = false;
1661
- var handleParsed = function (parsedKey, parsedValue) {
1662
- if (parseUtil_1.isOk(parsedKey) && parseUtil_1.isOk(parsedValue)) {
1663
- parseResult[parsedKey.value] = parsedValue.value;
1664
- }
1665
- else if (parseUtil_1.isAsync(parsedKey) || parseUtil_1.isAsync(parsedValue)) {
1666
- tasks.push(Promise.all([parsedKey, parsedValue]).then(function (_a) {
1667
- var _b = __read(_a, 2), k = _b[0], v = _b[1];
1668
- return handleParsed(k, v);
1669
- }));
1670
- }
1671
- else {
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 Promise.all(tasks).then(function () {
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 invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parseResult);
1479
+ return parseUtil_1.ParseStatus.mergeObjectSync(status, pairs);
1685
1480
  }
1686
- };
1687
- Object.defineProperty(ZodRecord.prototype, "element", {
1688
- get: function () {
1689
- return this._def.valueType;
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(__assign({ keyType: first, valueType: second, typeName: ZodFirstPartyTypeKind.ZodRecord }, processCreateParams(third)));
1487
+ return new ZodRecord({
1488
+ keyType: first,
1489
+ valueType: second,
1490
+ typeName: ZodFirstPartyTypeKind.ZodRecord,
1491
+ ...processCreateParams(third),
1492
+ });
1697
1493
  }
1698
- return new ZodRecord(__assign({ keyType: ZodString.create(), valueType: first, typeName: ZodFirstPartyTypeKind.ZodRecord }, processCreateParams(second)));
1699
- };
1700
- return ZodRecord;
1701
- }(ZodType));
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
- var ZodMap = /** @class */ (function (_super) {
1704
- __extends(ZodMap, _super);
1705
- function ZodMap() {
1706
- return _super !== null && _super.apply(this, arguments) || this;
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
- }, { data: data });
1510
+ received: ctx.parsedType,
1511
+ });
1715
1512
  return parseUtil_1.INVALID;
1716
1513
  }
1717
- var keyType = this._def.keyType;
1718
- var valueType = this._def.valueType;
1719
- var dataMap = data;
1720
- var parseResult = new Map();
1721
- var tasks = [];
1722
- var invalid = false;
1723
- var handleParsed = function (parsedKey, parsedValue) {
1724
- if (parseUtil_1.isAsync(parsedKey) || parseUtil_1.isAsync(parsedValue)) {
1725
- tasks.push(Promise.all([parsedKey, parsedValue]).then(function (_a) {
1726
- var _b = __read(_a, 2), k = _b[0], v = _b[1];
1727
- return handleParsed(k, v);
1728
- }));
1729
- }
1730
- else if (parseUtil_1.isInvalid(parsedKey) || parseUtil_1.isInvalid(parsedValue)) {
1731
- invalid = true;
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
- return Promise.all(tasks).then(function () {
1746
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parseResult);
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
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parseResult);
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
- ZodMap.create = function (keyType, valueType, params) {
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
- var ZodSet = /** @class */ (function (_super) {
1761
- __extends(ZodSet, _super);
1762
- function ZodSet() {
1763
- return _super !== null && _super.apply(this, arguments) || this;
1764
- }
1765
- ZodSet.prototype._parse = function (ctx, data, parsedType) {
1766
- if (parsedType !== parseUtil_1.ZodParsedType.set) {
1767
- this.addIssue(ctx, {
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
- }, { data: data });
1580
+ received: ctx.parsedType,
1581
+ });
1772
1582
  return parseUtil_1.INVALID;
1773
1583
  }
1774
- var dataSet = data;
1775
- var valueType = this._def.valueType;
1776
- var parsedSet = new Set();
1777
- var tasks = [];
1778
- var invalid = false;
1779
- var handleParsed = function (parsedItem) {
1780
- if (parseUtil_1.isOk(parsedItem)) {
1781
- parsedSet.add(parsedItem.value);
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
- __spread(dataSet.values()).forEach(function (item, i) {
1791
- return handleParsed(valueType._parse(ctx.stepInto(i), item, parseUtil_1.getParsedType(item)));
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(tasks).then(function () { return (invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parsedSet)); });
1598
+ return Promise.all(elements).then((elements) => finalizeSet(elements));
1795
1599
  }
1796
1600
  else {
1797
- return invalid ? parseUtil_1.INVALID : parseUtil_1.OK(parsedSet);
1601
+ return finalizeSet(elements);
1798
1602
  }
1799
- };
1800
- ZodSet.create = function (valueType, params) {
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
- var ZodFunction = /** @class */ (function (_super) {
1807
- __extends(ZodFunction, _super);
1808
- function ZodFunction() {
1809
- var _this = _super !== null && _super.apply(this, arguments) || this;
1810
- _this.validate = _this.implement;
1811
- return _this;
1812
- }
1813
- ZodFunction.prototype._parse = function (ctx, data, parsedType) {
1814
- var _this = this;
1815
- if (parsedType !== parseUtil_1.ZodParsedType.function) {
1816
- this.addIssue(ctx, {
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
- }, { data: data });
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: parseUtil_1.pathToArray(ctx.path),
1827
- errorMaps: [ctx.errorMap],
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: parseUtil_1.pathToArray(ctx.path),
1838
- errorMaps: [ctx.errorMap],
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
- var params = { errorMap: ctx.errorMap };
1846
- var fn = data;
1660
+ const params = { errorMap: ctx.contextualErrorMap };
1661
+ const fn = ctx.data;
1847
1662
  if (this._def.returns instanceof ZodPromise) {
1848
- return parseUtil_1.OK(function () {
1849
- var args = [];
1850
- for (var _i = 0; _i < arguments.length; _i++) {
1851
- args[_i] = arguments[_i];
1852
- }
1853
- return __awaiter(_this, void 0, void 0, function () {
1854
- var error, parsedArgs, result, parsedReturns;
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(function () {
1886
- var args = [];
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
- var result = fn.apply(void 0, __spread(parsedArgs.data));
1895
- var parsedReturns = _this._def.returns.safeParse(result, params);
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
- ZodFunction.prototype.parameters = function () {
1696
+ }
1697
+ parameters() {
1904
1698
  return this._def.args;
1905
- };
1906
- ZodFunction.prototype.returnType = function () {
1699
+ }
1700
+ returnType() {
1907
1701
  return this._def.returns;
1908
- };
1909
- ZodFunction.prototype.args = function () {
1910
- var items = [];
1911
- for (var _i = 0; _i < arguments.length; _i++) {
1912
- items[_i] = arguments[_i];
1913
- }
1914
- return new ZodFunction(__assign(__assign({}, this._def), { args: ZodTuple.create(items).rest(ZodUnknown.create()) }));
1915
- };
1916
- ZodFunction.prototype.returns = function (returnType) {
1917
- return new ZodFunction(__assign(__assign({}, this._def), { returns: returnType }));
1918
- };
1919
- ZodFunction.prototype.implement = function (func) {
1920
- var validatedFunc = this.parse(func);
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
- ZodFunction.prototype.strictImplement = function (func) {
1924
- var validatedFunc = this.parse(func);
1718
+ }
1719
+ strictImplement(func) {
1720
+ const validatedFunc = this.parse(func);
1925
1721
  return validatedFunc;
1926
- };
1927
- ZodFunction.create = function (args, returns, params) {
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
- var ZodLazy = /** @class */ (function (_super) {
1936
- __extends(ZodLazy, _super);
1937
- function ZodLazy() {
1938
- return _super !== null && _super.apply(this, arguments) || this;
1939
- }
1940
- Object.defineProperty(ZodLazy.prototype, "schema", {
1941
- get: function () {
1942
- return this._def.getter();
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
- ZodLazy.prototype._parse = function (ctx, data, parsedType) {
1948
- var lazySchema = this._def.getter();
1949
- return lazySchema._parse(ctx, data, parsedType);
1950
- };
1951
- ZodLazy.create = function (getter, params) {
1952
- return new ZodLazy(__assign({ getter: getter, typeName: ZodFirstPartyTypeKind.ZodLazy }, processCreateParams(params)));
1953
- };
1954
- return ZodLazy;
1955
- }(ZodType));
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
- var ZodLiteral = /** @class */ (function (_super) {
1958
- __extends(ZodLiteral, _super);
1959
- function ZodLiteral() {
1960
- return _super !== null && _super.apply(this, arguments) || this;
1961
- }
1962
- ZodLiteral.prototype._parse = function (ctx, data, _parsedType) {
1963
- if (data !== this._def.value) {
1964
- this.addIssue(ctx, {
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
- }, { data: data });
1760
+ received: ctx.data,
1761
+ });
1969
1762
  return parseUtil_1.INVALID;
1970
1763
  }
1971
- return parseUtil_1.OK(data);
1972
- };
1973
- Object.defineProperty(ZodLiteral.prototype, "value", {
1974
- get: function () {
1975
- return this._def.value;
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
- var ZodEnum = /** @class */ (function (_super) {
1993
- __extends(ZodEnum, _super);
1994
- function ZodEnum() {
1995
- return _super !== null && _super.apply(this, arguments) || this;
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
- }, { data: data });
1791
+ });
2003
1792
  return parseUtil_1.INVALID;
2004
1793
  }
2005
- return parseUtil_1.OK(data);
2006
- };
2007
- Object.defineProperty(ZodEnum.prototype, "options", {
2008
- get: function () {
2009
- return this._def.values;
2010
- },
2011
- enumerable: false,
2012
- configurable: true
2013
- });
2014
- Object.defineProperty(ZodEnum.prototype, "enum", {
2015
- get: function () {
2016
- var e_12, _a;
2017
- var enumValues = {};
2018
- try {
2019
- for (var _b = __values(this._def.values), _c = _b.next(); !_c.done; _c = _b.next()) {
2020
- var val = _c.value;
2021
- enumValues[val] = val;
2022
- }
2023
- }
2024
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
2025
- finally {
2026
- try {
2027
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2028
- }
2029
- finally { if (e_12) throw e_12.error; }
2030
- }
2031
- return enumValues;
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
- var ZodNativeEnum = /** @class */ (function (_super) {
2085
- __extends(ZodNativeEnum, _super);
2086
- function ZodNativeEnum() {
2087
- return _super !== null && _super.apply(this, arguments) || this;
2088
- }
2089
- ZodNativeEnum.prototype._parse = function (ctx, data, _parsedType) {
2090
- var nativeEnumValues = util_1.util.getValidEnumValues(this._def.values);
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
- }, { data: data });
1831
+ });
2096
1832
  return parseUtil_1.INVALID;
2097
1833
  }
2098
- return parseUtil_1.OK(data);
2099
- };
2100
- ZodNativeEnum.create = function (values, params) {
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
- var ZodPromise = /** @class */ (function (_super) {
2107
- __extends(ZodPromise, _super);
2108
- function ZodPromise() {
2109
- return _super !== null && _super.apply(this, arguments) || this;
2110
- }
2111
- ZodPromise.prototype._parse = function (ctx, data, parsedType) {
2112
- var _this = this;
2113
- if (parsedType !== parseUtil_1.ZodParsedType.promise && ctx.async === false) {
2114
- this.addIssue(ctx, {
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
- }, { data: data });
1852
+ received: ctx.parsedType,
1853
+ });
2119
1854
  return parseUtil_1.INVALID;
2120
1855
  }
2121
- var promisified = parsedType === parseUtil_1.ZodParsedType.promise ? data : Promise.resolve(data);
2122
- return parseUtil_1.OK(promisified.then(function (data) {
2123
- return _this._def.type.parseAsync(data, {
2124
- path: parseUtil_1.pathToArray(ctx.path),
2125
- errorMap: ctx.errorMap,
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
- ZodEffects.prototype.innerType = function () {
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
- ZodEffects.prototype._parse = function (ctx, initialData, initialParsedType) {
2144
- var _this = this;
2145
- var isSync = ctx.async === false;
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
- var processed = effect.transform(initialData);
1883
+ const processed = effect.transform(ctx.data);
2151
1884
  if (ctx.async) {
2152
- return Promise.resolve(processed).then(function (val) {
2153
- return _this._def.schema._parseAsync(ctx, val, parseUtil_1.getParsedType(val));
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
- var result = this._def.schema._parseSync(ctx, processed, parseUtil_1.getParsedType(processed));
2158
- if (result instanceof Promise)
2159
- throw new Error("Asynchronous preprocess step encountered during synchronous parse operation. Use .parseAsync instead.");
2160
- return result;
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
- var invalid_1 = false;
2165
- var executeRefinement_1 = function (acc, effect) {
2166
- var result = effect.refinement(acc, checkCtx_1);
2167
- if (result instanceof Promise) {
2168
- if (isSync) {
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
- return result.then(function () { return acc; });
2172
- }
2173
- return acc;
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 parseUtil_1.pathToArray(ctx.path);
1913
+ return ctx.path;
2184
1914
  },
2185
1915
  };
2186
- checkCtx_1.addIssue = checkCtx_1.addIssue.bind(checkCtx_1);
2187
- if (isSync) {
2188
- var base = this._def.schema._parseSync(ctx, data, parsedType);
2189
- if (parseUtil_1.isInvalid(base))
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
- var result = executeRefinement_1(base.value, effect);
2192
- return invalid_1 ? parseUtil_1.INVALID : parseUtil_1.OK(result);
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, data, parsedType)
2197
- .then(function (result) {
2198
- if (parseUtil_1.isInvalid(result))
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
- return executeRefinement_1(result.value, effect);
2201
- })
2202
- .then(function (val) { return (invalid_1 ? parseUtil_1.INVALID : parseUtil_1.OK(val)); });
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
- var invalid_2 = false;
2207
- var applyTransform_1 = function (acc, effect) {
2208
- var transformed = effect.transform(acc);
2209
- if (transformed instanceof Promise && isSync) {
2210
- throw new Error("Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.");
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 transformed;
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, data, parsedType)
2224
- .then(function (base) {
2225
- if (parseUtil_1.isInvalid(base))
2226
- return parseUtil_1.INVALID;
2227
- return applyTransform_1(base.value, effect);
2228
- })
2229
- .then(function (val) { return (invalid_2 ? parseUtil_1.INVALID : parseUtil_1.OK(val)); });
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
- ZodEffects.create = function (schema, effect, params) {
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
- var ZodOptional = /** @class */ (function (_super) {
2245
- __extends(ZodOptional, _super);
2246
- function ZodOptional() {
2247
- return _super !== null && _super.apply(this, arguments) || this;
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
- ZodOptional.prototype._parse = function (ctx, data, parsedType) {
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
- ZodOptional.create = function (type, params) {
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
- var ZodNullable = /** @class */ (function (_super) {
2265
- __extends(ZodNullable, _super);
2266
- function ZodNullable() {
2267
- return _super !== null && _super.apply(this, arguments) || this;
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
- ZodNullable.prototype._parse = function (ctx, data, parsedType) {
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
- ZodDefault.prototype._parse = function (ctx, data, parsedType) {
2290
- if (parsedType === parseUtil_1.ZodParsedType.undefined) {
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(ctx, data, parseUtil_1.getParsedType(data));
2294
- };
2295
- ZodDefault.prototype.removeDefault = function () {
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
- ZodDefault.create = function (type, params) {
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
- var custom = function (check, params) {
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
- var instanceOfType = function (cls, params) {
2346
- if (params === void 0) { params = {
2347
- message: "Input not instance of " + cls.name,
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
- var stringType = ZodString.create;
2129
+ const stringType = ZodString.create;
2353
2130
  exports.string = stringType;
2354
- var numberType = ZodNumber.create;
2131
+ const numberType = ZodNumber.create;
2355
2132
  exports.number = numberType;
2356
- var bigIntType = ZodBigInt.create;
2133
+ const bigIntType = ZodBigInt.create;
2357
2134
  exports.bigint = bigIntType;
2358
- var booleanType = ZodBoolean.create;
2135
+ const booleanType = ZodBoolean.create;
2359
2136
  exports.boolean = booleanType;
2360
- var dateType = ZodDate.create;
2137
+ const dateType = ZodDate.create;
2361
2138
  exports.date = dateType;
2362
- var undefinedType = ZodUndefined.create;
2139
+ const undefinedType = ZodUndefined.create;
2363
2140
  exports.undefined = undefinedType;
2364
- var nullType = ZodNull.create;
2141
+ const nullType = ZodNull.create;
2365
2142
  exports.null = nullType;
2366
- var anyType = ZodAny.create;
2143
+ const anyType = ZodAny.create;
2367
2144
  exports.any = anyType;
2368
- var unknownType = ZodUnknown.create;
2145
+ const unknownType = ZodUnknown.create;
2369
2146
  exports.unknown = unknownType;
2370
- var neverType = ZodNever.create;
2147
+ const neverType = ZodNever.create;
2371
2148
  exports.never = neverType;
2372
- var voidType = ZodVoid.create;
2149
+ const voidType = ZodVoid.create;
2373
2150
  exports.void = voidType;
2374
- var arrayType = ZodArray.create;
2151
+ const arrayType = ZodArray.create;
2375
2152
  exports.array = arrayType;
2376
- var objectType = ZodObject.create;
2153
+ const objectType = ZodObject.create;
2377
2154
  exports.object = objectType;
2378
- var strictObjectType = ZodObject.strictCreate;
2155
+ const strictObjectType = ZodObject.strictCreate;
2379
2156
  exports.strictObject = strictObjectType;
2380
- var unionType = ZodUnion.create;
2157
+ const unionType = ZodUnion.create;
2381
2158
  exports.union = unionType;
2382
- var intersectionType = ZodIntersection.create;
2159
+ const intersectionType = ZodIntersection.create;
2383
2160
  exports.intersection = intersectionType;
2384
- var tupleType = ZodTuple.create;
2161
+ const tupleType = ZodTuple.create;
2385
2162
  exports.tuple = tupleType;
2386
- var recordType = ZodRecord.create;
2163
+ const recordType = ZodRecord.create;
2387
2164
  exports.record = recordType;
2388
- var mapType = ZodMap.create;
2165
+ const mapType = ZodMap.create;
2389
2166
  exports.map = mapType;
2390
- var setType = ZodSet.create;
2167
+ const setType = ZodSet.create;
2391
2168
  exports.set = setType;
2392
- var functionType = ZodFunction.create;
2169
+ const functionType = ZodFunction.create;
2393
2170
  exports.function = functionType;
2394
- var lazyType = ZodLazy.create;
2171
+ const lazyType = ZodLazy.create;
2395
2172
  exports.lazy = lazyType;
2396
- var literalType = ZodLiteral.create;
2173
+ const literalType = ZodLiteral.create;
2397
2174
  exports.literal = literalType;
2398
- var enumType = ZodEnum.create;
2175
+ const enumType = ZodEnum.create;
2399
2176
  exports.enum = enumType;
2400
- var nativeEnumType = ZodNativeEnum.create;
2177
+ const nativeEnumType = ZodNativeEnum.create;
2401
2178
  exports.nativeEnum = nativeEnumType;
2402
- var promiseType = ZodPromise.create;
2179
+ const promiseType = ZodPromise.create;
2403
2180
  exports.promise = promiseType;
2404
- var effectsType = ZodEffects.create;
2181
+ const effectsType = ZodEffects.create;
2405
2182
  exports.effect = effectsType;
2406
2183
  exports.transformer = effectsType;
2407
- var optionalType = ZodOptional.create;
2184
+ const optionalType = ZodOptional.create;
2408
2185
  exports.optional = optionalType;
2409
- var nullableType = ZodNullable.create;
2186
+ const nullableType = ZodNullable.create;
2410
2187
  exports.nullable = nullableType;
2411
- var preprocessType = ZodEffects.createWithPreprocess;
2188
+ const preprocessType = ZodEffects.createWithPreprocess;
2412
2189
  exports.preprocess = preprocessType;
2413
- var ostring = function () { return stringType().optional(); };
2190
+ const ostring = () => stringType().optional();
2414
2191
  exports.ostring = ostring;
2415
- var onumber = function () { return numberType().optional(); };
2192
+ const onumber = () => numberType().optional();
2416
2193
  exports.onumber = onumber;
2417
- var oboolean = function () { return booleanType().optional(); };
2194
+ const oboolean = () => booleanType().optional();
2418
2195
  exports.oboolean = oboolean;
2419
2196
  //# sourceMappingURL=types.js.map