zod 3.21.2 → 3.21.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
@@ -2394,6 +2394,58 @@ z.string()
2394
2394
 
2395
2395
  The `.pipe()` method returns a `ZodPipeline` instance.
2396
2396
 
2397
+ #### You can use `.pipe()` to fix common issues with `z.coerce`.
2398
+
2399
+ You can constrain the input to types that work well with your chosen coercion. Then use `.pipe()` to apply the coercion.
2400
+
2401
+ without constrained input:
2402
+ ```ts
2403
+ const toDate = z.coerce.date()
2404
+
2405
+ // works intuitively
2406
+ console.log(toDate.safeParse('2023-01-01').success) // true
2407
+
2408
+ // might not be what you want
2409
+ console.log(toDate.safeParse(null).success) // true
2410
+ ```
2411
+
2412
+ with constrained input:
2413
+ ```ts
2414
+ const datelike = z.union([z.number(), z.string(), z.date()])
2415
+ const datelikeToDate = datelike.pipe(z.coerce.date())
2416
+
2417
+ // still works intuitively
2418
+ console.log(datelikeToDate.safeParse('2023-01-01').success) // true
2419
+
2420
+ // more likely what you want
2421
+ console.log(datelikeToDate.safeParse(null).success) // false
2422
+ ```
2423
+
2424
+ You can also use this technique to avoid coercions that throw uncaught errors.
2425
+
2426
+ without constrained input:
2427
+ ```ts
2428
+ const toBigInt = z.coerce.bigint()
2429
+
2430
+ // works intuitively
2431
+ console.log( toBigInt.safeParse( '42' ) ) // true
2432
+
2433
+ // probably not what you want
2434
+ console.log( toBigInt.safeParse( null ) ) // throws uncaught error
2435
+ ```
2436
+
2437
+ with constrained input:
2438
+ ```ts
2439
+ const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
2440
+ const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
2441
+
2442
+ // still works intuitively
2443
+ console.log( toBigInt.safeParse( '42' ).success ) // true
2444
+
2445
+ // error handled by zod, more likely what you want
2446
+ console.log( toBigInt.safeParse( null ).success ) // false
2447
+ ```
2448
+
2397
2449
  ## Guides and concepts
2398
2450
 
2399
2451
  ### Type inference
@@ -27,7 +27,7 @@ export declare namespace objectUtil {
27
27
  type requiredKeys<T extends object> = {
28
28
  [k in keyof T]: undefined extends T[k] ? never : k;
29
29
  }[keyof T];
30
- export type addQuestionMarks<T extends object, R extends keyof T = requiredKeys<T>> = Pick<T, R> & Partial<T>;
30
+ export type addQuestionMarks<T extends object, R extends keyof T = requiredKeys<T>> = Pick<Required<T>, R> & Partial<T>;
31
31
  export type identity<T> = T;
32
32
  export type flatten<T> = identity<{
33
33
  [k in keyof T]: T[k];
package/lib/index.mjs CHANGED
@@ -3463,10 +3463,6 @@ class ZodEffects extends ZodType {
3463
3463
  path: ctx.path,
3464
3464
  parent: ctx,
3465
3465
  });
3466
- // if (base.status === "aborted") return INVALID;
3467
- // if (base.status === "dirty") {
3468
- // return { status: "dirty", value: base.value };
3469
- // }
3470
3466
  if (!isValid(base))
3471
3467
  return base;
3472
3468
  const result = effect.transform(base.value, checkCtx);
@@ -3481,10 +3477,6 @@ class ZodEffects extends ZodType {
3481
3477
  .then((base) => {
3482
3478
  if (!isValid(base))
3483
3479
  return base;
3484
- // if (base.status === "aborted") return INVALID;
3485
- // if (base.status === "dirty") {
3486
- // return { status: "dirty", value: base.value };
3487
- // }
3488
3480
  return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
3489
3481
  });
3490
3482
  }
@@ -3727,13 +3719,26 @@ class ZodPipeline extends ZodType {
3727
3719
  }
3728
3720
  }
3729
3721
  const custom = (check, params = {},
3730
- /* @deprecated */
3722
+ /*
3723
+ * @deprecated
3724
+ *
3725
+ * Pass `fatal` into the params object instead:
3726
+ *
3727
+ * ```ts
3728
+ * z.string().custom((val) => val.length > 5, { fatal: false })
3729
+ * ```
3730
+ *
3731
+ */
3731
3732
  fatal) => {
3732
3733
  if (check)
3733
3734
  return ZodAny.create().superRefine((data, ctx) => {
3734
3735
  var _a, _b;
3735
3736
  if (!check(data)) {
3736
- const p = typeof params === "function" ? params(data) : params;
3737
+ const p = typeof params === "function"
3738
+ ? params(data)
3739
+ : typeof params === "string"
3740
+ ? { message: params }
3741
+ : params;
3737
3742
  const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
3738
3743
  const p2 = typeof p === "string" ? { message: p } : p;
3739
3744
  ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
package/lib/index.umd.js CHANGED
@@ -3469,10 +3469,6 @@
3469
3469
  path: ctx.path,
3470
3470
  parent: ctx,
3471
3471
  });
3472
- // if (base.status === "aborted") return INVALID;
3473
- // if (base.status === "dirty") {
3474
- // return { status: "dirty", value: base.value };
3475
- // }
3476
3472
  if (!isValid(base))
3477
3473
  return base;
3478
3474
  const result = effect.transform(base.value, checkCtx);
@@ -3487,10 +3483,6 @@
3487
3483
  .then((base) => {
3488
3484
  if (!isValid(base))
3489
3485
  return base;
3490
- // if (base.status === "aborted") return INVALID;
3491
- // if (base.status === "dirty") {
3492
- // return { status: "dirty", value: base.value };
3493
- // }
3494
3486
  return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
3495
3487
  });
3496
3488
  }
@@ -3733,13 +3725,26 @@
3733
3725
  }
3734
3726
  }
3735
3727
  const custom = (check, params = {},
3736
- /* @deprecated */
3728
+ /*
3729
+ * @deprecated
3730
+ *
3731
+ * Pass `fatal` into the params object instead:
3732
+ *
3733
+ * ```ts
3734
+ * z.string().custom((val) => val.length > 5, { fatal: false })
3735
+ * ```
3736
+ *
3737
+ */
3737
3738
  fatal) => {
3738
3739
  if (check)
3739
3740
  return ZodAny.create().superRefine((data, ctx) => {
3740
3741
  var _a, _b;
3741
3742
  if (!check(data)) {
3742
- const p = typeof params === "function" ? params(data) : params;
3743
+ const p = typeof params === "function"
3744
+ ? params(data)
3745
+ : typeof params === "string"
3746
+ ? { message: params }
3747
+ : params;
3743
3748
  const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
3744
3749
  const p2 = typeof p === "string" ? { message: p } : p;
3745
3750
  ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
package/lib/types.d.ts CHANGED
@@ -477,9 +477,6 @@ export declare type PassthroughType<T extends UnknownKeysParam> = T extends "pas
477
477
  } : unknown;
478
478
  export declare type deoptional<T extends ZodTypeAny> = T extends ZodOptional<infer U> ? deoptional<U> : T extends ZodNullable<infer U> ? ZodNullable<deoptional<U>> : T;
479
479
  export declare type SomeZodObject = ZodObject<ZodRawShape, UnknownKeysParam, ZodTypeAny>;
480
- export declare type objectKeyMask<Obj> = {
481
- [k in keyof Obj]?: true;
482
- };
483
480
  export declare type noUnrecognized<Obj extends object, Shape extends object> = {
484
481
  [k in keyof Obj]: k extends keyof Shape ? Obj[k] : never;
485
482
  };
@@ -502,19 +499,27 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
502
499
  [k in Key]: Schema;
503
500
  }, UnknownKeys, Catchall>;
504
501
  catchall<Index extends ZodTypeAny>(index: Index): ZodObject<T, UnknownKeys, Index>;
505
- pick<Mask extends objectKeyMask<T>>(mask: noUnrecognized<Mask, T>): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall>;
506
- omit<Mask extends objectKeyMask<T>>(mask: noUnrecognized<Mask, objectKeyMask<T>>): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall>;
502
+ pick<Mask extends {
503
+ [k in keyof T]?: true;
504
+ }>(mask: Mask): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall>;
505
+ omit<Mask extends {
506
+ [k in keyof T]?: true;
507
+ }>(mask: Mask): ZodObject<Omit<T, keyof Mask>, UnknownKeys, Catchall>;
507
508
  deepPartial(): partialUtil.DeepPartial<this>;
508
509
  partial(): ZodObject<{
509
510
  [k in keyof T]: ZodOptional<T[k]>;
510
511
  }, UnknownKeys, Catchall>;
511
- partial<Mask extends objectKeyMask<T>>(mask: noUnrecognized<Mask, objectKeyMask<T>>): ZodObject<objectUtil.noNever<{
512
+ partial<Mask extends {
513
+ [k in keyof T]?: true;
514
+ }>(mask: Mask): ZodObject<objectUtil.noNever<{
512
515
  [k in keyof T]: k extends keyof Mask ? ZodOptional<T[k]> : T[k];
513
516
  }>, UnknownKeys, Catchall>;
514
517
  required(): ZodObject<{
515
518
  [k in keyof T]: deoptional<T[k]>;
516
519
  }, UnknownKeys, Catchall>;
517
- required<Mask extends objectKeyMask<T>>(mask: noUnrecognized<Mask, objectKeyMask<T>>): ZodObject<objectUtil.noNever<{
520
+ required<Mask extends {
521
+ [k in keyof T]?: true;
522
+ }>(mask: Mask): ZodObject<objectUtil.noNever<{
518
523
  [k in keyof T]: k extends keyof Mask ? deoptional<T[k]> : T[k];
519
524
  }>, UnknownKeys, Catchall>;
520
525
  keyof(): ZodEnum<enumUtil.UnionToTupleString<keyof T>>;
@@ -842,7 +847,7 @@ export declare class ZodPipeline<A extends ZodTypeAny, B extends ZodTypeAny> ext
842
847
  declare type CustomParams = CustomErrorParams & {
843
848
  fatal?: boolean;
844
849
  };
845
- export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: CustomParams | ((input: any) => CustomParams), fatal?: boolean | undefined) => ZodType<T, ZodTypeDef, T>;
850
+ export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: string | CustomParams | ((input: any) => CustomParams), fatal?: boolean | undefined) => ZodType<T, ZodTypeDef, T>;
846
851
  export { ZodType as Schema, ZodType as ZodSchema };
847
852
  export declare const late: {
848
853
  object: <T extends ZodRawShape>(shape: () => T, params?: RawCreateParams) => ZodObject<T, "strip", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<baseObjectOutputType<T>, { [k in keyof baseObjectOutputType<T>]: undefined extends baseObjectOutputType<T>[k] ? never : k; }[keyof T]>]: objectUtil.addQuestionMarks<baseObjectOutputType<T>, { [k in keyof baseObjectOutputType<T>]: undefined extends baseObjectOutputType<T>[k] ? never : k; }[keyof T]>[k_1]; }, { [k_2 in keyof baseObjectInputType<T>]: baseObjectInputType<T>[k_2]; }>;
package/lib/types.js CHANGED
@@ -3103,7 +3103,11 @@ const custom = (check, params = {}, fatal) => {
3103
3103
  return ZodAny.create().superRefine((data, ctx) => {
3104
3104
  var _a, _b;
3105
3105
  if (!check(data)) {
3106
- const p = typeof params === "function" ? params(data) : params;
3106
+ const p = typeof params === "function"
3107
+ ? params(data)
3108
+ : typeof params === "string"
3109
+ ? { message: params }
3110
+ : params;
3107
3111
  const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
3108
3112
  const p2 = typeof p === "string" ? { message: p } : p;
3109
3113
  ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.21.2",
3
+ "version": "3.21.4",
4
4
  "author": "Colin McDonnell <colin@colinhacks.com>",
5
5
  "repository": {
6
6
  "type": "git",