zod 3.16.0 → 3.17.2

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/ZodError.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { TypeOf, ZodType } from ".";
2
- import { ZodParsedType } from "./helpers/parseUtil";
3
2
  import { Primitive } from "./helpers/typeAliases";
4
- import { util } from "./helpers/util";
3
+ import { util, ZodParsedType } from "./helpers/util";
5
4
  declare type allKeys<T> = T extends any ? keyof T : never;
6
5
  export declare type inferFlattenedErrors<T extends ZodType<any, any, any>, U = string> = typeToFlattenedError<TypeOf<T>, U>;
7
6
  export declare type typeToFlattenedError<T, U = string> = {
package/lib/ZodError.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setErrorMap = exports.overrideErrorMap = exports.defaultErrorMap = exports.ZodError = exports.quotelessJson = exports.ZodIssueCode = void 0;
4
- const parseUtil_1 = require("./helpers/parseUtil");
5
4
  const util_1 = require("./helpers/util");
6
5
  exports.ZodIssueCode = util_1.util.arrayToEnum([
7
6
  "invalid_type",
@@ -134,7 +133,7 @@ const defaultErrorMap = (issue, _ctx) => {
134
133
  let message;
135
134
  switch (issue.code) {
136
135
  case exports.ZodIssueCode.invalid_type:
137
- if (issue.received === parseUtil_1.ZodParsedType.undefined) {
136
+ if (issue.received === util_1.ZodParsedType.undefined) {
138
137
  message = "Required";
139
138
  }
140
139
  else {
package/lib/external.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./helpers/parseUtil";
2
2
  export * from "./helpers/typeAliases";
3
+ export { getParsedType, ZodParsedType } from "./helpers/util";
3
4
  export * from "./types";
4
5
  export * from "./ZodError";
package/lib/external.js CHANGED
@@ -14,7 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.ZodParsedType = exports.getParsedType = void 0;
17
18
  __exportStar(require("./helpers/parseUtil"), exports);
18
19
  __exportStar(require("./helpers/typeAliases"), exports);
20
+ var util_1 = require("./helpers/util");
21
+ Object.defineProperty(exports, "getParsedType", { enumerable: true, get: function () { return util_1.getParsedType; } });
22
+ Object.defineProperty(exports, "ZodParsedType", { enumerable: true, get: function () { return util_1.ZodParsedType; } });
19
23
  __exportStar(require("./types"), exports);
20
24
  __exportStar(require("./ZodError"), exports);
@@ -1,28 +1,5 @@
1
- import { IssueData, ZodErrorMap, ZodIssue } from "../ZodError";
2
- export declare const ZodParsedType: {
3
- function: "function";
4
- number: "number";
5
- string: "string";
6
- nan: "nan";
7
- integer: "integer";
8
- float: "float";
9
- boolean: "boolean";
10
- date: "date";
11
- bigint: "bigint";
12
- symbol: "symbol";
13
- undefined: "undefined";
14
- null: "null";
15
- array: "array";
16
- object: "object";
17
- unknown: "unknown";
18
- promise: "promise";
19
- void: "void";
20
- never: "never";
21
- map: "map";
22
- set: "set";
23
- };
24
- export declare type ZodParsedType = keyof typeof ZodParsedType;
25
- export declare const getParsedType: (data: any) => ZodParsedType;
1
+ import { type IssueData, type ZodErrorMap, type ZodIssue } from "../ZodError";
2
+ import { type ZodParsedType } from "./util";
26
3
  export declare const makeIssue: (params: {
27
4
  data: any;
28
5
  path: (string | number)[];
@@ -1,73 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isAsync = exports.isValid = exports.isDirty = exports.isAborted = exports.OK = exports.DIRTY = exports.INVALID = exports.ParseStatus = exports.addIssueToContext = exports.EMPTY_PATH = exports.makeIssue = exports.getParsedType = exports.ZodParsedType = void 0;
3
+ exports.isAsync = exports.isValid = exports.isDirty = exports.isAborted = exports.OK = exports.DIRTY = exports.INVALID = exports.ParseStatus = exports.addIssueToContext = exports.EMPTY_PATH = exports.makeIssue = void 0;
4
4
  const ZodError_1 = require("../ZodError");
5
- const util_1 = require("./util");
6
- exports.ZodParsedType = util_1.util.arrayToEnum([
7
- "string",
8
- "nan",
9
- "number",
10
- "integer",
11
- "float",
12
- "boolean",
13
- "date",
14
- "bigint",
15
- "symbol",
16
- "function",
17
- "undefined",
18
- "null",
19
- "array",
20
- "object",
21
- "unknown",
22
- "promise",
23
- "void",
24
- "never",
25
- "map",
26
- "set",
27
- ]);
28
- const getParsedType = (data) => {
29
- const t = typeof data;
30
- switch (t) {
31
- case "undefined":
32
- return exports.ZodParsedType.undefined;
33
- case "string":
34
- return exports.ZodParsedType.string;
35
- case "number":
36
- return isNaN(data) ? exports.ZodParsedType.nan : exports.ZodParsedType.number;
37
- case "boolean":
38
- return exports.ZodParsedType.boolean;
39
- case "function":
40
- return exports.ZodParsedType.function;
41
- case "bigint":
42
- return exports.ZodParsedType.bigint;
43
- case "object":
44
- if (Array.isArray(data)) {
45
- return exports.ZodParsedType.array;
46
- }
47
- if (data === null) {
48
- return exports.ZodParsedType.null;
49
- }
50
- if (data.then &&
51
- typeof data.then === "function" &&
52
- data.catch &&
53
- typeof data.catch === "function") {
54
- return exports.ZodParsedType.promise;
55
- }
56
- if (typeof Map !== "undefined" && data instanceof Map) {
57
- return exports.ZodParsedType.map;
58
- }
59
- if (typeof Set !== "undefined" && data instanceof Set) {
60
- return exports.ZodParsedType.set;
61
- }
62
- if (typeof Date !== "undefined" && data instanceof Date) {
63
- return exports.ZodParsedType.date;
64
- }
65
- return exports.ZodParsedType.object;
66
- default:
67
- return exports.ZodParsedType.unknown;
68
- }
69
- };
70
- exports.getParsedType = getParsedType;
71
5
  const makeIssue = (params) => {
72
6
  const { data, path, errorMaps, issueData } = params;
73
7
  const fullPath = [...path, ...(issueData.path || [])];
@@ -17,3 +17,27 @@ export declare namespace util {
17
17
  const isInteger: NumberConstructor["isInteger"];
18
18
  function joinValues<T extends any[]>(array: T, separator?: string): string;
19
19
  }
20
+ export declare const ZodParsedType: {
21
+ function: "function";
22
+ number: "number";
23
+ string: "string";
24
+ nan: "nan";
25
+ integer: "integer";
26
+ float: "float";
27
+ boolean: "boolean";
28
+ date: "date";
29
+ bigint: "bigint";
30
+ symbol: "symbol";
31
+ undefined: "undefined";
32
+ null: "null";
33
+ array: "array";
34
+ object: "object";
35
+ unknown: "unknown";
36
+ promise: "promise";
37
+ void: "void";
38
+ never: "never";
39
+ map: "map";
40
+ set: "set";
41
+ };
42
+ export declare type ZodParsedType = keyof typeof ZodParsedType;
43
+ export declare const getParsedType: (data: any) => ZodParsedType;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.util = void 0;
3
+ exports.getParsedType = exports.ZodParsedType = exports.util = void 0;
4
4
  var util;
5
5
  (function (util) {
6
6
  function assertNever(_x) {
@@ -55,3 +55,68 @@ var util;
55
55
  }
56
56
  util.joinValues = joinValues;
57
57
  })(util = exports.util || (exports.util = {}));
58
+ exports.ZodParsedType = util.arrayToEnum([
59
+ "string",
60
+ "nan",
61
+ "number",
62
+ "integer",
63
+ "float",
64
+ "boolean",
65
+ "date",
66
+ "bigint",
67
+ "symbol",
68
+ "function",
69
+ "undefined",
70
+ "null",
71
+ "array",
72
+ "object",
73
+ "unknown",
74
+ "promise",
75
+ "void",
76
+ "never",
77
+ "map",
78
+ "set",
79
+ ]);
80
+ const getParsedType = (data) => {
81
+ const t = typeof data;
82
+ switch (t) {
83
+ case "undefined":
84
+ return exports.ZodParsedType.undefined;
85
+ case "string":
86
+ return exports.ZodParsedType.string;
87
+ case "number":
88
+ return isNaN(data) ? exports.ZodParsedType.nan : exports.ZodParsedType.number;
89
+ case "boolean":
90
+ return exports.ZodParsedType.boolean;
91
+ case "function":
92
+ return exports.ZodParsedType.function;
93
+ case "bigint":
94
+ return exports.ZodParsedType.bigint;
95
+ case "object":
96
+ if (Array.isArray(data)) {
97
+ return exports.ZodParsedType.array;
98
+ }
99
+ if (data === null) {
100
+ return exports.ZodParsedType.null;
101
+ }
102
+ if (data.then &&
103
+ typeof data.then === "function" &&
104
+ data.catch &&
105
+ typeof data.catch === "function") {
106
+ return exports.ZodParsedType.promise;
107
+ }
108
+ if (typeof Map !== "undefined" && data instanceof Map) {
109
+ return exports.ZodParsedType.map;
110
+ }
111
+ if (typeof Set !== "undefined" && data instanceof Set) {
112
+ return exports.ZodParsedType.set;
113
+ }
114
+ if (typeof Date !== "undefined" && data instanceof Date) {
115
+ return exports.ZodParsedType.date;
116
+ }
117
+ return exports.ZodParsedType.object;
118
+ default:
119
+ return exports.ZodParsedType.unknown;
120
+ }
121
+ };
122
+ exports.getParsedType = getParsedType;
package/lib/index.mjs CHANGED
@@ -52,6 +52,70 @@ var util;
52
52
  }
53
53
  util.joinValues = joinValues;
54
54
  })(util || (util = {}));
55
+ const ZodParsedType = util.arrayToEnum([
56
+ "string",
57
+ "nan",
58
+ "number",
59
+ "integer",
60
+ "float",
61
+ "boolean",
62
+ "date",
63
+ "bigint",
64
+ "symbol",
65
+ "function",
66
+ "undefined",
67
+ "null",
68
+ "array",
69
+ "object",
70
+ "unknown",
71
+ "promise",
72
+ "void",
73
+ "never",
74
+ "map",
75
+ "set",
76
+ ]);
77
+ const getParsedType = (data) => {
78
+ const t = typeof data;
79
+ switch (t) {
80
+ case "undefined":
81
+ return ZodParsedType.undefined;
82
+ case "string":
83
+ return ZodParsedType.string;
84
+ case "number":
85
+ return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
86
+ case "boolean":
87
+ return ZodParsedType.boolean;
88
+ case "function":
89
+ return ZodParsedType.function;
90
+ case "bigint":
91
+ return ZodParsedType.bigint;
92
+ case "object":
93
+ if (Array.isArray(data)) {
94
+ return ZodParsedType.array;
95
+ }
96
+ if (data === null) {
97
+ return ZodParsedType.null;
98
+ }
99
+ if (data.then &&
100
+ typeof data.then === "function" &&
101
+ data.catch &&
102
+ typeof data.catch === "function") {
103
+ return ZodParsedType.promise;
104
+ }
105
+ if (typeof Map !== "undefined" && data instanceof Map) {
106
+ return ZodParsedType.map;
107
+ }
108
+ if (typeof Set !== "undefined" && data instanceof Set) {
109
+ return ZodParsedType.set;
110
+ }
111
+ if (typeof Date !== "undefined" && data instanceof Date) {
112
+ return ZodParsedType.date;
113
+ }
114
+ return ZodParsedType.object;
115
+ default:
116
+ return ZodParsedType.unknown;
117
+ }
118
+ };
55
119
 
56
120
  const ZodIssueCode = util.arrayToEnum([
57
121
  "invalid_type",
@@ -259,70 +323,6 @@ const setErrorMap = (map) => {
259
323
  overrideErrorMap = map;
260
324
  };
261
325
 
262
- const ZodParsedType = util.arrayToEnum([
263
- "string",
264
- "nan",
265
- "number",
266
- "integer",
267
- "float",
268
- "boolean",
269
- "date",
270
- "bigint",
271
- "symbol",
272
- "function",
273
- "undefined",
274
- "null",
275
- "array",
276
- "object",
277
- "unknown",
278
- "promise",
279
- "void",
280
- "never",
281
- "map",
282
- "set",
283
- ]);
284
- const getParsedType = (data) => {
285
- const t = typeof data;
286
- switch (t) {
287
- case "undefined":
288
- return ZodParsedType.undefined;
289
- case "string":
290
- return ZodParsedType.string;
291
- case "number":
292
- return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
293
- case "boolean":
294
- return ZodParsedType.boolean;
295
- case "function":
296
- return ZodParsedType.function;
297
- case "bigint":
298
- return ZodParsedType.bigint;
299
- case "object":
300
- if (Array.isArray(data)) {
301
- return ZodParsedType.array;
302
- }
303
- if (data === null) {
304
- return ZodParsedType.null;
305
- }
306
- if (data.then &&
307
- typeof data.then === "function" &&
308
- data.catch &&
309
- typeof data.catch === "function") {
310
- return ZodParsedType.promise;
311
- }
312
- if (typeof Map !== "undefined" && data instanceof Map) {
313
- return ZodParsedType.map;
314
- }
315
- if (typeof Set !== "undefined" && data instanceof Set) {
316
- return ZodParsedType.set;
317
- }
318
- if (typeof Date !== "undefined" && data instanceof Date) {
319
- return ZodParsedType.date;
320
- }
321
- return ZodParsedType.object;
322
- default:
323
- return ZodParsedType.unknown;
324
- }
325
- };
326
326
  const makeIssue = (params) => {
327
327
  const { data, path, errorMaps, issueData } = params;
328
328
  const fullPath = [...path, ...(issueData.path || [])];
@@ -709,10 +709,14 @@ class ZodString extends ZodType {
709
709
  ...errorUtil.errToObj(message),
710
710
  });
711
711
  /**
712
- * Deprecated.
713
- * Use z.string().min(1) instead.
712
+ * @deprecated Use z.string().min(1) instead.
713
+ * @see {@link ZodString.min}
714
714
  */
715
715
  this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
716
+ this.trim = () => new ZodString({
717
+ ...this._def,
718
+ checks: [...this._def.checks, { kind: "trim" }],
719
+ });
716
720
  }
717
721
  _parse(input) {
718
722
  const parsedType = this._getType(input);
@@ -816,6 +820,12 @@ class ZodString extends ZodType {
816
820
  status.dirty();
817
821
  }
818
822
  }
823
+ else if (check.kind === "trim") {
824
+ input.data = input.data.trim();
825
+ }
826
+ else {
827
+ util.assertNever(check);
828
+ }
819
829
  }
820
830
  return { status: status.value, value: input.data };
821
831
  }
@@ -1593,7 +1603,9 @@ class ZodObject extends ZodType {
1593
1603
  pick(mask) {
1594
1604
  const shape = {};
1595
1605
  util.objectKeys(mask).map((key) => {
1596
- shape[key] = this.shape[key];
1606
+ // only add to shape if key corresponds to an element of the current shape
1607
+ if (this.shape[key])
1608
+ shape[key] = this.shape[key];
1597
1609
  });
1598
1610
  return new ZodObject({
1599
1611
  ...this._def,
@@ -2382,10 +2394,11 @@ ZodLiteral.create = (value, params) => {
2382
2394
  ...processCreateParams(params),
2383
2395
  });
2384
2396
  };
2385
- function createZodEnum(values) {
2397
+ function createZodEnum(values, params) {
2386
2398
  return new ZodEnum({
2387
2399
  values: values,
2388
2400
  typeName: ZodFirstPartyTypeKind.ZodEnum,
2401
+ ...processCreateParams(params),
2389
2402
  });
2390
2403
  }
2391
2404
  class ZodEnum extends ZodType {
@@ -2721,9 +2734,15 @@ ZodNaN.create = (params) => {
2721
2734
  ...processCreateParams(params),
2722
2735
  });
2723
2736
  };
2724
- const custom = (check, params) => {
2737
+ const custom = (check, params = {}, fatal) => {
2725
2738
  if (check)
2726
- return ZodAny.create().refine(check, params);
2739
+ return ZodAny.create().superRefine((data, ctx) => {
2740
+ if (!check(data)) {
2741
+ const p = typeof params === "function" ? params(data) : params;
2742
+ const p2 = typeof p === "string" ? { message: p } : p;
2743
+ ctx.addIssue({ code: "custom", ...p2, fatal });
2744
+ }
2745
+ });
2727
2746
  return ZodAny.create();
2728
2747
  };
2729
2748
  const late = {
@@ -2765,7 +2784,7 @@ var ZodFirstPartyTypeKind;
2765
2784
  })(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
2766
2785
  const instanceOfType = (cls, params = {
2767
2786
  message: `Input not instance of ${cls.name}`,
2768
- }) => custom((data) => data instanceof cls, params);
2787
+ }) => custom((data) => data instanceof cls, params, true);
2769
2788
  const stringType = ZodString.create;
2770
2789
  const numberType = ZodNumber.create;
2771
2790
  const nanType = ZodNaN.create;
@@ -2804,8 +2823,8 @@ const oboolean = () => booleanType().optional();
2804
2823
 
2805
2824
  var mod = /*#__PURE__*/Object.freeze({
2806
2825
  __proto__: null,
2807
- ZodParsedType: ZodParsedType,
2808
2826
  getParsedType: getParsedType,
2827
+ ZodParsedType: ZodParsedType,
2809
2828
  makeIssue: makeIssue,
2810
2829
  EMPTY_PATH: EMPTY_PATH,
2811
2830
  addIssueToContext: addIssueToContext,