forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidations.test.ts
More file actions
125 lines (112 loc) · 3.02 KB
/
validations.test.ts
File metadata and controls
125 lines (112 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import * as z from "../index";
test("array min", async () => {
try {
await z.array(z.string()).min(4).parseAsync([]);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Array must contain at least 4 element(s)"
);
}
});
test("array max", async () => {
try {
await z.array(z.string()).max(2).parseAsync(["asdf", "asdf", "asdf"]);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Array must contain at most 2 element(s)"
);
}
});
test("string min", async () => {
try {
await z.string().min(4).parseAsync("asd");
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"String must contain at least 4 character(s)"
);
}
});
test("string max", async () => {
try {
await z.string().max(4).parseAsync("aasdfsdfsd");
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"String must contain at most 4 character(s)"
);
}
});
test("number min", async () => {
try {
await z.number().gte(3).parseAsync(2);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Number must be greater than or equal to 3"
);
}
});
test("number max", async () => {
try {
await z.number().lte(3).parseAsync(4);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Number must be less than or equal to 3"
);
}
});
test("number nonnegative", async () => {
try {
await z.number().nonnegative().parseAsync(-1);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Number must be greater than or equal to 0"
);
}
});
test("number nonpositive", async () => {
try {
await z.number().nonpositive().parseAsync(1);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Number must be less than or equal to 0"
);
}
});
test("number negative", async () => {
try {
await z.number().negative().parseAsync(1);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Number must be less than 0"
);
}
});
test("number positive", async () => {
try {
await z.number().positive().parseAsync(-1);
} catch (err) {
expect((err as z.ZodError).issues[0].message).toEqual(
"Number must be greater than 0"
);
}
});
test("instantiation", () => {
z.string().min(5);
z.string().max(5);
z.string().length(5);
z.string().email();
z.string().url();
z.string().uuid();
z.string().min(5, { message: "Must be 5 or more characters long" });
z.string().max(5, { message: "Must be 5 or fewer characters long" });
z.string().length(5, { message: "Must be exactly 5 characters long" });
z.string().email({ message: "Invalid email address." });
z.string().url({ message: "Invalid url" });
z.string().uuid({ message: "Invalid UUID" });
});
test("int", async () => {
const int = z.number().int();
int.parse(4);
expect(() => int.parse(3.5)).toThrow();
});