zod 3.20.0 → 3.20.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/README.md CHANGED
@@ -89,9 +89,9 @@
89
89
  - [Cyclical data](#cyclical-objects)
90
90
  - [Promises](#promises)
91
91
  - [Instanceof](#instanceof)
92
- - [Functions](#function-schemas)
92
+ - [Functions](#functions)
93
93
  - [Preprocess](#preprocess)
94
- - [Custom](#custom)
94
+ - [Custom](#custom-schemas)
95
95
  - [Schema methods](#schema-methods)
96
96
  - [.parse](#parse)
97
97
  - [.parseAsync](#parseasync)
@@ -591,6 +591,22 @@ z.coerce.bigint(); // BigInt(input)
591
591
  z.coerce.date(); // new Date(input)
592
592
  ```
593
593
 
594
+ **Boolean coercion**
595
+
596
+ Zod's boolean coercion is very simple! It passes the value into the `Boolean(value)` function, that's it. Any truthy value will resolve to `true`, any falsy value will resolve to `false`.
597
+
598
+ ```ts
599
+ z.coerce.boolean().parse("tuna"); // => true
600
+ z.coerce.boolean().parse("true"); // => true
601
+ z.coerce.boolean().parse("false"); // => true
602
+ z.coerce.boolean().parse(1); // => true
603
+ z.coerce.boolean().parse([]); // => true
604
+
605
+ z.coerce.boolean().parse(0); // => false
606
+ z.coerce.boolean().parse(undefined); // => false
607
+ z.coerce.boolean().parse(null); // => false
608
+ ```
609
+
594
610
  ### Datetime validation
595
611
 
596
612
  The `z.string().datetime()` method defaults to UTC validation: no timezone offsets with arbitrary sub-second decimal precision.
@@ -1534,7 +1550,7 @@ TestSchema.parse(new Test()); // passes
1534
1550
  TestSchema.parse("blob"); // throws
1535
1551
  ```
1536
1552
 
1537
- ## Function schemas
1553
+ ## Functions
1538
1554
 
1539
1555
  Zod also lets you define "function schemas". This makes it easy to validate the inputs and outputs of a function without intermixing your validation code and "business logic".
1540
1556
 
@@ -1980,7 +1996,8 @@ const Strings = z.string().transform((val, ctx) => {
1980
1996
  Transforms and refinements can be interleaved. These will be executed in the order they are declared.
1981
1997
 
1982
1998
  ```ts
1983
- const nameToGreeting = z.string()
1999
+ const nameToGreeting = z
2000
+ .string()
1984
2001
  .transform((val) => val.toUpperCase())
1985
2002
  .refine((val) => val.length > 15)
1986
2003
  .transform((val) => `Hello ${val}`)
@@ -2124,7 +2141,9 @@ z.union([z.string(), z.number()]);
2124
2141
  A convenience method for creating intersection types.
2125
2142
 
2126
2143
  ```ts
2127
- const nameAndAge = z.object({ name: z.string() }).and(z.object({ age: z.number() })); // { name: string } & { age: number }
2144
+ const nameAndAge = z
2145
+ .object({ name: z.string() })
2146
+ .and(z.object({ age: z.number() })); // { name: string } & { age: number }
2128
2147
 
2129
2148
  // equivalent to
2130
2149
  z.intersection(z.object({ name: z.string() }), z.object({ age: z.number() }));
package/lib/index.mjs CHANGED
@@ -774,7 +774,7 @@ const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-
774
774
  // old version: too slow, didn't support unicode
775
775
  // 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;
776
776
  // eslint-disable-next-line
777
- const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})(?<!-)$/i;
777
+ const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
778
778
  // interface IsDateStringOptions extends StringDateOptions {
779
779
  /**
780
780
  * Match any configuration
@@ -1081,7 +1081,7 @@ class ZodString extends ZodType {
1081
1081
  ...errorUtil.errToObj(message),
1082
1082
  });
1083
1083
  }
1084
- isDatetime() {
1084
+ get isDatetime() {
1085
1085
  return !!this._def.checks.find((ch) => ch.kind === "datetime");
1086
1086
  }
1087
1087
  get isEmail() {
@@ -3166,18 +3166,16 @@ class ZodCatch extends ZodType {
3166
3166
  });
3167
3167
  if (isAsync(result)) {
3168
3168
  return result.then((result) => {
3169
- const defaultValue = this._def.defaultValue();
3170
3169
  return {
3171
3170
  status: "valid",
3172
- value: result.status === "valid" ? result.value : defaultValue,
3171
+ value: result.status === "valid" ? result.value : this._def.defaultValue(),
3173
3172
  };
3174
3173
  });
3175
3174
  }
3176
3175
  else {
3177
- const defaultValue = this._def.defaultValue();
3178
3176
  return {
3179
3177
  status: "valid",
3180
- value: result.status === "valid" ? result.value : defaultValue,
3178
+ value: result.status === "valid" ? result.value : this._def.defaultValue(),
3181
3179
  };
3182
3180
  }
3183
3181
  }
package/lib/index.umd.js CHANGED
@@ -780,7 +780,7 @@
780
780
  // old version: too slow, didn't support unicode
781
781
  // 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;
782
782
  // eslint-disable-next-line
783
- const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})(?<!-)$/i;
783
+ const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
784
784
  // interface IsDateStringOptions extends StringDateOptions {
785
785
  /**
786
786
  * Match any configuration
@@ -1087,7 +1087,7 @@
1087
1087
  ...errorUtil.errToObj(message),
1088
1088
  });
1089
1089
  }
1090
- isDatetime() {
1090
+ get isDatetime() {
1091
1091
  return !!this._def.checks.find((ch) => ch.kind === "datetime");
1092
1092
  }
1093
1093
  get isEmail() {
@@ -3172,18 +3172,16 @@
3172
3172
  });
3173
3173
  if (isAsync(result)) {
3174
3174
  return result.then((result) => {
3175
- const defaultValue = this._def.defaultValue();
3176
3175
  return {
3177
3176
  status: "valid",
3178
- value: result.status === "valid" ? result.value : defaultValue,
3177
+ value: result.status === "valid" ? result.value : this._def.defaultValue(),
3179
3178
  };
3180
3179
  });
3181
3180
  }
3182
3181
  else {
3183
- const defaultValue = this._def.defaultValue();
3184
3182
  return {
3185
3183
  status: "valid",
3186
- value: result.status === "valid" ? result.value : defaultValue,
3184
+ value: result.status === "valid" ? result.value : this._def.defaultValue(),
3187
3185
  };
3188
3186
  }
3189
3187
  }
package/lib/types.d.ts CHANGED
@@ -163,7 +163,7 @@ export declare class ZodString extends ZodType<string, ZodStringDef> {
163
163
  */
164
164
  nonempty: (message?: errorUtil.ErrMessage | undefined) => ZodString;
165
165
  trim: () => ZodString;
166
- isDatetime(): boolean;
166
+ get isDatetime(): boolean;
167
167
  get isEmail(): boolean;
168
168
  get isURL(): boolean;
169
169
  get isUUID(): boolean;
@@ -495,7 +495,7 @@ export declare class ZodUnion<T extends ZodUnionOptions> extends ZodType<T[numbe
495
495
  export declare type ZodDiscriminatedUnionOption<Discriminator extends string> = ZodObject<{
496
496
  [key in Discriminator]: ZodTypeAny;
497
497
  } & ZodRawShape, any, any>;
498
- export interface ZodDiscriminatedUnionDef<Discriminator extends string, Options extends ZodDiscriminatedUnionOption<any>[]> extends ZodTypeDef {
498
+ export interface ZodDiscriminatedUnionDef<Discriminator extends string, Options extends ZodDiscriminatedUnionOption<any>[] = ZodDiscriminatedUnionOption<any>[]> extends ZodTypeDef {
499
499
  discriminator: Discriminator;
500
500
  options: Options;
501
501
  optionsMap: Map<Primitive, ZodDiscriminatedUnionOption<any>>;
package/lib/types.js CHANGED
@@ -304,7 +304,7 @@ const uuidRegex = /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-
304
304
  // old version: too slow, didn't support unicode
305
305
  // 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;
306
306
  // eslint-disable-next-line
307
- const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})(?<!-)$/i;
307
+ const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
308
308
  // interface IsDateStringOptions extends StringDateOptions {
309
309
  /**
310
310
  * Match any configuration
@@ -611,7 +611,7 @@ class ZodString extends ZodType {
611
611
  ...errorUtil_1.errorUtil.errToObj(message),
612
612
  });
613
613
  }
614
- isDatetime() {
614
+ get isDatetime() {
615
615
  return !!this._def.checks.find((ch) => ch.kind === "datetime");
616
616
  }
617
617
  get isEmail() {
@@ -2729,18 +2729,16 @@ class ZodCatch extends ZodType {
2729
2729
  });
2730
2730
  if ((0, parseUtil_1.isAsync)(result)) {
2731
2731
  return result.then((result) => {
2732
- const defaultValue = this._def.defaultValue();
2733
2732
  return {
2734
2733
  status: "valid",
2735
- value: result.status === "valid" ? result.value : defaultValue,
2734
+ value: result.status === "valid" ? result.value : this._def.defaultValue(),
2736
2735
  };
2737
2736
  });
2738
2737
  }
2739
2738
  else {
2740
- const defaultValue = this._def.defaultValue();
2741
2739
  return {
2742
2740
  status: "valid",
2743
- value: result.status === "valid" ? result.value : defaultValue,
2741
+ value: result.status === "valid" ? result.value : this._def.defaultValue(),
2744
2742
  };
2745
2743
  }
2746
2744
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.20.0",
3
+ "version": "3.20.2",
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",