zod 3.17.6 → 3.17.9

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
@@ -63,6 +63,7 @@
63
63
  - [Nullables](#nullables)
64
64
  - [Objects](#objects)
65
65
  - [.shape](#shape)
66
+ - [.enum](#enum)
66
67
  - [.extend](#extend)
67
68
  - [.merge](#merge)
68
69
  - [.pick/.omit](#pickomit)
@@ -550,14 +551,32 @@ const isActive = z.boolean({
550
551
 
551
552
  ## Dates
552
553
 
553
- z.date() accepts a date, not a date string
554
+ Use z.date() to validate `Date` instances.
554
555
 
555
556
  ```ts
556
557
  z.date().safeParse(new Date()); // success: true
557
558
  z.date().safeParse("2022-01-12T00:00:00.000Z"); // success: false
558
559
  ```
559
560
 
560
- To allow for dates or date strings, you can use preprocess
561
+ You can customize certain error messages when creating a boolean schema.
562
+
563
+ ```ts
564
+ const myDateSchema = z.date({
565
+ required_error: "Please select a date and time",
566
+ invalid_type_error: "That's not a date!",
567
+ });
568
+ ```
569
+
570
+ Zod provides a handful of date-specific validations.
571
+
572
+ ```ts
573
+ z.date().min(new Date("1900-01-01"), { message: "Too old" });
574
+ z.date().max(new Date(), { message: "Too young!" });
575
+ ```
576
+
577
+ **Supporting date strings**
578
+
579
+ To write a schema that accepts either a `Date` or a date string, use (`z.preprocess`)[#preprocess].
561
580
 
562
581
  ```ts
563
582
  const dateSchema = z.preprocess((arg) => {
@@ -764,6 +783,15 @@ Dog.shape.name; // => string schema
764
783
  Dog.shape.age; // => number schema
765
784
  ```
766
785
 
786
+ ### `.keyof`
787
+
788
+ Use `.key` to create a `ZodEnum` schema from the keys of an object schema.
789
+
790
+ ```ts
791
+ const keySchema = Dog.keyof();
792
+ keySchema; // ZodEnum<["name", "age"]>
793
+ ```
794
+
767
795
  ### `.extend`
768
796
 
769
797
  You can add additional fields to an object schema with the `.extend` method.
@@ -1772,7 +1800,7 @@ z.optional(z.string());
1772
1800
 
1773
1801
  ### `.nullable`
1774
1802
 
1775
- A convenience method that returns an nullable version of a schema.
1803
+ A convenience method that returns a nullable version of a schema.
1776
1804
 
1777
1805
  ```ts
1778
1806
  const nullableString = z.string().nullable(); // string | null
package/lib/ZodError.d.ts CHANGED
@@ -81,13 +81,13 @@ export interface ZodTooSmallIssue extends ZodIssueBase {
81
81
  code: typeof ZodIssueCode.too_small;
82
82
  minimum: number;
83
83
  inclusive: boolean;
84
- type: "array" | "string" | "number" | "set";
84
+ type: "array" | "string" | "number" | "set" | "date";
85
85
  }
86
86
  export interface ZodTooBigIssue extends ZodIssueBase {
87
87
  code: typeof ZodIssueCode.too_big;
88
88
  maximum: number;
89
89
  inclusive: boolean;
90
- type: "array" | "string" | "number" | "set";
90
+ type: "array" | "string" | "number" | "set" | "date";
91
91
  }
92
92
  export interface ZodInvalidIntersectionTypesIssue extends ZodIssueBase {
93
93
  code: typeof ZodIssueCode.invalid_intersection_types;
package/lib/ZodError.js CHANGED
@@ -191,6 +191,8 @@ const defaultErrorMap = (issue, _ctx) => {
191
191
  message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
192
192
  else if (issue.type === "number")
193
193
  message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
194
+ else if (issue.type === "date")
195
+ message = `Date must be greater than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.minimum)}`;
194
196
  else
195
197
  message = "Invalid input";
196
198
  break;
@@ -201,6 +203,8 @@ const defaultErrorMap = (issue, _ctx) => {
201
203
  message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
202
204
  else if (issue.type === "number")
203
205
  message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
206
+ else if (issue.type === "date")
207
+ message = `Date must be smaller than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.maximum)}`;
204
208
  else
205
209
  message = "Invalid input";
206
210
  break;
@@ -0,0 +1,8 @@
1
+ export declare namespace enumUtil {
2
+ type UnionToIntersectionFn<T> = (T extends unknown ? (k: () => T) => void : never) extends (k: infer Intersection) => void ? Intersection : never;
3
+ type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last ? Last : never;
4
+ type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never] ? Tuple : UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>;
5
+ type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never;
6
+ export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>;
7
+ export {};
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,5 +1,6 @@
1
1
  export declare namespace util {
2
2
  type AssertEqual<T, Expected> = [T] extends [Expected] ? [Expected] extends [T] ? true : false : false;
3
+ function assertEqual<A, B>(_cond: AssertEqual<A, B>): void;
3
4
  function assertNever(_x: never): never;
4
5
  type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
5
6
  type OmitKeys<T, K extends string> = Pick<T, Exclude<keyof T, K>>;
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getParsedType = exports.ZodParsedType = exports.util = void 0;
4
4
  var util;
5
5
  (function (util) {
6
+ function assertEqual(_cond) { }
7
+ util.assertEqual = assertEqual;
6
8
  function assertNever(_x) {
7
9
  throw new Error();
8
10
  }
package/lib/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
1
  var util;
2
2
  (function (util) {
3
+ function assertEqual(_cond) { }
4
+ util.assertEqual = assertEqual;
3
5
  function assertNever(_x) {
4
6
  throw new Error();
5
7
  }
@@ -303,6 +305,8 @@ const defaultErrorMap = (issue, _ctx) => {
303
305
  message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
304
306
  else if (issue.type === "number")
305
307
  message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
308
+ else if (issue.type === "date")
309
+ message = `Date must be greater than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.minimum)}`;
306
310
  else
307
311
  message = "Invalid input";
308
312
  break;
@@ -313,6 +317,8 @@ const defaultErrorMap = (issue, _ctx) => {
313
317
  message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
314
318
  else if (issue.type === "number")
315
319
  message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
320
+ else if (issue.type === "date")
321
+ message = `Date must be smaller than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.maximum)}`;
316
322
  else
317
323
  message = "Invalid input";
318
324
  break;
@@ -484,11 +490,10 @@ function processCreateParams(params) {
484
490
  const customMap = (iss, ctx) => {
485
491
  if (iss.code !== "invalid_type")
486
492
  return { message: ctx.defaultError };
487
- if (typeof ctx.data === "undefined" && required_error)
488
- return { message: required_error };
489
- if (params.invalid_type_error)
490
- return { message: params.invalid_type_error };
491
- return { message: ctx.defaultError };
493
+ if (typeof ctx.data === "undefined") {
494
+ return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
495
+ }
496
+ return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
492
497
  };
493
498
  return { errorMap: customMap, description };
494
499
  }
@@ -1229,14 +1234,88 @@ class ZodDate extends ZodType {
1229
1234
  });
1230
1235
  return INVALID;
1231
1236
  }
1237
+ const status = new ParseStatus();
1238
+ let ctx = undefined;
1239
+ for (const check of this._def.checks) {
1240
+ if (check.kind === "min") {
1241
+ if (input.data.getTime() < check.value) {
1242
+ ctx = this._getOrReturnCtx(input, ctx);
1243
+ addIssueToContext(ctx, {
1244
+ code: ZodIssueCode.too_small,
1245
+ message: check.message,
1246
+ inclusive: true,
1247
+ minimum: check.value,
1248
+ type: "date",
1249
+ });
1250
+ status.dirty();
1251
+ }
1252
+ }
1253
+ else if (check.kind === "max") {
1254
+ if (input.data.getTime() > check.value) {
1255
+ ctx = this._getOrReturnCtx(input, ctx);
1256
+ addIssueToContext(ctx, {
1257
+ code: ZodIssueCode.too_big,
1258
+ message: check.message,
1259
+ inclusive: true,
1260
+ maximum: check.value,
1261
+ type: "date",
1262
+ });
1263
+ status.dirty();
1264
+ }
1265
+ }
1266
+ else {
1267
+ util.assertNever(check);
1268
+ }
1269
+ }
1232
1270
  return {
1233
- status: "valid",
1271
+ status: status.value,
1234
1272
  value: new Date(input.data.getTime()),
1235
1273
  };
1236
1274
  }
1275
+ _addCheck(check) {
1276
+ return new ZodDate({
1277
+ ...this._def,
1278
+ checks: [...this._def.checks, check],
1279
+ });
1280
+ }
1281
+ min(minDate, message) {
1282
+ return this._addCheck({
1283
+ kind: "min",
1284
+ value: minDate.getTime(),
1285
+ message: errorUtil.toString(message),
1286
+ });
1287
+ }
1288
+ max(maxDate, message) {
1289
+ return this._addCheck({
1290
+ kind: "max",
1291
+ value: maxDate.getTime(),
1292
+ message: errorUtil.toString(message),
1293
+ });
1294
+ }
1295
+ get minDate() {
1296
+ let min = null;
1297
+ for (const ch of this._def.checks) {
1298
+ if (ch.kind === "min") {
1299
+ if (min === null || ch.value > min)
1300
+ min = ch.value;
1301
+ }
1302
+ }
1303
+ return min != null ? new Date(min) : null;
1304
+ }
1305
+ get maxDate() {
1306
+ let max = null;
1307
+ for (const ch of this._def.checks) {
1308
+ if (ch.kind === "max") {
1309
+ if (max === null || ch.value < max)
1310
+ max = ch.value;
1311
+ }
1312
+ }
1313
+ return max != null ? new Date(max) : null;
1314
+ }
1237
1315
  }
1238
1316
  ZodDate.create = (params) => {
1239
1317
  return new ZodDate({
1318
+ checks: [],
1240
1319
  typeName: ZodFirstPartyTypeKind.ZodDate,
1241
1320
  ...processCreateParams(params),
1242
1321
  });
@@ -1725,6 +1804,9 @@ class ZodObject extends ZodType {
1725
1804
  shape: () => newShape,
1726
1805
  });
1727
1806
  }
1807
+ keyof() {
1808
+ return createZodEnum(util.objectKeys(this.shape));
1809
+ }
1728
1810
  }
1729
1811
  ZodObject.create = (shape, params) => {
1730
1812
  return new ZodObject({
package/lib/index.umd.js CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  var util;
8
8
  (function (util) {
9
+ function assertEqual(_cond) { }
10
+ util.assertEqual = assertEqual;
9
11
  function assertNever(_x) {
10
12
  throw new Error();
11
13
  }
@@ -309,6 +311,8 @@
309
311
  message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
310
312
  else if (issue.type === "number")
311
313
  message = `Number must be greater than ${issue.inclusive ? `or equal to ` : ``}${issue.minimum}`;
314
+ else if (issue.type === "date")
315
+ message = `Date must be greater than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.minimum)}`;
312
316
  else
313
317
  message = "Invalid input";
314
318
  break;
@@ -319,6 +323,8 @@
319
323
  message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
320
324
  else if (issue.type === "number")
321
325
  message = `Number must be less than ${issue.inclusive ? `or equal to ` : ``}${issue.maximum}`;
326
+ else if (issue.type === "date")
327
+ message = `Date must be smaller than ${issue.inclusive ? `or equal to ` : ``}${new Date(issue.maximum)}`;
322
328
  else
323
329
  message = "Invalid input";
324
330
  break;
@@ -490,11 +496,10 @@
490
496
  const customMap = (iss, ctx) => {
491
497
  if (iss.code !== "invalid_type")
492
498
  return { message: ctx.defaultError };
493
- if (typeof ctx.data === "undefined" && required_error)
494
- return { message: required_error };
495
- if (params.invalid_type_error)
496
- return { message: params.invalid_type_error };
497
- return { message: ctx.defaultError };
499
+ if (typeof ctx.data === "undefined") {
500
+ return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
501
+ }
502
+ return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
498
503
  };
499
504
  return { errorMap: customMap, description };
500
505
  }
@@ -1235,14 +1240,88 @@
1235
1240
  });
1236
1241
  return INVALID;
1237
1242
  }
1243
+ const status = new ParseStatus();
1244
+ let ctx = undefined;
1245
+ for (const check of this._def.checks) {
1246
+ if (check.kind === "min") {
1247
+ if (input.data.getTime() < check.value) {
1248
+ ctx = this._getOrReturnCtx(input, ctx);
1249
+ addIssueToContext(ctx, {
1250
+ code: ZodIssueCode.too_small,
1251
+ message: check.message,
1252
+ inclusive: true,
1253
+ minimum: check.value,
1254
+ type: "date",
1255
+ });
1256
+ status.dirty();
1257
+ }
1258
+ }
1259
+ else if (check.kind === "max") {
1260
+ if (input.data.getTime() > check.value) {
1261
+ ctx = this._getOrReturnCtx(input, ctx);
1262
+ addIssueToContext(ctx, {
1263
+ code: ZodIssueCode.too_big,
1264
+ message: check.message,
1265
+ inclusive: true,
1266
+ maximum: check.value,
1267
+ type: "date",
1268
+ });
1269
+ status.dirty();
1270
+ }
1271
+ }
1272
+ else {
1273
+ util.assertNever(check);
1274
+ }
1275
+ }
1238
1276
  return {
1239
- status: "valid",
1277
+ status: status.value,
1240
1278
  value: new Date(input.data.getTime()),
1241
1279
  };
1242
1280
  }
1281
+ _addCheck(check) {
1282
+ return new ZodDate({
1283
+ ...this._def,
1284
+ checks: [...this._def.checks, check],
1285
+ });
1286
+ }
1287
+ min(minDate, message) {
1288
+ return this._addCheck({
1289
+ kind: "min",
1290
+ value: minDate.getTime(),
1291
+ message: errorUtil.toString(message),
1292
+ });
1293
+ }
1294
+ max(maxDate, message) {
1295
+ return this._addCheck({
1296
+ kind: "max",
1297
+ value: maxDate.getTime(),
1298
+ message: errorUtil.toString(message),
1299
+ });
1300
+ }
1301
+ get minDate() {
1302
+ let min = null;
1303
+ for (const ch of this._def.checks) {
1304
+ if (ch.kind === "min") {
1305
+ if (min === null || ch.value > min)
1306
+ min = ch.value;
1307
+ }
1308
+ }
1309
+ return min != null ? new Date(min) : null;
1310
+ }
1311
+ get maxDate() {
1312
+ let max = null;
1313
+ for (const ch of this._def.checks) {
1314
+ if (ch.kind === "max") {
1315
+ if (max === null || ch.value < max)
1316
+ max = ch.value;
1317
+ }
1318
+ }
1319
+ return max != null ? new Date(max) : null;
1320
+ }
1243
1321
  }
1244
1322
  ZodDate.create = (params) => {
1245
1323
  return new ZodDate({
1324
+ checks: [],
1246
1325
  typeName: exports.ZodFirstPartyTypeKind.ZodDate,
1247
1326
  ...processCreateParams(params),
1248
1327
  });
@@ -1731,6 +1810,9 @@
1731
1810
  shape: () => newShape,
1732
1811
  });
1733
1812
  }
1813
+ keyof() {
1814
+ return createZodEnum(util.objectKeys(this.shape));
1815
+ }
1734
1816
  }
1735
1817
  ZodObject.create = (shape, params) => {
1736
1818
  return new ZodObject({
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { enumUtil } from "./helpers/enumUtil";
1
2
  import { errorUtil } from "./helpers/errorUtil";
2
3
  import { AsyncParseReturnType, ParseContext, ParseInput, ParseParams, ParseReturnType, ParseStatus, SyncParseReturnType } from "./helpers/parseUtil";
3
4
  import { partialUtil } from "./helpers/partialUtil";
@@ -214,11 +215,26 @@ export declare class ZodBoolean extends ZodType<boolean, ZodBooleanDef> {
214
215
  _parse(input: ParseInput): ParseReturnType<boolean>;
215
216
  static create: (params?: RawCreateParams) => ZodBoolean;
216
217
  }
218
+ declare type ZodDateCheck = {
219
+ kind: "min";
220
+ value: number;
221
+ message?: string;
222
+ } | {
223
+ kind: "max";
224
+ value: number;
225
+ message?: string;
226
+ };
217
227
  export interface ZodDateDef extends ZodTypeDef {
228
+ checks: ZodDateCheck[];
218
229
  typeName: ZodFirstPartyTypeKind.ZodDate;
219
230
  }
220
231
  export declare class ZodDate extends ZodType<Date, ZodDateDef> {
221
232
  _parse(input: ParseInput): ParseReturnType<this["_output"]>;
233
+ _addCheck(check: ZodDateCheck): ZodDate;
234
+ min(minDate: Date, message?: errorUtil.ErrMessage): ZodDate;
235
+ max(maxDate: Date, message?: errorUtil.ErrMessage): ZodDate;
236
+ get minDate(): Date | null;
237
+ get maxDate(): Date | null;
222
238
  static create: (params?: RawCreateParams) => ZodDate;
223
239
  }
224
240
  export interface ZodUndefinedDef extends ZodTypeDef {
@@ -385,6 +401,7 @@ export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends Unknow
385
401
  required(): ZodObject<{
386
402
  [k in keyof T]: deoptional<T[k]>;
387
403
  }, UnknownKeys, Catchall>;
404
+ keyof(): ZodEnum<enumUtil.UnionToTupleString<keyof T>>;
388
405
  static create: <T_1 extends ZodRawShape>(shape: T_1, params?: RawCreateParams) => ZodObject<T_1, "strip", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>[k_3]; }>;
389
406
  static strictCreate: <T_1 extends ZodRawShape>(shape: T_1, params?: RawCreateParams) => ZodObject<T_1, "strict", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>[k_3]; }>;
390
407
  static lazycreate: <T_1 extends ZodRawShape>(shape: () => T_1, params?: RawCreateParams) => ZodObject<T_1, "strip", ZodTypeAny, { [k_1 in keyof objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>]: objectUtil.addQuestionMarks<{ [k in keyof T_1]: T_1[k]["_output"]; }>[k_1]; }, { [k_3 in keyof objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>]: objectUtil.addQuestionMarks<{ [k_2 in keyof T_1]: T_1[k_2]["_input"]; }>[k_3]; }>;
@@ -459,7 +476,7 @@ export declare class ZodTuple<T extends [ZodTypeAny, ...ZodTypeAny[]] | [] = [Zo
459
476
  _parse(input: ParseInput): ParseReturnType<this["_output"]>;
460
477
  get items(): T;
461
478
  rest<Rest extends ZodTypeAny>(rest: Rest): ZodTuple<T, Rest>;
462
- static create: <T_1 extends [ZodTypeAny, ...ZodTypeAny[]] | []>(schemas: T_1, params?: RawCreateParams) => ZodTuple<T_1, null>;
479
+ static create: <T_1 extends [] | [ZodTypeAny, ...ZodTypeAny[]]>(schemas: T_1, params?: RawCreateParams) => ZodTuple<T_1, null>;
463
480
  }
464
481
  export interface ZodRecordDef<Key extends KeySchema = ZodString, Value extends ZodTypeAny = ZodTypeAny> extends ZodTypeDef {
465
482
  valueType: Value;
@@ -518,9 +535,9 @@ export declare class ZodFunction<Args extends ZodTuple<any, any>, Returns extend
518
535
  returnType(): Returns;
519
536
  args<Items extends Parameters<typeof ZodTuple["create"]>[0]>(...items: Items): ZodFunction<ZodTuple<Items, ZodUnknown>, Returns>;
520
537
  returns<NewReturnType extends ZodType<any, any>>(returnType: NewReturnType): ZodFunction<Args, NewReturnType>;
521
- implement<F extends InnerTypeOfFunction<Args, Returns>>(func: F): F;
538
+ implement<F extends InnerTypeOfFunction<Args, Returns>>(func: F): ReturnType<F> extends Returns["_output"] ? (...args: Args["_input"]) => ReturnType<F> : OuterTypeOfFunction<Args, Returns>;
522
539
  strictImplement(func: InnerTypeOfFunction<Args, Returns>): InnerTypeOfFunction<Args, Returns>;
523
- validate: <F extends InnerTypeOfFunction<Args, Returns>>(func: F) => F;
540
+ validate: <F extends InnerTypeOfFunction<Args, Returns>>(func: F) => ReturnType<F> extends Returns["_output"] ? (...args: Args["_input"]) => ReturnType<F> : OuterTypeOfFunction<Args, Returns>;
524
541
  static create: <T extends ZodTuple<any, any> = ZodTuple<[], ZodUnknown>, U extends ZodTypeAny = ZodUnknown>(args?: T | undefined, returns?: U | undefined, params?: RawCreateParams) => ZodFunction<T, U>;
525
542
  }
526
543
  export interface ZodLazyDef<T extends ZodTypeAny = ZodTypeAny> extends ZodTypeDef {
@@ -707,7 +724,7 @@ declare const strictObjectType: <T extends ZodRawShape>(shape: T, params?: RawCr
707
724
  declare const unionType: <T extends readonly [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]>(types: T, params?: RawCreateParams) => ZodUnion<T>;
708
725
  declare const discriminatedUnionType: typeof ZodDiscriminatedUnion.create;
709
726
  declare const intersectionType: <T extends ZodTypeAny, U extends ZodTypeAny>(left: T, right: U, params?: RawCreateParams) => ZodIntersection<T, U>;
710
- declare const tupleType: <T extends [ZodTypeAny, ...ZodTypeAny[]] | []>(schemas: T, params?: RawCreateParams) => ZodTuple<T, null>;
727
+ declare const tupleType: <T extends [] | [ZodTypeAny, ...ZodTypeAny[]]>(schemas: T, params?: RawCreateParams) => ZodTuple<T, null>;
711
728
  declare const recordType: typeof ZodRecord.create;
712
729
  declare const mapType: <Key extends ZodTypeAny = ZodTypeAny, Value extends ZodTypeAny = ZodTypeAny>(keyType: Key, valueType: Value, params?: RawCreateParams) => ZodMap<Key, Value>;
713
730
  declare const setType: <Value extends ZodTypeAny = ZodTypeAny>(valueType: Value, params?: RawCreateParams) => ZodSet<Value>;
package/lib/types.js CHANGED
@@ -41,11 +41,10 @@ function processCreateParams(params) {
41
41
  const customMap = (iss, ctx) => {
42
42
  if (iss.code !== "invalid_type")
43
43
  return { message: ctx.defaultError };
44
- if (typeof ctx.data === "undefined" && required_error)
45
- return { message: required_error };
46
- if (params.invalid_type_error)
47
- return { message: params.invalid_type_error };
48
- return { message: ctx.defaultError };
44
+ if (typeof ctx.data === "undefined") {
45
+ return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
46
+ }
47
+ return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
49
48
  };
50
49
  return { errorMap: customMap, description };
51
50
  }
@@ -793,15 +792,89 @@ class ZodDate extends ZodType {
793
792
  });
794
793
  return parseUtil_1.INVALID;
795
794
  }
795
+ const status = new parseUtil_1.ParseStatus();
796
+ let ctx = undefined;
797
+ for (const check of this._def.checks) {
798
+ if (check.kind === "min") {
799
+ if (input.data.getTime() < check.value) {
800
+ ctx = this._getOrReturnCtx(input, ctx);
801
+ parseUtil_1.addIssueToContext(ctx, {
802
+ code: ZodError_1.ZodIssueCode.too_small,
803
+ message: check.message,
804
+ inclusive: true,
805
+ minimum: check.value,
806
+ type: "date",
807
+ });
808
+ status.dirty();
809
+ }
810
+ }
811
+ else if (check.kind === "max") {
812
+ if (input.data.getTime() > check.value) {
813
+ ctx = this._getOrReturnCtx(input, ctx);
814
+ parseUtil_1.addIssueToContext(ctx, {
815
+ code: ZodError_1.ZodIssueCode.too_big,
816
+ message: check.message,
817
+ inclusive: true,
818
+ maximum: check.value,
819
+ type: "date",
820
+ });
821
+ status.dirty();
822
+ }
823
+ }
824
+ else {
825
+ util_1.util.assertNever(check);
826
+ }
827
+ }
796
828
  return {
797
- status: "valid",
829
+ status: status.value,
798
830
  value: new Date(input.data.getTime()),
799
831
  };
800
832
  }
833
+ _addCheck(check) {
834
+ return new ZodDate({
835
+ ...this._def,
836
+ checks: [...this._def.checks, check],
837
+ });
838
+ }
839
+ min(minDate, message) {
840
+ return this._addCheck({
841
+ kind: "min",
842
+ value: minDate.getTime(),
843
+ message: errorUtil_1.errorUtil.toString(message),
844
+ });
845
+ }
846
+ max(maxDate, message) {
847
+ return this._addCheck({
848
+ kind: "max",
849
+ value: maxDate.getTime(),
850
+ message: errorUtil_1.errorUtil.toString(message),
851
+ });
852
+ }
853
+ get minDate() {
854
+ let min = null;
855
+ for (const ch of this._def.checks) {
856
+ if (ch.kind === "min") {
857
+ if (min === null || ch.value > min)
858
+ min = ch.value;
859
+ }
860
+ }
861
+ return min != null ? new Date(min) : null;
862
+ }
863
+ get maxDate() {
864
+ let max = null;
865
+ for (const ch of this._def.checks) {
866
+ if (ch.kind === "max") {
867
+ if (max === null || ch.value < max)
868
+ max = ch.value;
869
+ }
870
+ }
871
+ return max != null ? new Date(max) : null;
872
+ }
801
873
  }
802
874
  exports.ZodDate = ZodDate;
803
875
  ZodDate.create = (params) => {
804
876
  return new ZodDate({
877
+ checks: [],
805
878
  typeName: ZodFirstPartyTypeKind.ZodDate,
806
879
  ...processCreateParams(params),
807
880
  });
@@ -1298,6 +1371,9 @@ class ZodObject extends ZodType {
1298
1371
  shape: () => newShape,
1299
1372
  });
1300
1373
  }
1374
+ keyof() {
1375
+ return createZodEnum(util_1.util.objectKeys(this.shape));
1376
+ }
1301
1377
  }
1302
1378
  exports.ZodObject = ZodObject;
1303
1379
  ZodObject.create = (shape, params) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod",
3
- "version": "3.17.6",
3
+ "version": "3.17.9",
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",