1 |
|
2 |
|
3 |
|
4 | var Validator = require('../lib/modelValidator');
|
5 | var validator = new Validator();
|
6 |
|
7 | module.exports.validationTests = {
|
8 | invalidIntegerTypeTest: function(test) {
|
9 | var data = {
|
10 | id: 'sample'
|
11 | };
|
12 | var model = {
|
13 | required: [ 'id' ],
|
14 | properties: {
|
15 | id: {
|
16 | type: 'integer',
|
17 | description: 'The object id'
|
18 | }
|
19 | }
|
20 | };
|
21 |
|
22 | var errors = validator.validate(data, model);
|
23 |
|
24 | test.expect(2);
|
25 | test.ok(!errors.valid);
|
26 | test.ok(errors.errors[0].message === 'id (sample) is not a type of integer', 'message: ' + errors.errors[0].message);
|
27 |
|
28 | test.done();
|
29 | },
|
30 | invalidIntegerBlankTest: function(test) {
|
31 | var data = {
|
32 | id: ""
|
33 | };
|
34 | var model = {
|
35 | required: [],
|
36 | properties: {
|
37 | id: {
|
38 | type: 'integer',
|
39 | description: 'The object id'
|
40 | }
|
41 | }
|
42 | };
|
43 |
|
44 | var errors = validator.validate(data, model);
|
45 |
|
46 | test.expect(2);
|
47 | test.ok(!errors.valid);
|
48 | test.ok(errors.errors[0].message === 'id ({empty string}) is not a type of integer', errors.errors[0].message);
|
49 |
|
50 | test.done();
|
51 | },
|
52 | invalidIntegerBlankWhenRequiredTest: function(test) {
|
53 | var data = {
|
54 | id: ""
|
55 | };
|
56 | var model = {
|
57 | required: ['id'],
|
58 | properties: {
|
59 | id: {
|
60 | type: 'integer',
|
61 | description: 'The object id'
|
62 | }
|
63 | }
|
64 | };
|
65 |
|
66 | var errors = validator.validate(data, model);
|
67 |
|
68 | test.expect(2);
|
69 | test.ok(!errors.valid);
|
70 | test.ok(errors.errors[0].message === 'id is a required field');
|
71 |
|
72 | test.done();
|
73 | },
|
74 | validIntegerTypeTest: function(test) {
|
75 | var data = {
|
76 | id: 1
|
77 | };
|
78 | var model = {
|
79 | required: [ 'id' ],
|
80 | properties: {
|
81 | id: {
|
82 | type: 'integer',
|
83 | description: 'The object id'
|
84 | }
|
85 | }
|
86 | };
|
87 |
|
88 | var errors = validator.validate(data, model);
|
89 |
|
90 | test.expect(1);
|
91 | test.ok(errors.valid);
|
92 |
|
93 | test.done();
|
94 | },
|
95 | validInteger32TypeTest: function(test) {
|
96 | var data = {
|
97 | id: 1
|
98 | };
|
99 | var model = {
|
100 | required: [ 'id' ],
|
101 | properties: {
|
102 | id: {
|
103 | type: 'integer',
|
104 | description: 'The object id',
|
105 | format: 'int32'
|
106 | }
|
107 | }
|
108 | };
|
109 |
|
110 | var errors = validator.validate(data, model);
|
111 |
|
112 | test.expect(1);
|
113 | test.ok(errors.valid);
|
114 |
|
115 | test.done();
|
116 | },
|
117 | validInteger32TypeFailedTest: function(test) {
|
118 | var data = {
|
119 | id: 3000000000
|
120 | };
|
121 | var model = {
|
122 | required: [ 'id' ],
|
123 | properties: {
|
124 | id: {
|
125 | type: 'integer',
|
126 | description: 'The object id',
|
127 | format: 'int32'
|
128 | }
|
129 | }
|
130 | };
|
131 |
|
132 | var errors = validator.validate(data, model);
|
133 |
|
134 | test.expect(1);
|
135 | test.ok(!errors.valid);
|
136 |
|
137 | test.done();
|
138 | },
|
139 | validInteger64TypeTest: function(test) {
|
140 | var data = {
|
141 | id: 3000000000
|
142 | };
|
143 | var model = {
|
144 | required: [ 'id' ],
|
145 | properties: {
|
146 | id: {
|
147 | type: 'integer',
|
148 | description: 'The object id',
|
149 | format: 'int64'
|
150 | }
|
151 | }
|
152 | };
|
153 |
|
154 | var errors = validator.validate(data, model);
|
155 |
|
156 | test.expect(1);
|
157 | test.ok(errors.valid);
|
158 |
|
159 | test.done();
|
160 | },
|
161 | testdecimalcalcFails: function(test) {
|
162 | var v1 = 3.41111111;
|
163 | var v2 = 1.22222222;
|
164 |
|
165 | var result = v1 + v2;
|
166 |
|
167 | test.expect(1);
|
168 | test.ok(result !== 4.63333333, result);
|
169 | test.done();
|
170 | },
|
171 | validIntegerMinimumExceededTest: function(test) {
|
172 | var data = {
|
173 | id: 300
|
174 | };
|
175 | var model = {
|
176 | required: [ 'id' ],
|
177 | properties: {
|
178 | id: {
|
179 | type: 'integer',
|
180 | description: 'The object id',
|
181 | format: 'int32',
|
182 | minimum: 2400
|
183 | }
|
184 | }
|
185 | };
|
186 |
|
187 | var errors = validator.validate(data, model);
|
188 |
|
189 | test.expect(1);
|
190 | test.ok(!errors.valid);
|
191 |
|
192 | test.done();
|
193 | },
|
194 | validIntegerMinimumTest: function(test) {
|
195 | var data = {
|
196 | id: 300
|
197 | };
|
198 | var model = {
|
199 | required: [ 'id' ],
|
200 | properties: {
|
201 | id: {
|
202 | type: 'integer',
|
203 | description: 'The object id',
|
204 | format: 'int32',
|
205 | minimum: 300
|
206 | }
|
207 | }
|
208 | };
|
209 |
|
210 | var errors = validator.validate(data, model);
|
211 |
|
212 | test.expect(1);
|
213 | test.ok(errors.valid);
|
214 |
|
215 | test.done();
|
216 | },
|
217 | validIntegerMaxiumumExceededTest: function(test) {
|
218 | var data = {
|
219 | id: 300
|
220 | };
|
221 | var model = {
|
222 | required: [ 'id' ],
|
223 | properties: {
|
224 | id: {
|
225 | type: 'integer',
|
226 | description: 'The object id',
|
227 | format: 'int32',
|
228 | maximum: 24
|
229 | }
|
230 | }
|
231 | };
|
232 |
|
233 | var errors = validator.validate(data, model);
|
234 |
|
235 | test.expect(1);
|
236 | test.ok(!errors.valid);
|
237 |
|
238 | test.done();
|
239 | },
|
240 | validIntegerMaxiumumTest: function(test) {
|
241 | var data = {
|
242 | id: 300
|
243 | };
|
244 | var model = {
|
245 | required: [ 'id' ],
|
246 | properties: {
|
247 | id: {
|
248 | type: 'integer',
|
249 | description: 'The object id',
|
250 | format: 'int32',
|
251 | maximum: 300
|
252 | }
|
253 | }
|
254 | };
|
255 |
|
256 | var errors = validator.validate(data, model);
|
257 |
|
258 | test.expect(1);
|
259 | test.ok(errors.valid);
|
260 |
|
261 | test.done();
|
262 | },
|
263 | validIntegerMinimumExclusiveExceededTest: function(test) {
|
264 | var data = {
|
265 | id: 300
|
266 | };
|
267 | var model = {
|
268 | required: [ 'id' ],
|
269 | properties: {
|
270 | id: {
|
271 | type: 'integer',
|
272 | description: 'The object id',
|
273 | format: 'int32',
|
274 | exclusiveMinimum: 300
|
275 | }
|
276 | }
|
277 | };
|
278 |
|
279 | var errors = validator.validate(data, model);
|
280 |
|
281 | test.expect(1);
|
282 | test.ok(!errors.valid);
|
283 |
|
284 | test.done();
|
285 | },
|
286 | validIntegerMaximumExclusiveExceededTest: function(test) {
|
287 | var data = {
|
288 | id: 300
|
289 | };
|
290 | var model = {
|
291 | required: [ 'id' ],
|
292 | properties: {
|
293 | id: {
|
294 | type: 'integer',
|
295 | description: 'The object id',
|
296 | format: 'int32',
|
297 | exclusiveMaximum: 300
|
298 | }
|
299 | }
|
300 | };
|
301 |
|
302 | var errors = validator.validate(data, model);
|
303 |
|
304 | test.expect(1);
|
305 | test.ok(!errors.valid);
|
306 |
|
307 | test.done();
|
308 | },
|
309 | validIntegerMinimumExclusiveTest: function(test) {
|
310 | var data = {
|
311 | id: 301
|
312 | };
|
313 | var model = {
|
314 | required: [ 'id' ],
|
315 | properties: {
|
316 | id: {
|
317 | type: 'integer',
|
318 | description: 'The object id',
|
319 | format: 'int32',
|
320 | exclusiveMinimum: 300
|
321 | }
|
322 | }
|
323 | };
|
324 |
|
325 | var errors = validator.validate(data, model);
|
326 |
|
327 | test.expect(1);
|
328 | test.ok(errors.valid);
|
329 |
|
330 | test.done();
|
331 | },
|
332 | validIntegerMaximumExclusiveTest: function(test) {
|
333 | var data = {
|
334 | id: 299
|
335 | };
|
336 | var model = {
|
337 | required: [ 'id' ],
|
338 | properties: {
|
339 | id: {
|
340 | type: 'integer',
|
341 | description: 'The object id',
|
342 | format: 'int32',
|
343 | exclusiveMinimum: 300
|
344 | }
|
345 | }
|
346 | };
|
347 |
|
348 | var errors = validator.validate(data, model);
|
349 |
|
350 | test.expect(1);
|
351 | test.ok(!errors.valid);
|
352 |
|
353 | test.done();
|
354 | }
|
355 | }; |
\ | No newline at end of file |