zod 3.17.3 → 3.17.4

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
@@ -22,7 +22,7 @@
22
22
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
23
23
  <a href="https://discord.gg/RcG33DQJdf">Discord</a>
24
24
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
25
- <a href="https://www.npmjs.com/package/zod">NPM</a>
25
+ <a href="https://www.npmjs.com/package/zod">npm</a>
26
26
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
27
27
  <a href="https://github.com/colinhacks/zod/issues/new">Issues</a>
28
28
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
@@ -47,7 +47,7 @@
47
47
  - [Sponsors](#sponsors)
48
48
  - [Ecosystem](#ecosystem)
49
49
  - [Installation](#installation)
50
- - [Node](#node)
50
+ - [Node/npm](#Node/npm)
51
51
  - [Deno](#deno)
52
52
  - [Basic usage](#basic-usage)
53
53
  - [Primitives](#primitives)
@@ -215,6 +215,26 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
215
215
  <b>Trip</b>
216
216
  </td>
217
217
  </tr>
218
+ <tr>
219
+ <td align="center">
220
+ <a href="https://seasoned.cc">
221
+ <img src="https://avatars.githubusercontent.com/u/33913103?s=200&v=4" width="150px;" alt="" />
222
+ </a>
223
+ <br />
224
+ <b>Seasoned Software</b>
225
+ <br />
226
+ <a href="https://seasoned.cc">seasoned.cc</a>
227
+ </td>
228
+ <td align="center">
229
+ <a href="https://seasoned.cc">
230
+ <img src="https://avatars.githubusercontent.com/u/67802063?s=200&v=4" width="150px;" alt="" />
231
+ </a>
232
+ <br />
233
+ <b>Interval</b>
234
+ <br />
235
+ <a href="https://interval.com">interval.com</a>
236
+ </td>
237
+ </tr>
218
238
  </table>
219
239
 
220
240
  #### Bronze
@@ -252,6 +272,18 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
252
272
  <a href="https://twitter.com/alexdotjs">@alexdotjs</a>
253
273
  </td>
254
274
  </tr>
275
+ <tr>
276
+ <td align="center">
277
+ <a href="https://adaptable.io/">
278
+ <img src="https://avatars.githubusercontent.com/u/60378268?s=200&v=4" width="100px;" alt=""/>
279
+ </a>
280
+ <br />
281
+ <b>Adaptable</b>
282
+ <br/>
283
+ <a href="https://adaptable.io/">adaptable.io</a>
284
+ <br />
285
+ </td>
286
+ </tr>
255
287
  </table>
256
288
 
257
289
  ### Ecosystem
@@ -281,6 +313,7 @@ There are a growing number of tools that are built atop or support Zod natively!
281
313
  - [`prisma-zod-generator`](https://github.com/omar-dulaimi/prisma-zod-generator): Emit Zod schemas from your Prisma schema.
282
314
  - [`prisma-trpc-generator`](https://github.com/omar-dulaimi/prisma-trpc-generator): Emit fully implemented tRPC routers and their validation schemas using Zod.
283
315
  - [`nestjs-graphql-zod`](https://github.com/incetarik/nestjs-graphql-zod): Generates NestJS GraphQL model classes from Zod schemas dynamically and provides GraphQL method decorators working with Zod schemas.
316
+ - [`remix-domains`](https://github.com/SeasonedSoftware/remix-domains/): Improves end-to-end type safety in [Remix](https://remix.run/) by leveraging Zod to parse the framework's inputs such as FormData, URLSearchParams, etc.
284
317
 
285
318
  #### Form integrations
286
319
 
@@ -306,7 +339,7 @@ There are a growing number of tools that are built atop or support Zod natively!
306
339
  }
307
340
  ```
308
341
 
309
- ### Node/NPM
342
+ ### Node/npm
310
343
 
311
344
  To install Zod v3:
312
345
 
@@ -318,7 +351,7 @@ pnpm add zod # pnpm
318
351
 
319
352
  ### Deno
320
353
 
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:
354
+ 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:
322
355
 
323
356
  ```ts
324
357
  import { z } from "https://deno.land/x/zod/mod.ts";
@@ -330,7 +363,7 @@ You can also specify a particular version:
330
363
  import { z } from from "https://deno.land/x/zod@v3.16.1/mod.ts"
331
364
  ```
332
365
 
333
- > The rest of this README assumes you are using NPM and importing directly from the `"zod"` package.
366
+ > The rest of this README assumes you are using npm and importing directly from the `"zod"` package.
334
367
 
335
368
  ## Basic usage
336
369
 
@@ -433,7 +466,7 @@ z.string().nonempty({ message: "Can't be empty" });
433
466
 
434
467
  > Check out [validator.js](https://github.com/validatorjs/validator.js) for a bunch of other useful string validation functions.
435
468
 
436
- You can customize some common errors messages when creating a string schema.
469
+ You can customize some common error messages when creating a string schema.
437
470
 
438
471
  ```ts
439
472
  const name = z.string({
@@ -547,7 +580,7 @@ const VALUES = ["Salmon", "Tuna", "Trout"] as const;
547
580
  const FishEnum = z.enum(VALUES);
548
581
  ```
549
582
 
550
- This is not allowed, since Zod isn't able to infer the exact values of each elements.
583
+ This is not allowed, since Zod isn't able to infer the exact values of each element.
551
584
 
552
585
  ```ts
553
586
  const fish = ["Salmon", "Tuna", "Trout"];
@@ -728,7 +761,7 @@ Dog.shape.age; // => number schema
728
761
 
729
762
  ### `.extend`
730
763
 
731
- You can add additional fields an object schema with the `.extend` method.
764
+ You can add additional fields to an object schema with the `.extend` method.
732
765
 
733
766
  ```ts
734
767
  const DogWithBreed = Dog.extend({
@@ -848,7 +881,7 @@ const deepPartialUser = user.deepPartial();
848
881
 
849
882
  ### `.passthrough`
850
883
 
851
- By default Zod objects schemas strip out unrecognized keys during parsing.
884
+ By default Zod object schemas strip out unrecognized keys during parsing.
852
885
 
853
886
  ```ts
854
887
  const person = z.object({
@@ -875,7 +908,7 @@ person.passthrough().parse({
875
908
 
876
909
  ### `.strict`
877
910
 
878
- By default Zod objects schemas strip out unrecognized keys during parsing. You can _disallow_ unknown keys with `.strict()` . If there are any unknown keys in the input, Zod will throw an error.
911
+ By default Zod object schemas strip out unrecognized keys during parsing. You can _disallow_ unknown keys with `.strict()` . If there are any unknown keys in the input, Zod will throw an error.
879
912
 
880
913
  ```ts
881
914
  const person = z
@@ -1035,7 +1068,7 @@ const item = z
1035
1068
 
1036
1069
  Record schemas are used to validate types such as `{ [k: string]: number }`.
1037
1070
 
1038
- If you want to validate the _values_ of an object against some schema but don't care about the keys, use `Record`.
1071
+ If you want to validate the _values_ of an object against some schema but don't care about the keys, use `z.record(valueType)`:
1039
1072
 
1040
1073
  ```ts
1041
1074
  const NumberCache = z.record(z.number());
@@ -1058,9 +1091,22 @@ userStore["77d2586b-9e8e-4ecf-8b21-ea7e0530eadd"] = {
1058
1091
  }; // TypeError
1059
1092
  ```
1060
1093
 
1094
+ ### Record key type
1095
+
1096
+ If you want to validate both the keys and the values, use
1097
+ `z.record(keyType, valueType)`:
1098
+
1099
+ ```ts
1100
+ const NoEmptyKeysSchema = z.record(z.string().min(1), z.number());
1101
+ NoEmptyKeysSchema.parse({ count: 1 }); // => { 'count': 1 }
1102
+ NoEmptyKeysSchema.parse({ "": 1 }); // fails
1103
+ ```
1104
+
1105
+ _(Notice how when passing two arguments, `valueType` is the second argument)_
1106
+
1061
1107
  **A note on numerical keys**
1062
1108
 
1063
- You may have expected `z.record()` to accept two arguments, one for the keys and one for the values. After all, TypeScript's built-in Record type does: `Record<KeyType, ValueType>` . Otherwise, how do you represent the TypeScript type `Record<number, any>` in Zod?
1109
+ While `z.record(keyType, valueType)` is able to accept numerical key types and TypeScript's built-in Record type is `Record<KeyType, ValueType>`, it's hard to represent the TypeScript type `Record<number, any>` in Zod.
1064
1110
 
1065
1111
  As it turns out, TypeScript's behavior surrounding `[k: number]` is a little unintuitive:
1066
1112
 
@@ -1310,7 +1356,7 @@ type myFunction = z.infer<typeof myFunction>;
1310
1356
  // => (arg0: string)=>number
1311
1357
  ``` -->
1312
1358
 
1313
- Function schemas have an `.implement()` method which accepts a function and returns a new function that automatically validates it's inputs and outputs.
1359
+ Function schemas have an `.implement()` method which accepts a function and returns a new function that automatically validates its inputs and outputs.
1314
1360
 
1315
1361
  ```ts
1316
1362
  const trimmedLength = z
@@ -1528,7 +1574,7 @@ const userId = z.string().refine(async (id) => {
1528
1574
  });
1529
1575
  ```
1530
1576
 
1531
- > ⚠️If you use async refinements, you must use the `.parseAsync` method to parse data! Otherwise Zod will throw an error.
1577
+ > ⚠️ If you use async refinements, you must use the `.parseAsync` method to parse data! Otherwise Zod will throw an error.
1532
1578
 
1533
1579
  #### Relationship to transforms
1534
1580
 
@@ -1584,7 +1630,7 @@ const Strings = z.array(z.string()).superRefine((val, ctx) => {
1584
1630
  if (val.length !== new Set(val).size) {
1585
1631
  ctx.addIssue({
1586
1632
  code: z.ZodIssueCode.custom,
1587
- message: `No duplicated allowed.`,
1633
+ message: `No duplicates allowed.`,
1588
1634
  });
1589
1635
  }
1590
1636
  });
@@ -1977,13 +2023,13 @@ Branded -->
1977
2023
  * Missing support for parsing cyclical data (maybe)
1978
2024
  * Missing error customization -->
1979
2025
 
1980
- **Joi**
2026
+ ### Joi
1981
2027
 
1982
2028
  [https://github.com/hapijs/joi](https://github.com/hapijs/joi)
1983
2029
 
1984
2030
  Doesn't support static type inference 😕
1985
2031
 
1986
- **Yup**
2032
+ ### Yup
1987
2033
 
1988
2034
  [https://github.com/jquense/yup](https://github.com/jquense/yup)
1989
2035
 
@@ -1999,7 +2045,7 @@ Yup is a full-featured library that was implemented first in vanilla JS, and lat
1999
2045
 
2000
2046
  <!-- ¹Yup has a strange interpretation of the word `required`. Instead of meaning "not undefined", Yup uses it to mean "not empty". So `yup.string().required()` will not accept an empty string, and `yup.array(yup.string()).required()` will not accept an empty array. Instead, Yup us Zod arrays there is a dedicated `.nonempty()` method to indicate this, or you can implement it with a custom refinement. -->
2001
2047
 
2002
- **io-ts**
2048
+ ### io-ts
2003
2049
 
2004
2050
  [https://github.com/gcanti/io-ts](https://github.com/gcanti/io-ts)
2005
2051
 
@@ -2050,7 +2096,7 @@ This more declarative API makes schema definitions vastly more concise.
2050
2096
  - Missing promise schemas
2051
2097
  - Missing function schemas
2052
2098
 
2053
- **Runtypes**
2099
+ ### Runtypes
2054
2100
 
2055
2101
  [https://github.com/pelotom/runtypes](https://github.com/pelotom/runtypes)
2056
2102
 
@@ -2063,7 +2109,7 @@ Good type inference support, but limited options for object type masking (no `.p
2063
2109
  - Missing promise schemas
2064
2110
  - Missing error customization
2065
2111
 
2066
- **Ow**
2112
+ ### Ow
2067
2113
 
2068
2114
  [https://github.com/sindresorhus/ow](https://github.com/sindresorhus/ow)
2069
2115
 
package/lib/index.mjs CHANGED
@@ -884,25 +884,23 @@ class ZodString extends ZodType {
884
884
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
885
885
  }
886
886
  get minLength() {
887
- let min = -Infinity;
888
- this._def.checks.map((ch) => {
887
+ let min = null;
888
+ for (const ch of this._def.checks) {
889
889
  if (ch.kind === "min") {
890
- if (min === null || ch.value > min) {
890
+ if (min === null || ch.value > min)
891
891
  min = ch.value;
892
- }
893
892
  }
894
- });
893
+ }
895
894
  return min;
896
895
  }
897
896
  get maxLength() {
898
897
  let max = null;
899
- this._def.checks.map((ch) => {
898
+ for (const ch of this._def.checks) {
900
899
  if (ch.kind === "max") {
901
- if (max === null || ch.value < max) {
900
+ if (max === null || ch.value < max)
902
901
  max = ch.value;
903
- }
904
902
  }
905
- });
903
+ }
906
904
  return max;
907
905
  }
908
906
  }
package/lib/index.umd.js CHANGED
@@ -890,25 +890,23 @@
890
890
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
891
891
  }
892
892
  get minLength() {
893
- let min = -Infinity;
894
- this._def.checks.map((ch) => {
893
+ let min = null;
894
+ for (const ch of this._def.checks) {
895
895
  if (ch.kind === "min") {
896
- if (min === null || ch.value > min) {
896
+ if (min === null || ch.value > min)
897
897
  min = ch.value;
898
- }
899
898
  }
900
- });
899
+ }
901
900
  return min;
902
901
  }
903
902
  get maxLength() {
904
903
  let max = null;
905
- this._def.checks.map((ch) => {
904
+ for (const ch of this._def.checks) {
906
905
  if (ch.kind === "max") {
907
- if (max === null || ch.value < max) {
906
+ if (max === null || ch.value < max)
908
907
  max = ch.value;
909
- }
910
908
  }
911
- });
909
+ }
912
910
  return max;
913
911
  }
914
912
  }
package/lib/types.d.ts CHANGED
@@ -136,8 +136,8 @@ export declare class ZodString extends ZodType<string, ZodStringDef> {
136
136
  get isURL(): boolean;
137
137
  get isUUID(): boolean;
138
138
  get isCUID(): boolean;
139
- get minLength(): number;
140
- get maxLength(): null;
139
+ get minLength(): number | null;
140
+ get maxLength(): number | null;
141
141
  static create: (params?: RawCreateParams) => ZodString;
142
142
  }
143
143
  declare type ZodNumberCheck = {
package/lib/types.js CHANGED
@@ -466,25 +466,23 @@ class ZodString extends ZodType {
466
466
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
467
467
  }
468
468
  get minLength() {
469
- let min = -Infinity;
470
- this._def.checks.map((ch) => {
469
+ let min = null;
470
+ for (const ch of this._def.checks) {
471
471
  if (ch.kind === "min") {
472
- if (min === null || ch.value > min) {
472
+ if (min === null || ch.value > min)
473
473
  min = ch.value;
474
- }
475
474
  }
476
- });
475
+ }
477
476
  return min;
478
477
  }
479
478
  get maxLength() {
480
479
  let max = null;
481
- this._def.checks.map((ch) => {
480
+ for (const ch of this._def.checks) {
482
481
  if (ch.kind === "max") {
483
- if (max === null || ch.value < max) {
482
+ if (max === null || ch.value < max)
484
483
  max = ch.value;
485
- }
486
484
  }
487
- });
485
+ }
488
486
  return max;
489
487
  }
490
488
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.17.3",
3
+ "version": "3.17.4",
4
4
  "description": "TypeScript-first schema declaration and validation library with static type inference",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./index.d.ts",
@@ -59,9 +59,9 @@
59
59
  "test": "jest --coverage",
60
60
  "test:deno": "cd deno && deno test",
61
61
  "prepublishOnly": "npm run test && npm run build && npm run build:deno",
62
- "play": "nodemon -e ts -w . -x ts-node src/playground.ts --project tsconfig.json --trace-warnings",
62
+ "play": "nodemon -e ts -w . -x esr src/playground.ts",
63
63
  "depcruise": "depcruise -c .dependency-cruiser.js src",
64
- "benchmark": "ts-node src/benchmarks/index.ts",
64
+ "benchmark": "esr src/benchmarks/index.ts",
65
65
  "prepare": "husky install"
66
66
  },
67
67
  "devDependencies": {
@@ -73,6 +73,8 @@
73
73
  "@typescript-eslint/parser": "^5.15.0",
74
74
  "benchmark": "^2.1.4",
75
75
  "dependency-cruiser": "^9.19.0",
76
+ "esbuild": "^0.14.49",
77
+ "esbuild-runner": "^2.2.1",
76
78
  "eslint": "^8.11.0",
77
79
  "eslint-config-prettier": "^8.5.0",
78
80
  "eslint-plugin-ban": "^1.6.0",
@@ -88,7 +90,6 @@
88
90
  "rollup": "^2.70.1",
89
91
  "ts-jest": "^27.1.3",
90
92
  "ts-morph": "^14.0.0",
91
- "ts-node": "^10.7.0",
92
93
  "tslib": "^2.3.1",
93
94
  "typescript": "4.1"
94
95
  },