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/index.umd.js CHANGED
@@ -58,6 +58,70 @@
58
58
  }
59
59
  util.joinValues = joinValues;
60
60
  })(util || (util = {}));
61
+ const ZodParsedType = util.arrayToEnum([
62
+ "string",
63
+ "nan",
64
+ "number",
65
+ "integer",
66
+ "float",
67
+ "boolean",
68
+ "date",
69
+ "bigint",
70
+ "symbol",
71
+ "function",
72
+ "undefined",
73
+ "null",
74
+ "array",
75
+ "object",
76
+ "unknown",
77
+ "promise",
78
+ "void",
79
+ "never",
80
+ "map",
81
+ "set",
82
+ ]);
83
+ const getParsedType = (data) => {
84
+ const t = typeof data;
85
+ switch (t) {
86
+ case "undefined":
87
+ return ZodParsedType.undefined;
88
+ case "string":
89
+ return ZodParsedType.string;
90
+ case "number":
91
+ return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
92
+ case "boolean":
93
+ return ZodParsedType.boolean;
94
+ case "function":
95
+ return ZodParsedType.function;
96
+ case "bigint":
97
+ return ZodParsedType.bigint;
98
+ case "object":
99
+ if (Array.isArray(data)) {
100
+ return ZodParsedType.array;
101
+ }
102
+ if (data === null) {
103
+ return ZodParsedType.null;
104
+ }
105
+ if (data.then &&
106
+ typeof data.then === "function" &&
107
+ data.catch &&
108
+ typeof data.catch === "function") {
109
+ return ZodParsedType.promise;
110
+ }
111
+ if (typeof Map !== "undefined" && data instanceof Map) {
112
+ return ZodParsedType.map;
113
+ }
114
+ if (typeof Set !== "undefined" && data instanceof Set) {
115
+ return ZodParsedType.set;
116
+ }
117
+ if (typeof Date !== "undefined" && data instanceof Date) {
118
+ return ZodParsedType.date;
119
+ }
120
+ return ZodParsedType.object;
121
+ default:
122
+ return ZodParsedType.unknown;
123
+ }
124
+ };
61
125
 
62
126
  const ZodIssueCode = util.arrayToEnum([
63
127
  "invalid_type",
@@ -265,70 +329,6 @@
265
329
  exports.overrideErrorMap = map;
266
330
  };
267
331
 
268
- const ZodParsedType = util.arrayToEnum([
269
- "string",
270
- "nan",
271
- "number",
272
- "integer",
273
- "float",
274
- "boolean",
275
- "date",
276
- "bigint",
277
- "symbol",
278
- "function",
279
- "undefined",
280
- "null",
281
- "array",
282
- "object",
283
- "unknown",
284
- "promise",
285
- "void",
286
- "never",
287
- "map",
288
- "set",
289
- ]);
290
- const getParsedType = (data) => {
291
- const t = typeof data;
292
- switch (t) {
293
- case "undefined":
294
- return ZodParsedType.undefined;
295
- case "string":
296
- return ZodParsedType.string;
297
- case "number":
298
- return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
299
- case "boolean":
300
- return ZodParsedType.boolean;
301
- case "function":
302
- return ZodParsedType.function;
303
- case "bigint":
304
- return ZodParsedType.bigint;
305
- case "object":
306
- if (Array.isArray(data)) {
307
- return ZodParsedType.array;
308
- }
309
- if (data === null) {
310
- return ZodParsedType.null;
311
- }
312
- if (data.then &&
313
- typeof data.then === "function" &&
314
- data.catch &&
315
- typeof data.catch === "function") {
316
- return ZodParsedType.promise;
317
- }
318
- if (typeof Map !== "undefined" && data instanceof Map) {
319
- return ZodParsedType.map;
320
- }
321
- if (typeof Set !== "undefined" && data instanceof Set) {
322
- return ZodParsedType.set;
323
- }
324
- if (typeof Date !== "undefined" && data instanceof Date) {
325
- return ZodParsedType.date;
326
- }
327
- return ZodParsedType.object;
328
- default:
329
- return ZodParsedType.unknown;
330
- }
331
- };
332
332
  const makeIssue = (params) => {
333
333
  const { data, path, errorMaps, issueData } = params;
334
334
  const fullPath = [...path, ...(issueData.path || [])];
@@ -715,10 +715,14 @@
715
715
  ...errorUtil.errToObj(message),
716
716
  });
717
717
  /**
718
- * Deprecated.
719
- * Use z.string().min(1) instead.
718
+ * @deprecated Use z.string().min(1) instead.
719
+ * @see {@link ZodString.min}
720
720
  */
721
721
  this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
722
+ this.trim = () => new ZodString({
723
+ ...this._def,
724
+ checks: [...this._def.checks, { kind: "trim" }],
725
+ });
722
726
  }
723
727
  _parse(input) {
724
728
  const parsedType = this._getType(input);
@@ -822,6 +826,12 @@
822
826
  status.dirty();
823
827
  }
824
828
  }
829
+ else if (check.kind === "trim") {
830
+ input.data = input.data.trim();
831
+ }
832
+ else {
833
+ util.assertNever(check);
834
+ }
825
835
  }
826
836
  return { status: status.value, value: input.data };
827
837
  }
@@ -1599,7 +1609,9 @@
1599
1609
  pick(mask) {
1600
1610
  const shape = {};
1601
1611
  util.objectKeys(mask).map((key) => {
1602
- shape[key] = this.shape[key];
1612
+ // only add to shape if key corresponds to an element of the current shape
1613
+ if (this.shape[key])
1614
+ shape[key] = this.shape[key];
1603
1615
  });
1604
1616
  return new ZodObject({
1605
1617
  ...this._def,
@@ -2388,10 +2400,11 @@
2388
2400
  ...processCreateParams(params),
2389
2401
  });
2390
2402
  };
2391
- function createZodEnum(values) {
2403
+ function createZodEnum(values, params) {
2392
2404
  return new ZodEnum({
2393
2405
  values: values,
2394
2406
  typeName: exports.ZodFirstPartyTypeKind.ZodEnum,
2407
+ ...processCreateParams(params),
2395
2408
  });
2396
2409
  }
2397
2410
  class ZodEnum extends ZodType {
@@ -2727,9 +2740,15 @@
2727
2740
  ...processCreateParams(params),
2728
2741
  });
2729
2742
  };
2730
- const custom = (check, params) => {
2743
+ const custom = (check, params = {}, fatal) => {
2731
2744
  if (check)
2732
- return ZodAny.create().refine(check, params);
2745
+ return ZodAny.create().superRefine((data, ctx) => {
2746
+ if (!check(data)) {
2747
+ const p = typeof params === "function" ? params(data) : params;
2748
+ const p2 = typeof p === "string" ? { message: p } : p;
2749
+ ctx.addIssue({ code: "custom", ...p2, fatal });
2750
+ }
2751
+ });
2733
2752
  return ZodAny.create();
2734
2753
  };
2735
2754
  const late = {
@@ -2771,7 +2790,7 @@
2771
2790
  })(exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
2772
2791
  const instanceOfType = (cls, params = {
2773
2792
  message: `Input not instance of ${cls.name}`,
2774
- }) => custom((data) => data instanceof cls, params);
2793
+ }) => custom((data) => data instanceof cls, params, true);
2775
2794
  const stringType = ZodString.create;
2776
2795
  const numberType = ZodNumber.create;
2777
2796
  const nanType = ZodNaN.create;
@@ -2810,8 +2829,8 @@
2810
2829
 
2811
2830
  var mod = /*#__PURE__*/Object.freeze({
2812
2831
  __proto__: null,
2813
- ZodParsedType: ZodParsedType,
2814
2832
  getParsedType: getParsedType,
2833
+ ZodParsedType: ZodParsedType,
2815
2834
  makeIssue: makeIssue,
2816
2835
  EMPTY_PATH: EMPTY_PATH,
2817
2836
  addIssueToContext: addIssueToContext,
package/lib/types.d.ts CHANGED
@@ -102,6 +102,9 @@ declare type ZodStringCheck = {
102
102
  kind: "regex";
103
103
  regex: RegExp;
104
104
  message?: string;
105
+ } | {
106
+ kind: "trim";
107
+ message?: string;
105
108
  };
106
109
  export interface ZodStringDef extends ZodTypeDef {
107
110
  checks: ZodStringCheck[];
@@ -120,10 +123,11 @@ export declare class ZodString extends ZodType<string, ZodStringDef> {
120
123
  max(maxLength: number, message?: errorUtil.ErrMessage): ZodString;
121
124
  length(len: number, message?: errorUtil.ErrMessage): ZodString;
122
125
  /**
123
- * Deprecated.
124
- * Use z.string().min(1) instead.
126
+ * @deprecated Use z.string().min(1) instead.
127
+ * @see {@link ZodString.min}
125
128
  */
126
129
  nonempty: (message?: errorUtil.ErrMessage | undefined) => ZodString;
130
+ trim: () => ZodString;
127
131
  get isEmail(): boolean;
128
132
  get isURL(): boolean;
129
133
  get isUUID(): boolean;
@@ -530,8 +534,8 @@ export interface ZodEnumDef<T extends EnumValues = EnumValues> extends ZodTypeDe
530
534
  declare type Writeable<T> = {
531
535
  -readonly [P in keyof T]: T[P];
532
536
  };
533
- declare function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]>>(values: T): ZodEnum<Writeable<T>>;
534
- declare function createZodEnum<U extends string, T extends [U, ...U[]]>(values: T): ZodEnum<T>;
537
+ declare function createZodEnum<U extends string, T extends Readonly<[U, ...U[]]>>(values: T, params?: RawCreateParams): ZodEnum<Writeable<T>>;
538
+ declare function createZodEnum<U extends string, T extends [U, ...U[]]>(values: T, params?: RawCreateParams): ZodEnum<T>;
535
539
  export declare class ZodEnum<T extends [string, ...string[]]> extends ZodType<T[number], ZodEnumDef<T>> {
536
540
  _parse(input: ParseInput): ParseReturnType<this["_output"]>;
537
541
  get options(): T;
@@ -625,7 +629,7 @@ export declare class ZodNaN extends ZodType<number, ZodNaNDef> {
625
629
  _parse(input: ParseInput): ParseReturnType<any>;
626
630
  static create: (params?: RawCreateParams) => ZodNaN;
627
631
  }
628
- export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: Parameters<ZodTypeAny["refine"]>[1]) => ZodType<T, ZodTypeDef, T>;
632
+ export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: Parameters<ZodTypeAny["refine"]>[1], fatal?: boolean | undefined) => ZodType<T, ZodTypeDef, T>;
629
633
  export { ZodType as Schema, ZodType as ZodSchema };
630
634
  export declare const late: {
631
635
  object: <T extends ZodRawShape>(shape: () => T, params?: RawCreateParams) => ZodObject<T, "strip", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T]: T[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T]: T[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T]: T[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T]: T[k_2]["_input"]; }>[k_3]; }>;