forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidations.test.ts
106 lines (95 loc) · 2.48 KB
/
validations.test.ts
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
import * as z from '../index';
test('array min', () => {
z.array(z.string())
.min(4)
.parseAsync([])
.catch(err => {
expect(err.errors[0].message).toEqual('Should have at least 4 items');
});
});
test('array max', () => {
z.array(z.string())
.max(2)
.parseAsync(['asdf', 'asdf', 'asdf'])
.catch(err => {
expect(err.errors[0].message).toEqual('Should have at most 2 items');
});
});
test('string min', () => {
z.string()
.min(4)
.parseAsync('asd')
.catch(err => {
expect(err.errors[0].message).toEqual('Should be at least 4 characters');
});
});
test('string max', () => {
z.string()
.max(4)
.parseAsync('aasdfsdfsd')
.catch(err => {
expect(err.errors[0].message).toEqual('Should be at most 4 characters long');
});
});
test('number min', () => {
z.number()
.min(3)
.parseAsync(2)
.catch(err => {
expect(err.errors[0].message).toEqual('Value should be greater than or equal to 3');
});
});
test('number max', () => {
z.number()
.max(3)
.parseAsync(4)
.catch(err => {
expect(err.errors[0].message).toEqual('Value should be less than or equal to 3');
});
});
test('number nonnegative', () => {
z.number()
.nonnegative()
.parseAsync(-1)
.catch(err => {
expect(err.errors[0].message).toEqual('Value should be greater than or equal to 0');
});
});
test('number nonpositive', () => {
z.number()
.nonpositive()
.parseAsync(1)
.catch(err => {
expect(err.errors[0].message).toEqual('Value should be less than or equal to 0');
});
});
test('number negative', () => {
z.number()
.negative()
.parseAsync(1)
.catch(err => {
expect(err.errors[0].message).toEqual('Value should be less than 0');
});
});
test('number positive', () => {
z.number()
.positive()
.parseAsync(-1)
.catch(err => {
expect(err.errors[0].message).toEqual('Value should 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' });
});