This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
/
validator.spec.js
812 lines (702 loc) · 19.6 KB
/
validator.spec.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
'use strict'
/**
* indicative
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/
const test = require('japa')
const Validator = require('../src/Validator')
const Rule = require('../src/Rule')
const Formatters = require('../src/Formatters')
test.group('Validator', function (group) {
group.before(() => {
Formatters.register('vanilla', require('../src/Formatters/Vanilla'))
Formatters.default('vanilla')
})
group.after(() => {
Formatters.list.clear()
})
// ////////////////
// test suite 1 //
// ////////////////
test('should validate an object of rules', async function (assert) {
const rules = {
username: 'required'
}
const body = {
}
try {
const passed = await Validator.validate(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'required')
}
})
// ////////////////
// test suite 2 //
// ////////////////
test('should validate multiple rules on same field @current', async function (assert) {
const rules = {
username: 'alpha|alphaNumeric'
}
const body = {
username: 'aman@33$'
}
try {
const passed = await Validator.validateAll(body, rules)
assert.notExists(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'alpha')
assert.equal(e[1].field, 'username')
assert.equal(e[1].validation, 'alphaNumeric')
}
})
// ////////////////
// test suite 3 //
// ////////////////
test('should run all validations defined under rules object', async function (assert) {
const rules = {
age: 'required',
phone: 'required'
}
const body = {
}
try {
const passed = await Validator.validateAll(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'age')
assert.equal(e[0].validation, 'required')
assert.equal(e[1].field, 'phone')
assert.equal(e[1].validation, 'required')
}
})
// ////////////////
// test suite 4 //
// ////////////////
test('should return custom messages instead of default messages', async function (assert) {
const rules = {
age: 'required',
phone: 'required'
}
const body = {
}
const messages = {
'age.required': 'Age is required',
'phone.required': function () {
return 'Phone number is required for validations'
}
}
try {
const passed = await Validator.validateAll(body, rules, messages)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].message, messages['age.required'])
assert.equal(e[1].message, messages['phone.required']())
}
})
// ////////////////
// test suite 5 //
// ////////////////
test('should return original data when validation passes', async function (assert) {
const rules = {
age: 'required',
phone: 'required'
}
const body = {
age: 22,
phone: 9192910200
}
const validated = await Validator.validate(body, rules)
assert.equal(validated, body)
})
// ////////////////
// test suite 6 //
// ////////////////
test('should return original data when validation passes using validate method', async function (assert) {
const rules = {
age: 'required',
phone: 'required'
}
const body = {
age: 22,
phone: 9192910200
}
const validated = await Validator.validate(body, rules)
assert.equal(validated, body)
})
// ////////////////
// test suite 7 //
// ////////////////
test('should return errors thrown within validation cycle', async function (assert) {
const rules = {
age: 'foo',
phone: 'required'
}
const body = {
age: 22,
phone: 9192910200
}
try {
const validated = await Validator.validate(body, rules)
assert.notExist(validated)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].validation, 'ENGINE_EXCEPTION')
assert.equal(e[0].message, 'foo is not defined as a validation')
}
})
// ////////////////
// test suite 8 //
// ////////////////
test('should be able to add it\'s own rules to validation store', async function (assert) {
const phone = function (data, field, message, args, get) {
return new Promise(function (resolve, reject) {
reject(message)
})
}
Validator.extend('phone', phone, 'Enter valid phone number')
const rules = {
contact_no: 'phone'
}
const body = {}
try {
const validated = await Validator.validate(body, rules)
assert.notExist(validated)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].validation, 'phone')
assert.equal(e[0].message, 'Enter valid phone number')
}
})
// ////////////////
// test suite 9 //
// ////////////////
test('should return original data when validation passes using validateAll method', async function (assert) {
const rules = {
age: 'required|integer',
phone: 'required'
}
const body = {
age: 22,
phone: 9192910200
}
const validated = await Validator.validateAll(body, rules)
assert.equal(validated, body)
})
// /////////////////
// test suite 10 //
// /////////////////
test('should not validate multiple rules when using validate method', async function (assert) {
const rules = {
username: 'alpha|alphaNumeric'
}
const body = {
username: 'aman@33$'
}
try {
const passed = await Validator.validate(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'alpha')
assert.equal(e[1], undefined)
}
})
// /////////////////
// test suite 11 //
// /////////////////
test('should throw errors when valid function is not passed to extend method', async function (assert) {
const fn = function () {
return Validator.extend('phone', '', '')
}
assert.throws(fn, /Invalid arguments/)
})
// /////////////////
// test suite 12 //
// /////////////////
test('should extend raw validator', function (assert) {
const presence = function (hash, item) {
return hash[item]
}
Validator.is.extend('presence', presence)
const isPresent = Validator.is.presence({foo: 'bar'}, 'foo')
assert.equal(isPresent, 'bar')
})
// /////////////////
// test suite 13 //
// /////////////////
test('should throw error when function is not passed to is.extend', function (assert) {
const fn = function () {
return Validator.is.extend('presence', 'presence')
}
assert.throws(fn, /Invalid arguments/)
})
// /////////////////
// test suite 14 //
// /////////////////
test('should be able to define multiple rules as an array instead of | symbol', async function (assert) {
const rules = {
username: [Rule('alpha'), Rule('alphaNumeric')]
}
const body = {
username: 'virk@33$'
}
try {
const passed = await Validator.validateAll(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'alpha')
assert.equal(e[1].field, 'username')
assert.equal(e[1].validation, 'alphaNumeric')
}
})
test('should be able to define regex as an array', async function (assert) {
const rules = {
name: [Rule('regex', /[a-zA-z]+$/)]
}
const body = {
name: 'virk@33$'
}
try {
const passed = await Validator.validate(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'name')
assert.equal(e[0].validation, 'regex')
}
})
// /////////////////
// test suite 16 //
// /////////////////
test('should run all validations on multiple fields using validateAll', async function (assert) {
const rules = {
username: 'required',
email: 'required'
}
const body = {
}
try {
const passed = await Validator.validateAll(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'required')
assert.equal(e[1].field, 'email')
assert.equal(e[1].validation, 'required')
}
})
// /////////////////
// test suite 17 //
// /////////////////
test('should make use of snake case validations', async function (assert) {
const rules = {
username: 'alpha_numeric'
}
const body = {
username: 'virk@33$'
}
try {
const passed = await Validator.validate(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'alpha_numeric')
}
})
// /////////////////
// test suite 18 //
// /////////////////
test('should be able to define custom messages for snake case rules', async function (assert) {
const rules = {
username: 'alpha_numeric'
}
const body = {
username: 'virk@33$'
}
const messages = {
'alpha_numeric': 'special chars not allowed'
}
try {
const passed = await Validator.validate(body, rules, messages)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'alpha_numeric')
assert.equal(e[0].message, messages['alpha_numeric'])
}
})
// /////////////////
// test suite 19 //
// /////////////////
test('should fail validation when empty string is passed for any rule with strict mode on', async function (assert) {
Validator.setMode('strict')
const rules = {
select: 'array'
}
const body = {
select: ''
}
try {
const passed = await Validator.validate(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'select')
assert.equal(e[0].validation, 'array')
}
})
// /////////////////
// test suite 20 //
// /////////////////
test('should not fail validation when empty string is passed for any rule in normal mode', async function (assert) {
Validator.setMode('normal')
const rules = {
select: 'array'
}
const body = {
select: ''
}
const passed = await Validator.validate(body, rules)
assert.isObject(passed)
assert.property(passed, 'select')
})
// /////////////////
// test suite 21 //
// /////////////////
test('should be able to validate nested objects using array expression', async function (assert) {
const rules = {
'person.*.firstname': 'required'
}
const data = {
person: [{
firstname: null
}]
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'person.0.firstname')
assert.equal(e[0].validation, 'required')
}
})
// /////////////////
// test suite 22 //
// /////////////////
test('should be able to validate multiple nested objects using array expression', async function (assert) {
const rules = {
'person.*.firstname': 'required'
}
const data = {
person: [
{
firstname: 'virk'
},
{
firstname: null
}
]
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'person.1.firstname')
assert.equal(e[0].validation, 'required')
}
})
// /////////////////
// test suite 23 //
// /////////////////
test('should be able to validate flat arrays using array expression', async function (assert) {
const rules = {
'email.*': 'email'
}
const data = {
email: ['virkm']
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'email.0')
assert.equal(e[0].validation, 'email')
}
})
// /////////////////
// test suite 24 //
// /////////////////
test('should be able to validate multiple values inside flat arrays using array expression', async function (assert) {
const rules = {
'email.*': 'email'
}
const data = {
email: ['foo@bar.com', 'barnseek']
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'email.1')
assert.equal(e[0].validation, 'email')
}
})
// /////////////////
// test suite 25 //
// /////////////////
test('should throw an error when value is not an array', async function (assert) {
const rules = {
people: 'array',
'people.*.email': 'required|email'
}
const data = {
people: ''
}
try {
Validator.setMode('strict')
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e.length, 1)
assert.equal(e[0].field, 'people')
assert.equal(e[0].validation, 'array')
}
})
// /////////////////
// test suite 26 //
// /////////////////
test('should throw an error when value is an array but childs does not exists', async function (assert) {
const rules = {
people: 'array',
'people.*.email': 'required|email'
}
const data = {
people: [{}]
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e.length, 1)
assert.equal(e[0].field, 'people.0.email')
assert.equal(e[0].validation, 'required')
}
})
// /////////////////
// test suite 27 //
// /////////////////
test('should throw an error when value is an array but childs are not valid', async function (assert) {
const rules = {
people: 'array',
'people.*.email': 'required|email'
}
const data = {
people: [{email: 'foo'}]
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e.length, 1)
assert.equal(e[0].field, 'people.0.email')
assert.equal(e[0].validation, 'email')
}
})
// /////////////////
// test suite 28 //
// /////////////////
test('should throw an error when value is an array but one of the multiple childs is not valid', async function (assert) {
const rules = {
people: 'array',
'people.*.email': 'required|email'
}
const data = {
people: [{email: 'foo@bar.com'}, {email: 'snee'}]
}
try {
const passed = await Validator.validate(data, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e.length, 1)
assert.equal(e[0].field, 'people.1.email')
assert.equal(e[0].validation, 'email')
}
})
// /////////////////
// test suite 29 //
// /////////////////
test('should be able to define custom messages for array expressions', async function (assert) {
const rules = {
people: 'array',
'people.*.email': 'required|email'
}
const data = {
people: [{email: 'foo@bar.com'}, {email: 'snee'}]
}
const messages = {
'people.*.email.email': 'Enter valid email address'
}
try {
const passed = await Validator.validate(data, rules, messages)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e.length, 1)
assert.equal(e[0].field, 'people.1.email')
assert.equal(e[0].validation, 'email')
assert.equal(e[0].message, 'Enter valid email address')
}
})
// /////////////////
// test suite 30 //
// /////////////////
test('should be able to define messages for flat array expression', async function (assert) {
const rules = {
'email.*': 'email'
}
const data = {
email: ['virkm']
}
const messages = {
'email.*.email': 'Email address is not valid'
}
try {
const passed = await Validator.validate(data, rules, messages)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'email.0')
assert.equal(e[0].validation, 'email')
assert.equal(e[0].message, 'Email address is not valid')
}
})
// /////////////////
// test suite 31 //
// /////////////////
test('should be able to define messages and make use of dynamic attributes', async function (assert) {
const rules = {
'email.*': 'email'
}
const data = {
email: ['virkm']
}
const messages = {
'email.*.email': '{{field}} is not a valid email'
}
try {
const passed = await Validator.validate(data, rules, messages)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'email.0')
assert.equal(e[0].validation, 'email')
assert.equal(e[0].message, 'email.0 is not a valid email')
}
})
// /////////////////
// test suite 32 //
// /////////////////
test('should not mutate the actual data set', async function (assert) {
const rules = {
}
const data = {
username: ''
}
const passed = await Validator.validate(data, rules)
assert.deepEqual(passed, data)
})
// /////////////////
// test suite 33 //
// /////////////////
test('should not mutate actual data set in strict mode', async function (assert) {
const rules = {
email: 'required'
}
const data = {
username: '',
email: 'foo@bar.com'
}
Validator.setMode('strict')
const passed = await Validator.validate(data, rules)
assert.deepEqual(passed, data)
})
test('should skip the string validation when value is null', async function (assert) {
const rules = {
description: 'string'
}
const data = {
description: null
}
Validator.setMode('normal')
const passed = await Validator.validate(data, rules)
assert.deepEqual(passed, data)
})
test('should be able to add it\'s own validation messages to validation store', async function (assert) {
const phone = function (data, field, message, args, get) {
return new Promise(function (resolve, reject) {
reject(message)
})
}
Validator.extend('isPhone', phone, 'Enter valid phone number')
const rules = {
contact_no: 'is_phone'
}
const body = {}
try {
const validated = await Validator.validate(body, rules)
assert.notExist(validated)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].validation, 'is_phone')
assert.equal(e[0].message, 'Enter valid phone number')
}
})
test('define date format rule', async function (assert) {
const rules = {
date: [Rule('date_format', 'YYYY/MM/DD HH:mm:ss')]
}
const body = {
date: '2017/01/31 12:22:22'
}
const passed = await Validator.validateAll(body, rules)
assert.deepEqual(passed, body)
})
test('should not validate multiple rules on different fields', async function (assert) {
const rules = {
username: 'required',
email: 'required'
}
const body = {}
try {
const passed = await Validator.validate(body, rules)
assert.notExist(passed)
} catch (e) {
assert.isArray(e)
assert.equal(e[0].field, 'username')
assert.equal(e[0].validation, 'required')
assert.equal(e[1], undefined)
}
})
})