zod 3.17.7 → 3.17.10
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 +32 -4
- package/lib/ZodError.d.ts +2 -2
- package/lib/ZodError.js +5 -1
- package/lib/helpers/enumUtil.d.ts +8 -0
- package/lib/helpers/enumUtil.js +2 -0
- package/lib/index.mjs +87 -7
- package/lib/index.umd.js +87 -7
- package/lib/types.d.ts +19 -2
- package/lib/types.js +82 -6
- package/package.json +1 -1
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()
|
|
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
|
-
|
|
561
|
+
You can customize certain error messages when creating a date 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.
|
|
@@ -1676,7 +1704,7 @@ const Strings = z
|
|
|
1676
1704
|
To transform data after parsing, use the `transform` method.
|
|
1677
1705
|
|
|
1678
1706
|
```ts
|
|
1679
|
-
const stringToNumber = z.string().transform((val) =>
|
|
1707
|
+
const stringToNumber = z.string().transform((val) => val.length);
|
|
1680
1708
|
stringToNumber.parse("string"); // => 6
|
|
1681
1709
|
```
|
|
1682
1710
|
|
|
@@ -1772,7 +1800,7 @@ z.optional(z.string());
|
|
|
1772
1800
|
|
|
1773
1801
|
### `.nullable`
|
|
1774
1802
|
|
|
1775
|
-
A convenience method that returns
|
|
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
|
@@ -171,7 +171,7 @@ const defaultErrorMap = (issue, _ctx) => {
|
|
|
171
171
|
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
172
172
|
}
|
|
173
173
|
else if ("endsWith" in issue.validation) {
|
|
174
|
-
message = `Invalid input: must
|
|
174
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
175
175
|
}
|
|
176
176
|
else {
|
|
177
177
|
util_1.util.assertNever(issue.validation);
|
|
@@ -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
|
+
}
|
package/lib/index.mjs
CHANGED
|
@@ -285,7 +285,7 @@ const defaultErrorMap = (issue, _ctx) => {
|
|
|
285
285
|
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
286
286
|
}
|
|
287
287
|
else if ("endsWith" in issue.validation) {
|
|
288
|
-
message = `Invalid input: must
|
|
288
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
289
289
|
}
|
|
290
290
|
else {
|
|
291
291
|
util.assertNever(issue.validation);
|
|
@@ -305,6 +305,8 @@ const defaultErrorMap = (issue, _ctx) => {
|
|
|
305
305
|
message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
306
306
|
else if (issue.type === "number")
|
|
307
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)}`;
|
|
308
310
|
else
|
|
309
311
|
message = "Invalid input";
|
|
310
312
|
break;
|
|
@@ -315,6 +317,8 @@ const defaultErrorMap = (issue, _ctx) => {
|
|
|
315
317
|
message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
316
318
|
else if (issue.type === "number")
|
|
317
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)}`;
|
|
318
322
|
else
|
|
319
323
|
message = "Invalid input";
|
|
320
324
|
break;
|
|
@@ -486,11 +490,10 @@ function processCreateParams(params) {
|
|
|
486
490
|
const customMap = (iss, ctx) => {
|
|
487
491
|
if (iss.code !== "invalid_type")
|
|
488
492
|
return { message: ctx.defaultError };
|
|
489
|
-
if (typeof ctx.data === "undefined"
|
|
490
|
-
return { message: required_error };
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
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 };
|
|
494
497
|
};
|
|
495
498
|
return { errorMap: customMap, description };
|
|
496
499
|
}
|
|
@@ -1231,14 +1234,88 @@ class ZodDate extends ZodType {
|
|
|
1231
1234
|
});
|
|
1232
1235
|
return INVALID;
|
|
1233
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
|
+
}
|
|
1234
1270
|
return {
|
|
1235
|
-
status:
|
|
1271
|
+
status: status.value,
|
|
1236
1272
|
value: new Date(input.data.getTime()),
|
|
1237
1273
|
};
|
|
1238
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
|
+
}
|
|
1239
1315
|
}
|
|
1240
1316
|
ZodDate.create = (params) => {
|
|
1241
1317
|
return new ZodDate({
|
|
1318
|
+
checks: [],
|
|
1242
1319
|
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
1243
1320
|
...processCreateParams(params),
|
|
1244
1321
|
});
|
|
@@ -1727,6 +1804,9 @@ class ZodObject extends ZodType {
|
|
|
1727
1804
|
shape: () => newShape,
|
|
1728
1805
|
});
|
|
1729
1806
|
}
|
|
1807
|
+
keyof() {
|
|
1808
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
1809
|
+
}
|
|
1730
1810
|
}
|
|
1731
1811
|
ZodObject.create = (shape, params) => {
|
|
1732
1812
|
return new ZodObject({
|
package/lib/index.umd.js
CHANGED
|
@@ -291,7 +291,7 @@
|
|
|
291
291
|
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
292
292
|
}
|
|
293
293
|
else if ("endsWith" in issue.validation) {
|
|
294
|
-
message = `Invalid input: must
|
|
294
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
295
295
|
}
|
|
296
296
|
else {
|
|
297
297
|
util.assertNever(issue.validation);
|
|
@@ -311,6 +311,8 @@
|
|
|
311
311
|
message = `String must contain ${issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
312
312
|
else if (issue.type === "number")
|
|
313
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)}`;
|
|
314
316
|
else
|
|
315
317
|
message = "Invalid input";
|
|
316
318
|
break;
|
|
@@ -321,6 +323,8 @@
|
|
|
321
323
|
message = `String must contain ${issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
322
324
|
else if (issue.type === "number")
|
|
323
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)}`;
|
|
324
328
|
else
|
|
325
329
|
message = "Invalid input";
|
|
326
330
|
break;
|
|
@@ -492,11 +496,10 @@
|
|
|
492
496
|
const customMap = (iss, ctx) => {
|
|
493
497
|
if (iss.code !== "invalid_type")
|
|
494
498
|
return { message: ctx.defaultError };
|
|
495
|
-
if (typeof ctx.data === "undefined"
|
|
496
|
-
return { message: required_error };
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
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 };
|
|
500
503
|
};
|
|
501
504
|
return { errorMap: customMap, description };
|
|
502
505
|
}
|
|
@@ -1237,14 +1240,88 @@
|
|
|
1237
1240
|
});
|
|
1238
1241
|
return INVALID;
|
|
1239
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
|
+
}
|
|
1240
1276
|
return {
|
|
1241
|
-
status:
|
|
1277
|
+
status: status.value,
|
|
1242
1278
|
value: new Date(input.data.getTime()),
|
|
1243
1279
|
};
|
|
1244
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
|
+
}
|
|
1245
1321
|
}
|
|
1246
1322
|
ZodDate.create = (params) => {
|
|
1247
1323
|
return new ZodDate({
|
|
1324
|
+
checks: [],
|
|
1248
1325
|
typeName: exports.ZodFirstPartyTypeKind.ZodDate,
|
|
1249
1326
|
...processCreateParams(params),
|
|
1250
1327
|
});
|
|
@@ -1733,6 +1810,9 @@
|
|
|
1733
1810
|
shape: () => newShape,
|
|
1734
1811
|
});
|
|
1735
1812
|
}
|
|
1813
|
+
keyof() {
|
|
1814
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
1815
|
+
}
|
|
1736
1816
|
}
|
|
1737
1817
|
ZodObject.create = (shape, params) => {
|
|
1738
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[]]
|
|
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;
|
|
@@ -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[]]
|
|
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"
|
|
45
|
-
return { message: required_error };
|
|
46
|
-
|
|
47
|
-
|
|
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:
|
|
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) => {
|