-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.test.js
192 lines (158 loc) · 3.76 KB
/
schema.test.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const ValidateSchema = require(".");
// return failed
const dataWithRequiredField = {
username: "",
};
const dataWithBooleanField = {
accept: false,
};
const dataWithTypeField = {
select: 123,
};
const dataWithEmailField = {
email: "rppieterser@gmail",
};
const dataWithPhoneField = {
phone: "812 83898",
};
const dataWithMinField = {
password: "12",
};
const dataWithMaxField = {
password: "1256!",
};
const dataWithCustomField = {
password: "1256444",
};
// return success
const dataWithRequiredField2 = {
username: "dfv",
};
const dataWithEmailField2 = {
email: "rppieterser@gmail.com",
};
const dataWithPhoneField2 = {
phone: "0812383898",
};
const dataWithMinField2 = {
password: "12dascear",
};
const dataWithMaxField2 = {
password: "1!",
};
const dataWithCustomField2 = {
password: "12564!44",
};
const dataWithBooleanField2 = {
accept: true,
};
const dataWithTypeField2 = {
select: "hello",
};
// schemas
const validateRequired = new ValidateSchema({
username: {
required: true,
},
});
const validateBoolean = new ValidateSchema({
accept: {
shouldBeTrue: true,
},
});
const validateType = new ValidateSchema({
select: {
isType: "string",
},
});
const validateEmail = new ValidateSchema({
email: {
type: "email",
},
});
const validatePhone = new ValidateSchema({
phone: {
type: "phone",
},
});
const validateMin = new ValidateSchema({
password: {
min: 6,
},
});
const validateMax = new ValidateSchema({
password: {
max: 3,
},
});
const validateCustom = new ValidateSchema({
password: {
custom: (val) => {
return val?.includes("!");
},
},
});
test("Field is required fail", () => {
const test = validateRequired.validate(dataWithRequiredField);
expect(test.success).toBe(false);
});
test("Field is email fail", () => {
const test = validateEmail.validate(dataWithEmailField);
expect(test.success).toBe(false);
});
test("Field is phone fail", () => {
const test = validatePhone.validate(dataWithPhoneField);
expect(test.success).toBe(false);
});
test("Field is min fail", () => {
const test = validateMin.validate(dataWithMinField);
expect(test.success).toBe(false);
});
test("Field is max fail", () => {
const test = validateMax.validate(dataWithMaxField);
expect(test.success).toBe(false);
});
test("Field is custom fail", () => {
const test = validateCustom.validate(dataWithCustomField);
expect(test.success).toBe(false);
});
test("Field is true fail", () => {
const test = validateBoolean.validate(dataWithBooleanField);
expect(test.success).toBe(false);
});
test("Field is string fail", () => {
const test = validateType.validate(dataWithTypeField);
expect(test.success).toBe(false);
});
test("Field is required pass", () => {
const test = validateRequired.validate(dataWithRequiredField2);
expect(test.success).toBe(true);
});
test("Field is email pass", () => {
const test = validateEmail.validate(dataWithEmailField2);
expect(test.success).toBe(true);
});
test("Field is phone pass", () => {
const test = validatePhone.validate(dataWithPhoneField2);
expect(test.success).toBe(true);
});
test("Field is min pass", () => {
const test = validateMin.validate(dataWithMinField2);
expect(test.success).toBe(true);
});
test("Field is max pass", () => {
const test = validateMax.validate(dataWithMaxField2);
expect(test.success).toBe(true);
});
test("Field is custom pass", () => {
const test = validateCustom.validate(dataWithCustomField2);
expect(test.success).toBe(true);
});
test("Field is true pass", () => {
const test = validateBoolean.validate(dataWithBooleanField2);
expect(test.success).toBe(true);
});
test("Field is string pass", () => {
const test = validateType.validate(dataWithTypeField2);
expect(test.success).toBe(true);
});