zod 3.16.1 → 3.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -47,6 +47,8 @@
47
47
  - [Sponsors](#sponsors)
48
48
  - [Ecosystem](#ecosystem)
49
49
  - [Installation](#installation)
50
+ - [Node](#node)
51
+ - [Deno](#deno)
50
52
  - [Basic usage](#basic-usage)
51
53
  - [Primitives](#primitives)
52
54
  - [Literals](#literals)
@@ -288,30 +290,47 @@ There are a growing number of tools that are built atop or support Zod natively!
288
290
 
289
291
  ## Installation
290
292
 
293
+ ### Requirements
294
+
295
+ - TypeScript 4.1+!
296
+ - You must enable `strict` mode in your `tsconfig.json`. This is a best practice for all TypeScript projects.
297
+
298
+ ```ts
299
+ // tsconfig.json
300
+ {
301
+ // ...
302
+ "compilerOptions": {
303
+ // ...
304
+ "strict": true
305
+ }
306
+ }
307
+ ```
308
+
309
+ ### Node/NPM
310
+
291
311
  To install Zod v3:
292
312
 
293
313
  ```sh
294
- npm install zod
314
+ npm install zod # npm
315
+ yarn add zod # yarn
316
+ pnpm add zod # pnpm
295
317
  ```
296
318
 
297
- ⚠️ IMPORTANT: You must enable `strict` mode in your `tsconfig.json`. This is a best practice for all TypeScript projects.
319
+ ### Deno
320
+
321
+ Unlike Node, Deno relies on direct URL imports instead of a package manager like NPM. Zod is available on [deno.land/x](deno.land/x). The latest version can be imported like so:
298
322
 
299
323
  ```ts
300
- // tsconfig.json
301
- {
302
- // ...
303
- "compilerOptions": {
304
- // ...
305
- "strict": true
306
- }
307
- }
324
+ import { z } from "https://deno.land/x/zod/mod.ts";
325
+ ```
326
+
327
+ You can also specify a particular version:
328
+
329
+ ```ts
330
+ import { z } from from "https://deno.land/x/zod@v3.16.1/mod.ts"
308
331
  ```
309
332
 
310
- > **TypeScript requirements**
311
- >
312
- > - Zod 3.x requires TypeScript 4.1+
313
- > - Zod 2.x requires TypeScript 3.7+
314
- > - Zod 1.x requires TypeScript 3.3+
333
+ > The rest of this README assumes you are using NPM and importing directly from the `"zod"` package.
315
334
 
316
335
  ## Basic usage
317
336
 
@@ -402,6 +421,9 @@ z.string().uuid();
402
421
  z.string().cuid();
403
422
  z.string().regex(regex);
404
423
 
424
+ // trim whitespace
425
+ z.string().trim();
426
+
405
427
  // deprecated, equivalent to .min(1)
406
428
  z.string().nonempty();
407
429
 
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
@@ -1,11 +1,7 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
9
5
  }) : (function(o, m, k, k2) {
10
6
  if (k2 === undefined) k2 = k;
11
7
  o[k2] = m[k];
@@ -14,7 +10,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
11
  };
16
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.ZodParsedType = exports.getParsedType = void 0;
17
14
  __exportStar(require("./helpers/parseUtil"), exports);
18
15
  __exportStar(require("./helpers/typeAliases"), exports);
16
+ var util_1 = require("./helpers/util");
17
+ Object.defineProperty(exports, "getParsedType", { enumerable: true, get: function () { return util_1.getParsedType; } });
18
+ Object.defineProperty(exports, "ZodParsedType", { enumerable: true, get: function () { return util_1.ZodParsedType; } });
19
19
  __exportStar(require("./types"), exports);
20
20
  __exportStar(require("./ZodError"), exports);
@@ -2,8 +2,12 @@ export declare namespace errorUtil {
2
2
  type ErrMessage = string | {
3
3
  message?: string;
4
4
  };
5
- const errToObj: (message?: ErrMessage | undefined) => {
5
+ const errToObj: (message?: string | {
6
+ message?: string | undefined;
7
+ } | undefined) => {
6
8
  message?: string | undefined;
7
9
  };
8
- const toString: (message?: ErrMessage | undefined) => string | undefined;
10
+ const toString: (message?: string | {
11
+ message?: string | undefined;
12
+ } | undefined) => string | undefined;
9
13
  }
@@ -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, ZodErrorMap, 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 || [])];
@@ -92,7 +26,7 @@ const makeIssue = (params) => {
92
26
  exports.makeIssue = makeIssue;
93
27
  exports.EMPTY_PATH = [];
94
28
  function addIssueToContext(ctx, issueData) {
95
- const issue = (0, exports.makeIssue)({
29
+ const issue = exports.makeIssue({
96
30
  issueData: issueData,
97
31
  data: ctx.data,
98
32
  path: ctx.path,
@@ -100,7 +34,7 @@ function addIssueToContext(ctx, issueData) {
100
34
  ctx.common.contextualErrorMap,
101
35
  ctx.schemaErrorMap,
102
36
  ZodError_1.overrideErrorMap,
103
- ZodError_1.defaultErrorMap, // then global default map
37
+ ZodError_1.defaultErrorMap,
104
38
  ].filter((x) => !!x),
105
39
  });
106
40
  ctx.common.issues.push(issue);
@@ -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.js CHANGED
@@ -1,11 +1,7 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
9
5
  }) : (function(o, m, k, k2) {
10
6
  if (k2 === undefined) k2 = k;
11
7
  o[k2] = m[k];
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 || [])];
@@ -354,7 +354,7 @@ function addIssueToContext(ctx, issueData) {
354
354
  ctx.common.contextualErrorMap,
355
355
  ctx.schemaErrorMap,
356
356
  overrideErrorMap,
357
- defaultErrorMap, // then global default map
357
+ defaultErrorMap,
358
358
  ].filter((x) => !!x),
359
359
  });
360
360
  ctx.common.issues.push(issue);
@@ -713,6 +713,10 @@ class ZodString extends ZodType {
713
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
  }
@@ -1378,7 +1388,7 @@ var objectUtil;
1378
1388
  objectUtil.mergeShapes = (first, second) => {
1379
1389
  return {
1380
1390
  ...first,
1381
- ...second, // second overwrites first
1391
+ ...second,
1382
1392
  };
1383
1393
  };
1384
1394
  })(objectUtil || (objectUtil = {}));
@@ -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 {
@@ -2810,8 +2823,8 @@ const oboolean = () => booleanType().optional();
2810
2823
 
2811
2824
  var mod = /*#__PURE__*/Object.freeze({
2812
2825
  __proto__: null,
2813
- ZodParsedType: ZodParsedType,
2814
2826
  getParsedType: getParsedType,
2827
+ ZodParsedType: ZodParsedType,
2815
2828
  makeIssue: makeIssue,
2816
2829
  EMPTY_PATH: EMPTY_PATH,
2817
2830
  addIssueToContext: addIssueToContext,