zod 3.15.0 → 3.16.1

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/ZodError.d.ts CHANGED
@@ -54,6 +54,7 @@ export interface ZodInvalidUnionDiscriminatorIssue extends ZodIssueBase {
54
54
  options: Primitive[];
55
55
  }
56
56
  export interface ZodInvalidEnumValueIssue extends ZodIssueBase {
57
+ received: string | number;
57
58
  code: typeof ZodIssueCode.invalid_enum_value;
58
59
  options: (string | number)[];
59
60
  }
@@ -110,7 +111,9 @@ export declare type ZodFormattedError<T, U = string> = {
110
111
  _errors: U[];
111
112
  } & (T extends [any, ...any[]] ? {
112
113
  [K in keyof T]?: ZodFormattedError<T[K]>;
113
- } : T extends any[] ? ZodFormattedError<T[number]>[] : T extends object ? {
114
+ } : T extends any[] ? {
115
+ [k: number]: ZodFormattedError<T[number]>;
116
+ } : T extends object ? {
114
117
  [K in keyof T]?: ZodFormattedError<T[K]>;
115
118
  } : unknown);
116
119
  export declare type inferFormattedError<T extends ZodType<any, any, any>, U = string> = ZodFormattedError<TypeOf<T>, U>;
package/lib/ZodError.js CHANGED
@@ -1,6 +1,7 @@
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");
4
5
  const util_1 = require("./helpers/util");
5
6
  exports.ZodIssueCode = util_1.util.arrayToEnum([
6
7
  "invalid_type",
@@ -75,14 +76,14 @@ class ZodError extends Error {
75
76
  const el = issue.path[i];
76
77
  const terminal = i === issue.path.length - 1;
77
78
  if (!terminal) {
78
- if (typeof el === "string") {
79
- curr[el] = curr[el] || { _errors: [] };
80
- }
81
- else if (typeof el === "number") {
82
- const errorArray = [];
83
- errorArray._errors = [];
84
- curr[el] = curr[el] || errorArray;
85
- }
79
+ curr[el] = curr[el] || { _errors: [] };
80
+ // if (typeof el === "string") {
81
+ // curr[el] = curr[el] || { _errors: [] };
82
+ // } else if (typeof el === "number") {
83
+ // const errorArray: any = [];
84
+ // errorArray._errors = [];
85
+ // curr[el] = curr[el] || errorArray;
86
+ // }
86
87
  }
87
88
  else {
88
89
  curr[el] = curr[el] || { _errors: [] };
@@ -133,7 +134,7 @@ const defaultErrorMap = (issue, _ctx) => {
133
134
  let message;
134
135
  switch (issue.code) {
135
136
  case exports.ZodIssueCode.invalid_type:
136
- if (issue.received === "undefined") {
137
+ if (issue.received === parseUtil_1.ZodParsedType.undefined) {
137
138
  message = "Required";
138
139
  }
139
140
  else {
@@ -144,22 +145,16 @@ const defaultErrorMap = (issue, _ctx) => {
144
145
  message = `Invalid literal value, expected ${JSON.stringify(issue.expected)}`;
145
146
  break;
146
147
  case exports.ZodIssueCode.unrecognized_keys:
147
- message = `Unrecognized key(s) in object: ${issue.keys
148
- .map((k) => `'${k}'`)
149
- .join(", ")}`;
148
+ message = `Unrecognized key(s) in object: ${util_1.util.joinValues(issue.keys, ", ")}`;
150
149
  break;
151
150
  case exports.ZodIssueCode.invalid_union:
152
151
  message = `Invalid input`;
153
152
  break;
154
153
  case exports.ZodIssueCode.invalid_union_discriminator:
155
- message = `Invalid discriminator value. Expected ${issue.options
156
- .map((val) => (typeof val === "string" ? `'${val}'` : val))
157
- .join(" | ")}`;
154
+ message = `Invalid discriminator value. Expected ${util_1.util.joinValues(issue.options)}`;
158
155
  break;
159
156
  case exports.ZodIssueCode.invalid_enum_value:
160
- message = `Invalid enum value. Expected ${issue.options
161
- .map((val) => (typeof val === "string" ? `'${val}'` : val))
162
- .join(" | ")}`;
157
+ message = `Invalid enum value. Expected ${util_1.util.joinValues(issue.options)}, received '${issue.received}'`;
163
158
  break;
164
159
  case exports.ZodIssueCode.invalid_arguments:
165
160
  message = `Invalid function arguments`;
@@ -15,4 +15,5 @@ export declare namespace util {
15
15
  }>;
16
16
  type noUndefined<T> = T extends undefined ? never : T;
17
17
  const isInteger: NumberConstructor["isInteger"];
18
+ function joinValues<T extends any[]>(array: T, separator?: string): string;
18
19
  }
@@ -48,4 +48,10 @@ var util;
48
48
  util.isInteger = typeof Number.isInteger === "function"
49
49
  ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
50
50
  : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
51
+ function joinValues(array, separator = " | ") {
52
+ return array
53
+ .map((val) => (typeof val === "string" ? `'${val}'` : val))
54
+ .join(separator);
55
+ }
56
+ util.joinValues = joinValues;
51
57
  })(util = exports.util || (exports.util = {}));
package/lib/index.mjs CHANGED
@@ -45,6 +45,12 @@ var util;
45
45
  util.isInteger = typeof Number.isInteger === "function"
46
46
  ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
47
47
  : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
48
+ function joinValues(array, separator = " | ") {
49
+ return array
50
+ .map((val) => (typeof val === "string" ? `'${val}'` : val))
51
+ .join(separator);
52
+ }
53
+ util.joinValues = joinValues;
48
54
  })(util || (util = {}));
49
55
 
50
56
  const ZodIssueCode = util.arrayToEnum([
@@ -119,14 +125,14 @@ class ZodError extends Error {
119
125
  const el = issue.path[i];
120
126
  const terminal = i === issue.path.length - 1;
121
127
  if (!terminal) {
122
- if (typeof el === "string") {
123
- curr[el] = curr[el] || { _errors: [] };
124
- }
125
- else if (typeof el === "number") {
126
- const errorArray = [];
127
- errorArray._errors = [];
128
- curr[el] = curr[el] || errorArray;
129
- }
128
+ curr[el] = curr[el] || { _errors: [] };
129
+ // if (typeof el === "string") {
130
+ // curr[el] = curr[el] || { _errors: [] };
131
+ // } else if (typeof el === "number") {
132
+ // const errorArray: any = [];
133
+ // errorArray._errors = [];
134
+ // curr[el] = curr[el] || errorArray;
135
+ // }
130
136
  }
131
137
  else {
132
138
  curr[el] = curr[el] || { _errors: [] };
@@ -176,7 +182,7 @@ const defaultErrorMap = (issue, _ctx) => {
176
182
  let message;
177
183
  switch (issue.code) {
178
184
  case ZodIssueCode.invalid_type:
179
- if (issue.received === "undefined") {
185
+ if (issue.received === ZodParsedType.undefined) {
180
186
  message = "Required";
181
187
  }
182
188
  else {
@@ -187,22 +193,16 @@ const defaultErrorMap = (issue, _ctx) => {
187
193
  message = `Invalid literal value, expected ${JSON.stringify(issue.expected)}`;
188
194
  break;
189
195
  case ZodIssueCode.unrecognized_keys:
190
- message = `Unrecognized key(s) in object: ${issue.keys
191
- .map((k) => `'${k}'`)
192
- .join(", ")}`;
196
+ message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
193
197
  break;
194
198
  case ZodIssueCode.invalid_union:
195
199
  message = `Invalid input`;
196
200
  break;
197
201
  case ZodIssueCode.invalid_union_discriminator:
198
- message = `Invalid discriminator value. Expected ${issue.options
199
- .map((val) => (typeof val === "string" ? `'${val}'` : val))
200
- .join(" | ")}`;
202
+ message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
201
203
  break;
202
204
  case ZodIssueCode.invalid_enum_value:
203
- message = `Invalid enum value. Expected ${issue.options
204
- .map((val) => (typeof val === "string" ? `'${val}'` : val))
205
- .join(" | ")}`;
205
+ message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
206
206
  break;
207
207
  case ZodIssueCode.invalid_arguments:
208
208
  message = `Invalid function arguments`;
@@ -709,8 +709,8 @@ class ZodString extends ZodType {
709
709
  ...errorUtil.errToObj(message),
710
710
  });
711
711
  /**
712
- * Deprecated.
713
- * Use z.string().min(1) instead.
712
+ * @deprecated Use z.string().min(1) instead.
713
+ * @see {@link ZodString.min}
714
714
  */
715
715
  this.nonempty = (message) => this.min(1, errorUtil.errToObj(message));
716
716
  }
@@ -2390,11 +2390,23 @@ function createZodEnum(values) {
2390
2390
  }
2391
2391
  class ZodEnum extends ZodType {
2392
2392
  _parse(input) {
2393
+ if (typeof input.data !== "string") {
2394
+ const ctx = this._getOrReturnCtx(input);
2395
+ const expectedValues = this._def.values;
2396
+ addIssueToContext(ctx, {
2397
+ expected: util.joinValues(expectedValues),
2398
+ received: ctx.parsedType,
2399
+ code: ZodIssueCode.invalid_type,
2400
+ });
2401
+ return INVALID;
2402
+ }
2393
2403
  if (this._def.values.indexOf(input.data) === -1) {
2394
2404
  const ctx = this._getOrReturnCtx(input);
2405
+ const expectedValues = this._def.values;
2395
2406
  addIssueToContext(ctx, {
2407
+ received: ctx.data,
2396
2408
  code: ZodIssueCode.invalid_enum_value,
2397
- options: this._def.values,
2409
+ options: expectedValues,
2398
2410
  });
2399
2411
  return INVALID;
2400
2412
  }
@@ -2429,11 +2441,23 @@ ZodEnum.create = createZodEnum;
2429
2441
  class ZodNativeEnum extends ZodType {
2430
2442
  _parse(input) {
2431
2443
  const nativeEnumValues = util.getValidEnumValues(this._def.values);
2444
+ const ctx = this._getOrReturnCtx(input);
2445
+ if (ctx.parsedType !== ZodParsedType.string &&
2446
+ ctx.parsedType !== ZodParsedType.number) {
2447
+ const expectedValues = util.objectValues(nativeEnumValues);
2448
+ addIssueToContext(ctx, {
2449
+ expected: util.joinValues(expectedValues),
2450
+ received: ctx.parsedType,
2451
+ code: ZodIssueCode.invalid_type,
2452
+ });
2453
+ return INVALID;
2454
+ }
2432
2455
  if (nativeEnumValues.indexOf(input.data) === -1) {
2433
- const ctx = this._getOrReturnCtx(input);
2456
+ const expectedValues = util.objectValues(nativeEnumValues);
2434
2457
  addIssueToContext(ctx, {
2458
+ received: ctx.data,
2435
2459
  code: ZodIssueCode.invalid_enum_value,
2436
- options: util.objectValues(nativeEnumValues),
2460
+ options: expectedValues,
2437
2461
  });
2438
2462
  return INVALID;
2439
2463
  }
@@ -2591,7 +2615,7 @@ class ZodEffects extends ZodType {
2591
2615
  // if (base.status === "dirty") {
2592
2616
  // return { status: "dirty", value: base.value };
2593
2617
  // }
2594
- return Promise.resolve(effect.transform(base.value, checkCtx)).then(OK);
2618
+ return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
2595
2619
  });
2596
2620
  }
2597
2621
  }
@@ -2697,9 +2721,15 @@ ZodNaN.create = (params) => {
2697
2721
  ...processCreateParams(params),
2698
2722
  });
2699
2723
  };
2700
- const custom = (check, params) => {
2724
+ const custom = (check, params = {}, fatal) => {
2701
2725
  if (check)
2702
- return ZodAny.create().refine(check, params);
2726
+ return ZodAny.create().superRefine((data, ctx) => {
2727
+ if (!check(data)) {
2728
+ const p = typeof params === "function" ? params(data) : params;
2729
+ const p2 = typeof p === "string" ? { message: p } : p;
2730
+ ctx.addIssue({ code: "custom", ...p2, fatal });
2731
+ }
2732
+ });
2703
2733
  return ZodAny.create();
2704
2734
  };
2705
2735
  const late = {
@@ -2741,7 +2771,7 @@ var ZodFirstPartyTypeKind;
2741
2771
  })(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
2742
2772
  const instanceOfType = (cls, params = {
2743
2773
  message: `Input not instance of ${cls.name}`,
2744
- }) => custom((data) => data instanceof cls, params);
2774
+ }) => custom((data) => data instanceof cls, params, true);
2745
2775
  const stringType = ZodString.create;
2746
2776
  const numberType = ZodNumber.create;
2747
2777
  const nanType = ZodNaN.create;
package/lib/index.umd.js CHANGED
@@ -51,6 +51,12 @@
51
51
  util.isInteger = typeof Number.isInteger === "function"
52
52
  ? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
53
53
  : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
54
+ function joinValues(array, separator = " | ") {
55
+ return array
56
+ .map((val) => (typeof val === "string" ? `'${val}'` : val))
57
+ .join(separator);
58
+ }
59
+ util.joinValues = joinValues;
54
60
  })(util || (util = {}));
55
61
 
56
62
  const ZodIssueCode = util.arrayToEnum([
@@ -125,14 +131,14 @@
125
131
  const el = issue.path[i];
126
132
  const terminal = i === issue.path.length - 1;
127
133
  if (!terminal) {
128
- if (typeof el === "string") {
129
- curr[el] = curr[el] || { _errors: [] };
130
- }
131
- else if (typeof el === "number") {
132
- const errorArray = [];
133
- errorArray._errors = [];
134
- curr[el] = curr[el] || errorArray;
135
- }
134
+ curr[el] = curr[el] || { _errors: [] };
135
+ // if (typeof el === "string") {
136
+ // curr[el] = curr[el] || { _errors: [] };
137
+ // } else if (typeof el === "number") {
138
+ // const errorArray: any = [];
139
+ // errorArray._errors = [];
140
+ // curr[el] = curr[el] || errorArray;
141
+ // }
136
142
  }
137
143
  else {
138
144
  curr[el] = curr[el] || { _errors: [] };
@@ -182,7 +188,7 @@
182
188
  let message;
183
189
  switch (issue.code) {
184
190
  case ZodIssueCode.invalid_type:
185
- if (issue.received === "undefined") {
191
+ if (issue.received === ZodParsedType.undefined) {
186
192
  message = "Required";
187
193
  }
188
194
  else {
@@ -193,22 +199,16 @@
193
199
  message = `Invalid literal value, expected ${JSON.stringify(issue.expected)}`;
194
200
  break;
195
201
  case ZodIssueCode.unrecognized_keys:
196
- message = `Unrecognized key(s) in object: ${issue.keys
197
- .map((k) => `'${k}'`)
198
- .join(", ")}`;
202
+ message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
199
203
  break;
200
204
  case ZodIssueCode.invalid_union:
201
205
  message = `Invalid input`;
202
206
  break;
203
207
  case ZodIssueCode.invalid_union_discriminator:
204
- message = `Invalid discriminator value. Expected ${issue.options
205
- .map((val) => (typeof val === "string" ? `'${val}'` : val))
206
- .join(" | ")}`;
208
+ message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
207
209
  break;
208
210
  case ZodIssueCode.invalid_enum_value:
209
- message = `Invalid enum value. Expected ${issue.options
210
- .map((val) => (typeof val === "string" ? `'${val}'` : val))
211
- .join(" | ")}`;
211
+ message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
212
212
  break;
213
213
  case ZodIssueCode.invalid_arguments:
214
214
  message = `Invalid function arguments`;
@@ -715,8 +715,8 @@
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
722
  }
@@ -2396,11 +2396,23 @@
2396
2396
  }
2397
2397
  class ZodEnum extends ZodType {
2398
2398
  _parse(input) {
2399
+ if (typeof input.data !== "string") {
2400
+ const ctx = this._getOrReturnCtx(input);
2401
+ const expectedValues = this._def.values;
2402
+ addIssueToContext(ctx, {
2403
+ expected: util.joinValues(expectedValues),
2404
+ received: ctx.parsedType,
2405
+ code: ZodIssueCode.invalid_type,
2406
+ });
2407
+ return INVALID;
2408
+ }
2399
2409
  if (this._def.values.indexOf(input.data) === -1) {
2400
2410
  const ctx = this._getOrReturnCtx(input);
2411
+ const expectedValues = this._def.values;
2401
2412
  addIssueToContext(ctx, {
2413
+ received: ctx.data,
2402
2414
  code: ZodIssueCode.invalid_enum_value,
2403
- options: this._def.values,
2415
+ options: expectedValues,
2404
2416
  });
2405
2417
  return INVALID;
2406
2418
  }
@@ -2435,11 +2447,23 @@
2435
2447
  class ZodNativeEnum extends ZodType {
2436
2448
  _parse(input) {
2437
2449
  const nativeEnumValues = util.getValidEnumValues(this._def.values);
2450
+ const ctx = this._getOrReturnCtx(input);
2451
+ if (ctx.parsedType !== ZodParsedType.string &&
2452
+ ctx.parsedType !== ZodParsedType.number) {
2453
+ const expectedValues = util.objectValues(nativeEnumValues);
2454
+ addIssueToContext(ctx, {
2455
+ expected: util.joinValues(expectedValues),
2456
+ received: ctx.parsedType,
2457
+ code: ZodIssueCode.invalid_type,
2458
+ });
2459
+ return INVALID;
2460
+ }
2438
2461
  if (nativeEnumValues.indexOf(input.data) === -1) {
2439
- const ctx = this._getOrReturnCtx(input);
2462
+ const expectedValues = util.objectValues(nativeEnumValues);
2440
2463
  addIssueToContext(ctx, {
2464
+ received: ctx.data,
2441
2465
  code: ZodIssueCode.invalid_enum_value,
2442
- options: util.objectValues(nativeEnumValues),
2466
+ options: expectedValues,
2443
2467
  });
2444
2468
  return INVALID;
2445
2469
  }
@@ -2597,7 +2621,7 @@
2597
2621
  // if (base.status === "dirty") {
2598
2622
  // return { status: "dirty", value: base.value };
2599
2623
  // }
2600
- return Promise.resolve(effect.transform(base.value, checkCtx)).then(OK);
2624
+ return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
2601
2625
  });
2602
2626
  }
2603
2627
  }
@@ -2703,9 +2727,15 @@
2703
2727
  ...processCreateParams(params),
2704
2728
  });
2705
2729
  };
2706
- const custom = (check, params) => {
2730
+ const custom = (check, params = {}, fatal) => {
2707
2731
  if (check)
2708
- return ZodAny.create().refine(check, params);
2732
+ return ZodAny.create().superRefine((data, ctx) => {
2733
+ if (!check(data)) {
2734
+ const p = typeof params === "function" ? params(data) : params;
2735
+ const p2 = typeof p === "string" ? { message: p } : p;
2736
+ ctx.addIssue({ code: "custom", ...p2, fatal });
2737
+ }
2738
+ });
2709
2739
  return ZodAny.create();
2710
2740
  };
2711
2741
  const late = {
@@ -2747,7 +2777,7 @@
2747
2777
  })(exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
2748
2778
  const instanceOfType = (cls, params = {
2749
2779
  message: `Input not instance of ${cls.name}`,
2750
- }) => custom((data) => data instanceof cls, params);
2780
+ }) => custom((data) => data instanceof cls, params, true);
2751
2781
  const stringType = ZodString.create;
2752
2782
  const numberType = ZodNumber.create;
2753
2783
  const nanType = ZodNaN.create;
package/lib/types.d.ts CHANGED
@@ -120,8 +120,8 @@ export declare class ZodString extends ZodType<string, ZodStringDef> {
120
120
  max(maxLength: number, message?: errorUtil.ErrMessage): ZodString;
121
121
  length(len: number, message?: errorUtil.ErrMessage): ZodString;
122
122
  /**
123
- * Deprecated.
124
- * Use z.string().min(1) instead.
123
+ * @deprecated Use z.string().min(1) instead.
124
+ * @see {@link ZodString.min}
125
125
  */
126
126
  nonempty: (message?: errorUtil.ErrMessage | undefined) => ZodString;
127
127
  get isEmail(): boolean;
@@ -267,28 +267,28 @@ export declare class ZodArray<T extends ZodTypeAny, Cardinality extends ArrayCar
267
267
  }
268
268
  export declare type ZodNonEmptyArray<T extends ZodTypeAny> = ZodArray<T, "atleastone">;
269
269
  export declare namespace objectUtil {
270
- type MergeShapes<U extends ZodRawShape, V extends ZodRawShape> = {
270
+ export type MergeShapes<U extends ZodRawShape, V extends ZodRawShape> = {
271
271
  [k in Exclude<keyof U, keyof V>]: U[k];
272
272
  } & V;
273
+ type optionalKeys<T extends object> = {
274
+ [k in keyof T]: undefined extends T[k] ? k : never;
275
+ }[keyof T];
273
276
  type requiredKeys<T extends object> = {
274
277
  [k in keyof T]: undefined extends T[k] ? never : k;
275
278
  }[keyof T];
276
- type addQuestionMarks<T extends object> = {
277
- [k in keyof T]?: T[k];
278
- } & {
279
- [k in requiredKeys<T>]: T[k];
280
- };
281
- type identity<T> = T;
282
- type flatten<T extends object> = identity<{
279
+ export type addQuestionMarks<T extends object> = Partial<Pick<T, optionalKeys<T>>> & Pick<T, requiredKeys<T>>;
280
+ export type identity<T> = T;
281
+ export type flatten<T extends object> = identity<{
283
282
  [k in keyof T]: T[k];
284
283
  }>;
285
- type noNeverKeys<T extends ZodRawShape> = {
284
+ export type noNeverKeys<T extends ZodRawShape> = {
286
285
  [k in keyof T]: [T[k]] extends [never] ? never : k;
287
286
  }[keyof T];
288
- type noNever<T extends ZodRawShape> = identity<{
287
+ export type noNever<T extends ZodRawShape> = identity<{
289
288
  [k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
290
289
  }>;
291
- const mergeShapes: <U extends ZodRawShape, T extends ZodRawShape>(first: U, second: T) => T & U;
290
+ export const mergeShapes: <U extends ZodRawShape, T extends ZodRawShape>(first: U, second: T) => T & U;
291
+ export {};
292
292
  }
293
293
  export declare type extendShape<A, B> = Omit<A, keyof B> & B;
294
294
  declare type UnknownKeysParam = "passthrough" | "strict" | "strip";
@@ -625,7 +625,7 @@ export declare class ZodNaN extends ZodType<number, ZodNaNDef> {
625
625
  _parse(input: ParseInput): ParseReturnType<any>;
626
626
  static create: (params?: RawCreateParams) => ZodNaN;
627
627
  }
628
- export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: Parameters<ZodTypeAny["refine"]>[1]) => ZodType<T, ZodTypeDef, T>;
628
+ export declare const custom: <T>(check?: ((data: unknown) => any) | undefined, params?: Parameters<ZodTypeAny["refine"]>[1], fatal?: boolean | undefined) => ZodType<T, ZodTypeDef, T>;
629
629
  export { ZodType as Schema, ZodType as ZodSchema };
630
630
  export declare const late: {
631
631
  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]; }>;
package/lib/types.js CHANGED
@@ -291,8 +291,8 @@ class ZodString extends ZodType {
291
291
  ...errorUtil_1.errorUtil.errToObj(message),
292
292
  });
293
293
  /**
294
- * Deprecated.
295
- * Use z.string().min(1) instead.
294
+ * @deprecated Use z.string().min(1) instead.
295
+ * @see {@link ZodString.min}
296
296
  */
297
297
  this.nonempty = (message) => this.min(1, errorUtil_1.errorUtil.errToObj(message));
298
298
  }
@@ -1996,11 +1996,23 @@ function createZodEnum(values) {
1996
1996
  }
1997
1997
  class ZodEnum extends ZodType {
1998
1998
  _parse(input) {
1999
+ if (typeof input.data !== "string") {
2000
+ const ctx = this._getOrReturnCtx(input);
2001
+ const expectedValues = this._def.values;
2002
+ (0, parseUtil_1.addIssueToContext)(ctx, {
2003
+ expected: util_1.util.joinValues(expectedValues),
2004
+ received: ctx.parsedType,
2005
+ code: ZodError_1.ZodIssueCode.invalid_type,
2006
+ });
2007
+ return parseUtil_1.INVALID;
2008
+ }
1999
2009
  if (this._def.values.indexOf(input.data) === -1) {
2000
2010
  const ctx = this._getOrReturnCtx(input);
2011
+ const expectedValues = this._def.values;
2001
2012
  (0, parseUtil_1.addIssueToContext)(ctx, {
2013
+ received: ctx.data,
2002
2014
  code: ZodError_1.ZodIssueCode.invalid_enum_value,
2003
- options: this._def.values,
2015
+ options: expectedValues,
2004
2016
  });
2005
2017
  return parseUtil_1.INVALID;
2006
2018
  }
@@ -2036,11 +2048,23 @@ ZodEnum.create = createZodEnum;
2036
2048
  class ZodNativeEnum extends ZodType {
2037
2049
  _parse(input) {
2038
2050
  const nativeEnumValues = util_1.util.getValidEnumValues(this._def.values);
2051
+ const ctx = this._getOrReturnCtx(input);
2052
+ if (ctx.parsedType !== parseUtil_1.ZodParsedType.string &&
2053
+ ctx.parsedType !== parseUtil_1.ZodParsedType.number) {
2054
+ const expectedValues = util_1.util.objectValues(nativeEnumValues);
2055
+ (0, parseUtil_1.addIssueToContext)(ctx, {
2056
+ expected: util_1.util.joinValues(expectedValues),
2057
+ received: ctx.parsedType,
2058
+ code: ZodError_1.ZodIssueCode.invalid_type,
2059
+ });
2060
+ return parseUtil_1.INVALID;
2061
+ }
2039
2062
  if (nativeEnumValues.indexOf(input.data) === -1) {
2040
- const ctx = this._getOrReturnCtx(input);
2063
+ const expectedValues = util_1.util.objectValues(nativeEnumValues);
2041
2064
  (0, parseUtil_1.addIssueToContext)(ctx, {
2065
+ received: ctx.data,
2042
2066
  code: ZodError_1.ZodIssueCode.invalid_enum_value,
2043
- options: util_1.util.objectValues(nativeEnumValues),
2067
+ options: expectedValues,
2044
2068
  });
2045
2069
  return parseUtil_1.INVALID;
2046
2070
  }
@@ -2200,7 +2224,7 @@ class ZodEffects extends ZodType {
2200
2224
  // if (base.status === "dirty") {
2201
2225
  // return { status: "dirty", value: base.value };
2202
2226
  // }
2203
- return Promise.resolve(effect.transform(base.value, checkCtx)).then(parseUtil_1.OK);
2227
+ return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
2204
2228
  });
2205
2229
  }
2206
2230
  }
@@ -2312,9 +2336,15 @@ ZodNaN.create = (params) => {
2312
2336
  ...processCreateParams(params),
2313
2337
  });
2314
2338
  };
2315
- const custom = (check, params) => {
2339
+ const custom = (check, params = {}, fatal) => {
2316
2340
  if (check)
2317
- return ZodAny.create().refine(check, params);
2341
+ return ZodAny.create().superRefine((data, ctx) => {
2342
+ if (!check(data)) {
2343
+ const p = typeof params === "function" ? params(data) : params;
2344
+ const p2 = typeof p === "string" ? { message: p } : p;
2345
+ ctx.addIssue({ code: "custom", ...p2, fatal });
2346
+ }
2347
+ });
2318
2348
  return ZodAny.create();
2319
2349
  };
2320
2350
  exports.custom = custom;
@@ -2357,7 +2387,7 @@ var ZodFirstPartyTypeKind;
2357
2387
  })(ZodFirstPartyTypeKind = exports.ZodFirstPartyTypeKind || (exports.ZodFirstPartyTypeKind = {}));
2358
2388
  const instanceOfType = (cls, params = {
2359
2389
  message: `Input not instance of ${cls.name}`,
2360
- }) => (0, exports.custom)((data) => data instanceof cls, params);
2390
+ }) => (0, exports.custom)((data) => data instanceof cls, params, true);
2361
2391
  exports.instanceof = instanceOfType;
2362
2392
  const stringType = ZodString.create;
2363
2393
  exports.string = stringType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.15.0",
3
+ "version": "3.16.1",
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",