zod 3.14.2 → 3.14.5

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/index.mjs CHANGED
@@ -49,6 +49,7 @@ var util;
49
49
 
50
50
  const ZodIssueCode = util.arrayToEnum([
51
51
  "invalid_type",
52
+ "invalid_literal",
52
53
  "custom",
53
54
  "invalid_union",
54
55
  "invalid_union_discriminator",
@@ -71,51 +72,6 @@ class ZodError extends Error {
71
72
  constructor(issues) {
72
73
  super();
73
74
  this.issues = [];
74
- this.format = () => {
75
- const fieldErrors = { _errors: [] };
76
- const processError = (error) => {
77
- for (const issue of error.issues) {
78
- if (issue.code === "invalid_union") {
79
- issue.unionErrors.map(processError);
80
- }
81
- else if (issue.code === "invalid_return_type") {
82
- processError(issue.returnTypeError);
83
- }
84
- else if (issue.code === "invalid_arguments") {
85
- processError(issue.argumentsError);
86
- }
87
- else if (issue.path.length === 0) {
88
- fieldErrors._errors.push(issue.message);
89
- }
90
- else {
91
- let curr = fieldErrors;
92
- let i = 0;
93
- while (i < issue.path.length) {
94
- const el = issue.path[i];
95
- const terminal = i === issue.path.length - 1;
96
- if (!terminal) {
97
- if (typeof el === "string") {
98
- curr[el] = curr[el] || { _errors: [] };
99
- }
100
- else if (typeof el === "number") {
101
- const errorArray = [];
102
- errorArray._errors = [];
103
- curr[el] = curr[el] || errorArray;
104
- }
105
- }
106
- else {
107
- curr[el] = curr[el] || { _errors: [] };
108
- curr[el]._errors.push(issue.message);
109
- }
110
- curr = curr[el];
111
- i++;
112
- }
113
- }
114
- }
115
- };
116
- processError(this);
117
- return fieldErrors;
118
- };
119
75
  this.addIssue = (sub) => {
120
76
  this.issues = [...this.issues, sub];
121
77
  };
@@ -136,6 +92,55 @@ class ZodError extends Error {
136
92
  get errors() {
137
93
  return this.issues;
138
94
  }
95
+ format(_mapper) {
96
+ const mapper = _mapper ||
97
+ function (issue) {
98
+ return issue.message;
99
+ };
100
+ const fieldErrors = { _errors: [] };
101
+ const processError = (error) => {
102
+ for (const issue of error.issues) {
103
+ if (issue.code === "invalid_union") {
104
+ issue.unionErrors.map(processError);
105
+ }
106
+ else if (issue.code === "invalid_return_type") {
107
+ processError(issue.returnTypeError);
108
+ }
109
+ else if (issue.code === "invalid_arguments") {
110
+ processError(issue.argumentsError);
111
+ }
112
+ else if (issue.path.length === 0) {
113
+ fieldErrors._errors.push(mapper(issue));
114
+ }
115
+ else {
116
+ let curr = fieldErrors;
117
+ let i = 0;
118
+ while (i < issue.path.length) {
119
+ const el = issue.path[i];
120
+ const terminal = i === issue.path.length - 1;
121
+ if (!terminal) {
122
+ if (typeof el === "string") {
123
+ curr[el] = curr[el] || { _errors: [] };
124
+ }
125
+ else if (typeof el === "number") {
126
+ const errorArray = [];
127
+ errorArray._errors = [];
128
+ curr[el] = curr[el] || errorArray;
129
+ }
130
+ }
131
+ else {
132
+ curr[el] = curr[el] || { _errors: [] };
133
+ curr[el]._errors.push(mapper(issue));
134
+ }
135
+ curr = curr[el];
136
+ i++;
137
+ }
138
+ }
139
+ }
140
+ };
141
+ processError(this);
142
+ return fieldErrors;
143
+ }
139
144
  toString() {
140
145
  return this.message;
141
146
  }
@@ -178,6 +183,9 @@ const defaultErrorMap = (issue, _ctx) => {
178
183
  message = `Expected ${issue.expected}, received ${issue.received}`;
179
184
  }
180
185
  break;
186
+ case ZodIssueCode.invalid_literal:
187
+ message = `Invalid literal value, expected ${JSON.stringify(issue.expected)}`;
188
+ break;
181
189
  case ZodIssueCode.unrecognized_keys:
182
190
  message = `Unrecognized key(s) in object: ${issue.keys
183
191
  .map((k) => `'${k}'`)
@@ -419,6 +427,17 @@ var errorUtil;
419
427
  errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
420
428
  })(errorUtil || (errorUtil = {}));
421
429
 
430
+ class ParseInputLazyPath {
431
+ constructor(parent, value, path, key) {
432
+ this.parent = parent;
433
+ this.data = value;
434
+ this._path = path;
435
+ this._key = key;
436
+ }
437
+ get path() {
438
+ return this._path.concat(this._key);
439
+ }
440
+ }
422
441
  const handleResult = (ctx, result) => {
423
442
  if (isValid(result)) {
424
443
  return { success: true, data: result.value };
@@ -475,8 +494,8 @@ class ZodType {
475
494
  this.transform = this.transform.bind(this);
476
495
  this.default = this.default.bind(this);
477
496
  this.describe = this.describe.bind(this);
478
- this.isOptional = this.isOptional.bind(this);
479
497
  this.isNullable = this.isNullable.bind(this);
498
+ this.isOptional = this.isOptional.bind(this);
480
499
  }
481
500
  get description() {
482
501
  return this._def.description;
@@ -530,7 +549,6 @@ class ZodType {
530
549
  common: {
531
550
  issues: [],
532
551
  async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
533
- typeCache: typeof Map !== "undefined" ? new Map() : undefined,
534
552
  contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
535
553
  },
536
554
  path: (params === null || params === void 0 ? void 0 : params.path) || [],
@@ -554,7 +572,6 @@ class ZodType {
554
572
  issues: [],
555
573
  contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
556
574
  async: true,
557
- typeCache: typeof Map !== "undefined" ? new Map() : undefined,
558
575
  },
559
576
  path: (params === null || params === void 0 ? void 0 : params.path) || [],
560
577
  schemaErrorMap: this._def.errorMap,
@@ -1308,21 +1325,13 @@ class ZodArray extends ZodType {
1308
1325
  }
1309
1326
  if (ctx.common.async) {
1310
1327
  return Promise.all(ctx.data.map((item, i) => {
1311
- return def.type._parseAsync({
1312
- parent: ctx,
1313
- path: [...ctx.path, i],
1314
- data: item,
1315
- });
1328
+ return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
1316
1329
  })).then((result) => {
1317
1330
  return ParseStatus.mergeArray(status, result);
1318
1331
  });
1319
1332
  }
1320
1333
  const result = ctx.data.map((item, i) => {
1321
- return def.type._parseSync({
1322
- parent: ctx,
1323
- path: [...ctx.path, i],
1324
- data: item,
1325
- });
1334
+ return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
1326
1335
  });
1327
1336
  return ParseStatus.mergeArray(status, result);
1328
1337
  }
@@ -1442,19 +1451,19 @@ class ZodObject extends ZodType {
1442
1451
  }
1443
1452
  const { status, ctx } = this._processInputParams(input);
1444
1453
  const { shape, keys: shapeKeys } = this._getCached();
1445
- const dataKeys = util.objectKeys(ctx.data);
1446
- const extraKeys = dataKeys.filter((k) => !shapeKeys.includes(k));
1454
+ const extraKeys = [];
1455
+ for (const key in ctx.data) {
1456
+ if (!shapeKeys.includes(key)) {
1457
+ extraKeys.push(key);
1458
+ }
1459
+ }
1447
1460
  const pairs = [];
1448
1461
  for (const key of shapeKeys) {
1449
1462
  const keyValidator = shape[key];
1450
1463
  const value = ctx.data[key];
1451
1464
  pairs.push({
1452
1465
  key: { status: "valid", value: key },
1453
- value: keyValidator._parse({
1454
- parent: ctx,
1455
- data: value,
1456
- path: [...ctx.path, key],
1457
- }),
1466
+ value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
1458
1467
  alwaysSet: key in ctx.data,
1459
1468
  });
1460
1469
  }
@@ -1489,7 +1498,7 @@ class ZodObject extends ZodType {
1489
1498
  const value = ctx.data[key];
1490
1499
  pairs.push({
1491
1500
  key: { status: "valid", value: key },
1492
- value: catchall._parse({ parent: ctx, path: [...ctx.path, key], data: value } //, ctx.child(key), value, getParsedType(value)
1501
+ value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
1493
1502
  ),
1494
1503
  alwaysSet: key in ctx.data,
1495
1504
  });
@@ -1985,11 +1994,7 @@ class ZodTuple extends ZodType {
1985
1994
  const schema = this._def.items[itemIndex] || this._def.rest;
1986
1995
  if (!schema)
1987
1996
  return null;
1988
- return schema._parse({
1989
- data: item,
1990
- path: [...ctx.path, itemIndex],
1991
- parent: ctx,
1992
- });
1997
+ return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
1993
1998
  })
1994
1999
  .filter((x) => !!x); // filter nulls
1995
2000
  if (ctx.common.async) {
@@ -2041,16 +2046,8 @@ class ZodRecord extends ZodType {
2041
2046
  const valueType = this._def.valueType;
2042
2047
  for (const key in ctx.data) {
2043
2048
  pairs.push({
2044
- key: keyType._parse({
2045
- data: key,
2046
- path: [...ctx.path, key],
2047
- parent: ctx,
2048
- }),
2049
- value: valueType._parse({
2050
- data: ctx.data[key],
2051
- path: [...ctx.path, key],
2052
- parent: ctx,
2053
- }),
2049
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
2050
+ value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
2054
2051
  });
2055
2052
  }
2056
2053
  if (ctx.common.async) {
@@ -2095,16 +2092,8 @@ class ZodMap extends ZodType {
2095
2092
  const valueType = this._def.valueType;
2096
2093
  const pairs = [...ctx.data.entries()].map(([key, value], index) => {
2097
2094
  return {
2098
- key: keyType._parse({
2099
- data: key,
2100
- path: [...ctx.path, index, "key"],
2101
- parent: ctx,
2102
- }),
2103
- value: valueType._parse({
2104
- data: value,
2105
- path: [...ctx.path, index, "value"],
2106
- parent: ctx,
2107
- }),
2095
+ key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
2096
+ value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
2108
2097
  };
2109
2098
  });
2110
2099
  if (ctx.common.async) {
@@ -2197,7 +2186,7 @@ class ZodSet extends ZodType {
2197
2186
  }
2198
2187
  return { status: status.value, value: parsedSet };
2199
2188
  }
2200
- const elements = [...ctx.data.values()].map((item, i) => valueType._parse({ data: item, path: [...ctx.path, i], parent: ctx }));
2189
+ const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
2201
2190
  if (ctx.common.async) {
2202
2191
  return Promise.all(elements).then((elements) => finalizeSet(elements));
2203
2192
  }
@@ -2375,9 +2364,8 @@ class ZodLiteral extends ZodType {
2375
2364
  if (input.data !== this._def.value) {
2376
2365
  const ctx = this._getOrReturnCtx(input);
2377
2366
  addIssueToContext(ctx, {
2378
- code: ZodIssueCode.invalid_type,
2379
- expected: getParsedType(this._def.value),
2380
- received: ctx.parsedType,
2367
+ code: ZodIssueCode.invalid_literal,
2368
+ expected: this._def.value,
2381
2369
  });
2382
2370
  return INVALID;
2383
2371
  }