Repository navigation
Expand file tree
/
Copy pathtest_validator.js
More file actions
235 lines (181 loc) · 5.74 KB
/
Copy pathtest_validator.js
File metadata and controls
235 lines (181 loc) · 5.74 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
(function (global) {
var validate = global.validate;
function applyArgs(arrayOfArgs) {
// default options
var matcher = applyArgs.matcher !== undefined ? applyArgs.matcher : "toBe",
// wrap validate invocation in an anonymous function to catch exceptions
wrap = applyArgs.wrap !== undefined ? applyArgs.wrap : false,
// put not before the matcher
not = applyArgs.not !== undefined ? applyArgs.not : false;
arrayOfArgs.forEach(function (args) {
var expectedResult = args.slice(-1)[0],
validatorArgs = args.slice(0, -1);
invocation = wrap ?
function () { validate.apply(null, validatorArgs); } :
validate.apply(null, validatorArgs);
var result;
if (not) {
result = !wrap ?
result = expect(invocation).not[matcher](expectedResult) :
result = expect(invocation).not[matcher]();
} else {
result = !wrap ?
result = expect(invocation)[matcher](expectedResult) :
result = expect(invocation)[matcher]();
}
});
// reset defaults
delete applyArgs.matcher;
delete applyArgs.wrap;
delete applyArgs.not;
}
describe("base validators", function () {
// Base validators
it("validates typeIsNumber", function () {
applyArgs([
[5.2, "typeIsNumber", true],
["2", "typeIsNumber", false],
]);
});
it("validates typeIsString", function () {
applyArgs([
["hello", "typeIsString", true],
["2", "typeIsString", true],
[2, "typeIsString", false],
]);
});
it("validates typeIsObject", function () {
applyArgs([
[null, "typeIsObject", true],
[{}, "typeIsObject", true],
[new Array(2), "typeIsObject", true],
[[1,2], "typeIsObject", true],
["hello", "typeIsObject", false],
]);
});
it("validates notNull", function () {
applyArgs([
["hello", "notNull", true],
["", "notNull", true],
[[], "notNull", true],
[{}, "notNull", true],
[null, "notNull", false],
]);
});
it("validates isArray", function () {
applyArgs([
[[], "isArray", true],
[[{}], "isArray", true],
[new Array(3), "isArray", true],
["hello", "isArray", false],
[2, "isArray", false],
]);
});
it("validates isNaN", function () {
applyArgs([
[NaN, "isNaN", true],
[[], "isNaN", false],
[5, "isNaN", false],
["5", "isNaN", false],
]);
});
}); // end base validators
describe("property validators", function () {
it("validates (strict) min/max", function () {
applyArgs([
["5", "!null", {max: 12}, true],
[16, "number", {min: 12}, true],
[12, "number", {min: 12}, true],
[13, "number", {strictMin: 12}, true],
[-1, "number", {strictMax: 0}, true],
[12, "number", {strictMin: 12}, false],
[0, "number", {strictMax: 0}, false],
[20, "!null, !boolean", {max: 12}, false],
]);
});
it("validates (strict) minLength/maxLength", function () {
applyArgs([
["hello", "string", {maxLength: 20}, true],
["hello", "string", {strictMaxLength: 6}, true],
["a", "string", {minLength: 2}, false],
["ab", "string", {strictMinLength: 2}, false],
["hello", "string", {strictMaxLength: 5}, false],
["hello", "string", {strictMaxLength: 4}, false],
["a", "string", {minLength: 2}, false],
["ab", "string", {strictMinLength: 2}, false],
]);
});
}); // end property validators
describe("composite validators", function () {
// Composite validators
it("validates objects", function () {
applyArgs([
[{a: "a"}, "isNonEmptyObject", true],
[{}, "isNonEmptyObject", false],
[null, "isNonEmptyObject", false],
[["a", 2], "isNonEmptyObject", false],
]);
});
it("validates arrays", function () {
applyArgs([
[[5, "abc"], "isNonEmptyArray", true],
[[], "isNonEmptyArray", false],
]);
});
it("validates strings", function () {
applyArgs([
["test", "isNonEmptyString", true],
["", "isNonEmptyString", false],
[" ", "isNonEmptyString", false],
]);
});
}); // end composite validators
describe("JSON validators", function () {
it("validates eachElement object", function () {
applyArgs([
[{a: 9, b: 3}, "eachElement", {rules: "validNumber"}, true],
[{a: NaN, b: 3}, "eachElement", {rules: "validNumber"}, false],
]);
});
it("validates eachElement on array", function () {
applyArgs([
[["a", 9, "b", 3], "eachElement", {rules: "!null"}, true],
[["0xFF", 9, 2.5, 3], "eachElement", {rules: "validNumeric"}, true],
[["a", 9, "b", 3], "eachElement", {rules: "validNumber"}, false],
[["0xFF", 9, 2.5, 3], "eachElement", {rules: "validIntegerish"}, false],
]);
});
it("validates eachElementProperty on object", function () {
applyArgs([
[{row1: {id: 9}, row2: {id: 22}}, "eachElementProperty", {propertyName: "id", rules: "validInteger"}, true],
[{row1: {id: 9}, row2: {id: 2.2}}, "eachElementProperty", {propertyName: "id", rules: "validInteger"}, false],
]);
});
it("validates eachElementProperty on array", function () {
applyArgs([
[[{b: 9}, {b: 2.2}], "eachElementProperty", {propertyName: "b", rules: "validNumber"}, true],
[[{b: 9}, {b: 2.2}], "eachElementProperty", {propertyName: "b", rules: "validInteger"}, false],
]);
});
}); // end JSON validators
describe("validate() options", function () {
// Optional parameters
it("throws error", function () {
applyArgs.wrap = true;
applyArgs.matcher = "toThrow";
applyArgs([
[[5], "number", "number of requests", undefined],
["5", "!string", "data", undefined],
]);
});
it("does not throw error", function () {
applyArgs.wrap = true;
applyArgs.matcher = "toThrow";
applyArgs.not = true;
applyArgs([
[5, "number", "number of requests", undefined],
[[5], "!string", "data", undefined],
]);
});
}); // end validate options
} (this));