1 | var Valida = require('../');
|
2 | var expect = require('chai').expect;
|
3 |
|
4 | describe('validators', function () {
|
5 | describe('required', function () {
|
6 | var schema = {
|
7 | name: [
|
8 | { validator: Valida.Validator.required }
|
9 | ],
|
10 | };
|
11 |
|
12 | describe('given the field is present in the data', function () {
|
13 | it('should consider valid', function (done) {
|
14 | var data = { name: 'Jack' };
|
15 |
|
16 | Valida.process(data, schema, function(err, ctx) {
|
17 | if (err) return done(err);
|
18 | expect(ctx.isValid()).to.eql(true);
|
19 | done();
|
20 | });
|
21 | });
|
22 | });
|
23 |
|
24 | describe('given the field is not present in the data', function () {
|
25 | it('should consider invalid', function (done) {
|
26 | var data = { };
|
27 |
|
28 | Valida.process(data, schema, function(err, ctx) {
|
29 | if (err) return done(err);
|
30 | expect(ctx.isValid()).to.eql(false);
|
31 | done();
|
32 | });
|
33 | });
|
34 | });
|
35 | });
|
36 |
|
37 | describe('empty', function () {
|
38 | var schema = {
|
39 | name: [
|
40 | { validator: Valida.Validator.empty }
|
41 | ],
|
42 | };
|
43 |
|
44 | describe('given the field is not empty', function () {
|
45 | it('should consider valid', function (done) {
|
46 | var data = { name: 'Jack' };
|
47 |
|
48 | Valida.process(data, schema, function(err, ctx) {
|
49 | if (err) return done(err);
|
50 | expect(ctx.isValid()).to.eql(true);
|
51 | done();
|
52 | });
|
53 | });
|
54 | });
|
55 |
|
56 | describe('given the field is empty', function () {
|
57 | it('should consider invalid', function (done) {
|
58 | var data = { name: '' };
|
59 |
|
60 | Valida.process(data, schema, function(err, ctx) {
|
61 | if (err) return done(err);
|
62 | expect(ctx.isValid()).to.eql(false);
|
63 | done();
|
64 | });
|
65 | });
|
66 | });
|
67 | });
|
68 |
|
69 | describe('regex', function () {
|
70 | var schema = {
|
71 | name: [
|
72 | { validator: Valida.Validator.regex, pattern: '[A-Z]', modifiers: 'i' }
|
73 | ],
|
74 | };
|
75 |
|
76 | describe('given the field matches the regex pattern', function () {
|
77 | it('should consider valid', function (done) {
|
78 | var data = { name: 'Jack' };
|
79 |
|
80 | Valida.process(data, schema, function(err, ctx) {
|
81 | if (err) return done(err);
|
82 | expect(ctx.isValid()).to.eql(true);
|
83 | done();
|
84 | });
|
85 | });
|
86 | });
|
87 |
|
88 | describe('given the field does not match the regex pattern', function () {
|
89 | it('should consider invalid', function (done) {
|
90 | var data = { name: '1111' };
|
91 |
|
92 | Valida.process(data, schema, function(err, ctx) {
|
93 | if (err) return done(err);
|
94 | expect(ctx.isValid()).to.eql(false);
|
95 | done();
|
96 | });
|
97 | });
|
98 | });
|
99 | });
|
100 |
|
101 | describe('len', function () {
|
102 | var schema = {
|
103 | fruits: [
|
104 | { validator: Valida.Validator.len, min: 2, max: 3 }
|
105 | ]
|
106 | };
|
107 |
|
108 | describe('given an array that matches the expected length', function () {
|
109 | it('should consider valid', function (done) {
|
110 | var data = { fruits: ['apple', 'orange'] };
|
111 |
|
112 | Valida.process(data, schema, function(err, ctx) {
|
113 | if (err) return done(err);
|
114 | expect(ctx.isValid()).to.eql(true);
|
115 | done();
|
116 | });
|
117 | });
|
118 | });
|
119 |
|
120 | describe('given and array that does not match the expected length', function () {
|
121 | it('should consider invalid when min is not right', function (done) {
|
122 | var data = { fruits: ['apple'] };
|
123 |
|
124 | Valida.process(data, schema, function(err, ctx) {
|
125 | if (err) return done(err);
|
126 | expect(ctx.isValid()).to.eql(false);
|
127 | done();
|
128 | });
|
129 | });
|
130 |
|
131 | it('should consider invalid when max is not right', function (done) {
|
132 | var data = { fruits: ['apple', 'orange', 'banana', 'watermelon'] };
|
133 |
|
134 | Valida.process(data, schema, function(err, ctx) {
|
135 | if (err) return done(err);
|
136 | expect(ctx.isValid()).to.eql(false);
|
137 | done();
|
138 | });
|
139 | });
|
140 | });
|
141 |
|
142 | describe('given a string that matches the expected length', function () {
|
143 | it('should consider valid', function (done) {
|
144 | var data = { fruits: 'fig' };
|
145 |
|
146 | Valida.process(data, schema, function(err, ctx) {
|
147 | if (err) return done(err);
|
148 | expect(ctx.isValid()).to.eql(true);
|
149 | done();
|
150 | });
|
151 | });
|
152 | });
|
153 |
|
154 | describe('given and array that does not match the expected length', function () {
|
155 | it('should consider invalid when min is not right', function (done) {
|
156 | var data = { fruits: 'x' };
|
157 |
|
158 | Valida.process(data, schema, function(err, ctx) {
|
159 | if (err) return done(err);
|
160 | expect(ctx.isValid()).to.eql(false);
|
161 | done();
|
162 | });
|
163 | });
|
164 |
|
165 | it('should consider invalid when max is not right', function (done) {
|
166 | var data = { fruits: 'coconut' };
|
167 |
|
168 | Valida.process(data, schema, function(err, ctx) {
|
169 | if (err) return done(err);
|
170 | expect(ctx.isValid()).to.eql(false);
|
171 | done();
|
172 | });
|
173 | });
|
174 | });
|
175 | });
|
176 |
|
177 | describe('array', function () {
|
178 | var schema = {
|
179 | fruits: [
|
180 | { validator: Valida.Validator.array }
|
181 | ],
|
182 | };
|
183 |
|
184 | describe('given the field is an array', function () {
|
185 | it('should consider valid', function (done) {
|
186 | var data = { fruits: ['orange'] };
|
187 |
|
188 | Valida.process(data, schema, function(err, ctx) {
|
189 | if (err) return done(err);
|
190 | expect(ctx.isValid()).to.eql(true);
|
191 | done();
|
192 | });
|
193 | });
|
194 | });
|
195 |
|
196 | describe('given the field is not an array', function () {
|
197 | it('should consider invalid', function (done) {
|
198 | var data = { fruits: 'orange' };
|
199 |
|
200 | Valida.process(data, schema, function(err, ctx) {
|
201 | if (err) return done(err);
|
202 | expect(ctx.isValid()).to.eql(false);
|
203 | done();
|
204 | });
|
205 | });
|
206 | });
|
207 | });
|
208 |
|
209 | describe('plainObject', function () {
|
210 | var schema = {
|
211 | user: [
|
212 | { validator: Valida.Validator.plainObject }
|
213 | ],
|
214 | };
|
215 |
|
216 | describe('given the field is a plain object', function () {
|
217 | it('should consider valid', function (done) {
|
218 | var data = { user: { name: 'Jack' } };
|
219 |
|
220 | Valida.process(data, schema, function(err, ctx) {
|
221 | if (err) return done(err);
|
222 | expect(ctx.isValid()).to.eql(true);
|
223 | done();
|
224 | });
|
225 | });
|
226 | });
|
227 |
|
228 | describe('given the field is not a plain object', function () {
|
229 | it('should consider invalid', function (done) {
|
230 | var data = { user: [{ name: 'Jack' }] };
|
231 |
|
232 | Valida.process(data, schema, function(err, ctx) {
|
233 | if (err) return done(err);
|
234 | expect(ctx.isValid()).to.eql(false);
|
235 | done();
|
236 | });
|
237 | });
|
238 | });
|
239 | });
|
240 |
|
241 | describe('date', function () {
|
242 | var schema = {
|
243 | createdAt: [
|
244 | { validator: Valida.Validator.date }
|
245 | ],
|
246 | };
|
247 |
|
248 | describe('given the field is a date', function () {
|
249 | it('should consider valid', function (done) {
|
250 | var data = { createdAt: new Date() };
|
251 |
|
252 | Valida.process(data, schema, function(err, ctx) {
|
253 | if (err) return done(err);
|
254 | expect(ctx.isValid()).to.eql(true);
|
255 | done();
|
256 | });
|
257 | });
|
258 | });
|
259 |
|
260 | describe('given the field is not a date', function () {
|
261 | it('should consider invalid', function (done) {
|
262 | var data = { createdAt: 'nope' };
|
263 |
|
264 | Valida.process(data, schema, function(err, ctx) {
|
265 | if (err) return done(err);
|
266 | expect(ctx.isValid()).to.eql(false);
|
267 | done();
|
268 | });
|
269 | });
|
270 | });
|
271 | });
|
272 |
|
273 | describe('integer', function () {
|
274 | var schema = {
|
275 | age: [
|
276 | { validator: Valida.Validator.integer }
|
277 | ],
|
278 | };
|
279 |
|
280 | describe('given the field is a integer', function () {
|
281 | it('should consider valid', function (done) {
|
282 | var data = { age: 45 };
|
283 |
|
284 | Valida.process(data, schema, function(err, ctx) {
|
285 | if (err) return done(err);
|
286 | expect(ctx.isValid()).to.eql(true);
|
287 | done();
|
288 | });
|
289 | });
|
290 | });
|
291 |
|
292 | describe('given the field is not a integer', function () {
|
293 | it('should consider invalid', function (done) {
|
294 | var data = { age: 'nope' };
|
295 |
|
296 | Valida.process(data, schema, function(err, ctx) {
|
297 | if (err) return done(err);
|
298 | expect(ctx.isValid()).to.eql(false);
|
299 | done();
|
300 | });
|
301 | });
|
302 | });
|
303 | });
|
304 |
|
305 | describe('enum', function () {
|
306 | var schema = {
|
307 | fruit: [
|
308 | { validator: Valida.Validator.enum, items: ['apple', 'orange'] }
|
309 | ],
|
310 | };
|
311 |
|
312 | describe('given a value that exists in the enum', function () {
|
313 | it('should consider valid', function (done) {
|
314 | var data = { fruit: 'orange' };
|
315 |
|
316 | Valida.process(data, schema, function(err, ctx) {
|
317 | if (err) return done(err);
|
318 | expect(ctx.isValid()).to.eql(true);
|
319 | done();
|
320 | });
|
321 | });
|
322 | });
|
323 |
|
324 | describe('given a value that does not exist in the enum', function () {
|
325 | it('should consider invalid', function (done) {
|
326 | var data = { fruit: 'watermelon' };
|
327 |
|
328 | Valida.process(data, schema, function(err, ctx) {
|
329 | if (err) return done(err);
|
330 | expect(ctx.isValid()).to.eql(false);
|
331 | done();
|
332 | });
|
333 | });
|
334 | });
|
335 | });
|
336 |
|
337 | describe('bool', function () {
|
338 | var schema = {
|
339 | published: [
|
340 | { validator: Valida.Validator.bool }
|
341 | ],
|
342 | };
|
343 |
|
344 | describe('given a value true of bool type', function () {
|
345 | it('should consider valid', function (done) {
|
346 | var data = { published: true };
|
347 |
|
348 | Valida.process(data, schema, function(err, ctx) {
|
349 | if (err) return done(err);
|
350 | expect(ctx.isValid()).to.eql(true);
|
351 | done();
|
352 | });
|
353 | });
|
354 | });
|
355 |
|
356 | describe('given a value false of bool type', function () {
|
357 | it('should consider valid', function (done) {
|
358 | var data = { published: false };
|
359 |
|
360 | Valida.process(data, schema, function(err, ctx) {
|
361 | if (err) return done(err);
|
362 | expect(ctx.isValid()).to.eql(true);
|
363 | done();
|
364 | });
|
365 | });
|
366 | });
|
367 |
|
368 | describe('given a value true of string type', function () {
|
369 | it('should consider invalid', function (done) {
|
370 | var data = { published: 'true' };
|
371 |
|
372 | Valida.process(data, schema, function(err, ctx) {
|
373 | if (err) return done(err);
|
374 | expect(ctx.isValid()).to.eql(false);
|
375 | done();
|
376 | });
|
377 | });
|
378 | });
|
379 | });
|
380 |
|
381 | describe('float', function () {
|
382 | var schema = {
|
383 | salary: [
|
384 | { validator: Valida.Validator.float }
|
385 | ],
|
386 | };
|
387 |
|
388 | describe('given a value 1.20 of "float" type', function () {
|
389 | it('should consider valid', function (done) {
|
390 | var data = { salary: 1.20 };
|
391 |
|
392 | Valida.process(data, schema, function(err, ctx) {
|
393 | if (err) return done(err);
|
394 | expect(ctx.isValid()).to.eql(true);
|
395 | done();
|
396 | });
|
397 | });
|
398 | });
|
399 |
|
400 | describe('given a value 1.20 of string type', function () {
|
401 | it('should consider invalid', function (done) {
|
402 | var data = { salary: '1.20' };
|
403 |
|
404 | Valida.process(data, schema, function(err, ctx) {
|
405 | if (err) return done(err);
|
406 | expect(ctx.isValid()).to.eql(false);
|
407 | done();
|
408 | });
|
409 | });
|
410 | });
|
411 | });
|
412 |
|
413 | describe('range', function () {
|
414 | describe('given a schema with both min and max values', () => {
|
415 | var schema = {
|
416 | code: [
|
417 | { validator: Valida.Validator.range, min: 0, max: 10 }
|
418 | ],
|
419 | };
|
420 |
|
421 | describe('given a value smaller than the min value', () => {
|
422 | it ('should consider invalid', (done) => {
|
423 | var data = { code: -10 };
|
424 |
|
425 | Valida.process(data, schema, function(err, ctx) {
|
426 | if (err) return done(err);
|
427 | expect(ctx.isValid()).to.eql(false);
|
428 | done();
|
429 | });
|
430 | });
|
431 | });
|
432 |
|
433 | describe('given a value bigger than the max value', () => {
|
434 | it ('should consider invalid', (done) => {
|
435 | var data = { code: 20 };
|
436 |
|
437 | Valida.process(data, schema, function(err, ctx) {
|
438 | if (err) return done(err);
|
439 | expect(ctx.isValid()).to.eql(false);
|
440 | done();
|
441 | });
|
442 | });
|
443 | });
|
444 |
|
445 | describe('given a value between the min and max value', () => {
|
446 | it ('should consider valid', (done) => {
|
447 | var data = { code: 5 };
|
448 |
|
449 | Valida.process(data, schema, function(err, ctx) {
|
450 | if (err) return done(err);
|
451 | expect(ctx.isValid()).to.eql(true);
|
452 | done();
|
453 | });
|
454 | });
|
455 | });
|
456 |
|
457 | describe('given a value equal to the min value', () => {
|
458 | it ('should consider valid', (done) => {
|
459 | var data = { code: 0 };
|
460 |
|
461 | Valida.process(data, schema, function(err, ctx) {
|
462 | if (err) return done(err);
|
463 | expect(ctx.isValid()).to.eql(true);
|
464 | done();
|
465 | });
|
466 | });
|
467 | });
|
468 |
|
469 | describe('given a value equal to the max value', () => {
|
470 | it ('should consider valid', (done) => {
|
471 | var data = { code: 10 };
|
472 |
|
473 | Valida.process(data, schema, function(err, ctx) {
|
474 | if (err) return done(err);
|
475 | expect(ctx.isValid()).to.eql(true);
|
476 | done();
|
477 | });
|
478 | });
|
479 | });
|
480 | });
|
481 |
|
482 | describe('given a schema with only min value', () => {
|
483 | var schema = {
|
484 | code: [
|
485 | { validator: Valida.Validator.range, min: 0 }
|
486 | ],
|
487 | };
|
488 |
|
489 | describe('given a value smaller than the min value', () => {
|
490 | it ('should consider invalid', (done) => {
|
491 | var data = { code: -10 };
|
492 |
|
493 | Valida.process(data, schema, function(err, ctx) {
|
494 | if (err) return done(err);
|
495 | expect(ctx.isValid()).to.eql(false);
|
496 | done();
|
497 | });
|
498 | });
|
499 | });
|
500 |
|
501 | describe('given a value bigger than the min value', () => {
|
502 | it ('should consider valid', (done) => {
|
503 | var data = { code: 5 };
|
504 |
|
505 | Valida.process(data, schema, function(err, ctx) {
|
506 | if (err) return done(err);
|
507 | expect(ctx.isValid()).to.eql(true);
|
508 | done();
|
509 | });
|
510 | });
|
511 | });
|
512 |
|
513 | describe('given a value equal to the min value', () => {
|
514 | it ('should consider valid', (done) => {
|
515 | var data = { code: 0 };
|
516 |
|
517 | Valida.process(data, schema, function(err, ctx) {
|
518 | if (err) return done(err);
|
519 | expect(ctx.isValid()).to.eql(true);
|
520 | done();
|
521 | });
|
522 | });
|
523 | });
|
524 | });
|
525 |
|
526 | describe('given a schema with only max value', () => {
|
527 | var schema = {
|
528 | code: [
|
529 | { validator: Valida.Validator.range, max: 10 }
|
530 | ],
|
531 | };
|
532 |
|
533 | describe('given a value bigger than the max value', () => {
|
534 | it ('should consider invalid', (done) => {
|
535 | var data = { code: 20 };
|
536 |
|
537 | Valida.process(data, schema, function(err, ctx) {
|
538 | if (err) return done(err);
|
539 | expect(ctx.isValid()).to.eql(false);
|
540 | done();
|
541 | });
|
542 | });
|
543 | });
|
544 |
|
545 | describe('given a value smaller than the max value', () => {
|
546 | it ('should consider valid', (done) => {
|
547 | var data = { code: 5 };
|
548 |
|
549 | Valida.process(data, schema, function(err, ctx) {
|
550 | if (err) return done(err);
|
551 | expect(ctx.isValid()).to.eql(true);
|
552 | done();
|
553 | });
|
554 | });
|
555 | });
|
556 |
|
557 | describe('given a value equal to the max value', () => {
|
558 | it ('should consider valid', (done) => {
|
559 | var data = { code: 10 };
|
560 |
|
561 | Valida.process(data, schema, function(err, ctx) {
|
562 | if (err) return done(err);
|
563 | expect(ctx.isValid()).to.eql(true);
|
564 | done();
|
565 | });
|
566 | });
|
567 | });
|
568 | });
|
569 | });
|
570 | });
|