quival 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/test.js ADDED
@@ -0,0 +1,2495 @@
1
+ import { strict as assert } from 'assert';
2
+ import Validator from '../src/Validator.js';
3
+
4
+ globalThis.File = class {
5
+ name;
6
+ size;
7
+ type;
8
+
9
+ constructor(name, size, type) {
10
+ this.name = name;
11
+ this.size = size;
12
+ this.type = type;
13
+ }
14
+ };
15
+
16
+ describe('Validation', () => {
17
+ describe(`Rule 'accepted'`, () => {
18
+ const rules = { field: 'accepted' };
19
+
20
+ it(`Passes when the field is within accepted values`, async () => {
21
+ const validator = new Validator({}, rules);
22
+
23
+ for (const field of ['yes', 'on', '1', 1, true, 'true']) {
24
+ validator.setData({ field });
25
+ assert(await validator.passes());
26
+ }
27
+ });
28
+
29
+ it(`Fails when the field is not within accepted values`, async () => {
30
+ const validator = new Validator({}, rules);
31
+
32
+ for (const field of ['no', 'off', '0', 0, false, 'false']) {
33
+ validator.setData({ field });
34
+ assert(await validator.fails());
35
+ }
36
+ });
37
+
38
+ it(`Fails when the field is not present, empty or null`, async () => {
39
+ const validator = new Validator({}, rules);
40
+ assert(await validator.fails());
41
+
42
+ validator.setData({ field: '' });
43
+ assert(await validator.fails());
44
+
45
+ validator.setData({ field: null });
46
+ assert(await validator.fails());
47
+ });
48
+ });
49
+
50
+ describe(`Rule 'accepted_if'`, () => {
51
+ const rules = { field: 'accepted_if:other,foo' };
52
+
53
+ it(`Passes when accepted if the other field's value is equal to provided value`, async () => {
54
+ const validator = new Validator({ field: true, other: 'foo' }, rules);
55
+ assert(await validator.passes());
56
+ });
57
+
58
+ it(`Fails when declined if the other field's value is equal to provided value`, async () => {
59
+ const validator = new Validator({ field: false, other: 'foo' }, rules);
60
+ assert(await validator.fails());
61
+ });
62
+
63
+ it(`Passes when declined if the other field's value is not equal to provided value`, async () => {
64
+ const validator = new Validator({ field: true, other: 'bar' }, rules);
65
+ assert(await validator.passes());
66
+ });
67
+
68
+ it(`Passes when accepted if the other field's value is not equal to provided value`, async () => {
69
+ const validator = new Validator({ field: true, other: 'bar' }, rules);
70
+ assert(await validator.passes());
71
+ });
72
+ });
73
+
74
+ describe(`Rule 'after'`, () => {
75
+ const date = '2022-01-01';
76
+ const rules1 = { field: `after:${date}` };
77
+ const rules2 = { field: 'after:date' };
78
+
79
+ it(`Passes when the field is after ${date}`, async () => {
80
+ const validator = new Validator({ field: '2022-01-02' }, rules1);
81
+ assert(await validator.passes());
82
+ });
83
+
84
+ it(`Passes when the field is after the other field's value`, async () => {
85
+ const validator = new Validator({ field: '2022-01-02', date }, rules2);
86
+ assert(await validator.passes());
87
+ });
88
+
89
+ it(`Fails when the field is equal to ${date}`, async () => {
90
+ const validator = new Validator({ field: '2022-01-01' }, rules1);
91
+ assert(await validator.fails());
92
+ });
93
+
94
+ it(`Fails when the field is equal to the other field's value`, async () => {
95
+ const validator = new Validator({ field: '2022-01-01', date }, rules2);
96
+ assert(await validator.fails());
97
+ });
98
+
99
+ it(`Fails when the field is before ${date}`, async () => {
100
+ const validator = new Validator({ field: '2021-12-31' }, rules1);
101
+ assert(await validator.fails());
102
+ });
103
+
104
+ it(`Fails when the field is before the other field's value`, async () => {
105
+ const validator = new Validator({ field: '2021-12-31', date }, rules2);
106
+ assert(await validator.fails());
107
+ });
108
+ });
109
+
110
+ describe(`Rule 'after_or_equal'`, () => {
111
+ const date = '2022-01-01';
112
+ const rules1 = { field: `after_or_equal:${date}` };
113
+ const rules2 = { field: 'after_or_equal:date' };
114
+
115
+ it(`Passes when the field is after ${date}`, async () => {
116
+ const validator = new Validator({ field: '2022-01-02' }, rules1);
117
+ assert(await validator.passes());
118
+ });
119
+
120
+ it(`Passes when the field is after the other field's value`, async () => {
121
+ const validator = new Validator({ field: '2022-01-02', date }, rules2);
122
+ assert(await validator.passes());
123
+ });
124
+
125
+ it(`Passes when the field is equal to ${date}`, async () => {
126
+ const validator = new Validator({ field: '2022-01-01' }, rules1);
127
+ assert(await validator.passes());
128
+ });
129
+
130
+ it(`Passes when the field is equal to the other field's value`, async () => {
131
+ const validator = new Validator({ field: '2022-01-01', date }, rules2);
132
+ assert(await validator.passes());
133
+ });
134
+
135
+ it(`Fails when the field is before ${date}`, async () => {
136
+ const validator = new Validator({ field: '2021-12-31' }, rules1);
137
+ assert(await validator.fails());
138
+ });
139
+
140
+ it(`Fails when the field is before the other field's value`, async () => {
141
+ const validator = new Validator({ field: '2021-12-31', date }, rules2);
142
+ assert(await validator.fails());
143
+ });
144
+ });
145
+
146
+ describe(`Rule 'alpha'`, () => {
147
+ const rules = { field: 'alpha' };
148
+
149
+ it(`Passes when the field contains alphabets`, async () => {
150
+ const validator = new Validator({ field: 'abc' }, rules);
151
+ assert(await validator.passes());
152
+ });
153
+
154
+ it(`Fails when the field contains numbers`, async () => {
155
+ const validator = new Validator({ field: 'abc123' }, rules);
156
+ assert(await validator.fails());
157
+ });
158
+
159
+ it(`Fails when the field contains symbols`, async () => {
160
+ const validator = new Validator({ field: 'abc!@' }, rules);
161
+ assert(await validator.fails());
162
+ });
163
+ });
164
+
165
+ describe(`Rule 'alpha_dash'`, () => {
166
+ const rules = { field: 'alpha_dash' };
167
+
168
+ it(`Passes when the field contains alphabets`, async () => {
169
+ const validator = new Validator({ field: 'abc' }, rules);
170
+ assert(await validator.passes());
171
+ });
172
+
173
+ it(`Passes when the field contains numbers`, async () => {
174
+ const validator = new Validator({ field: 'abc123' }, rules);
175
+ assert(await validator.passes());
176
+ });
177
+
178
+ it(`Passes when the field contains dashes and underscores`, async () => {
179
+ const validator = new Validator({ field: 'abc123-_' }, rules);
180
+ assert(await validator.passes());
181
+ });
182
+
183
+ it(`Fails when the field contains other symbols`, async () => {
184
+ const validator = new Validator({ field: 'abc!@' }, rules);
185
+ assert(await validator.fails());
186
+ });
187
+ });
188
+
189
+ describe(`Rule 'alpha_num'`, () => {
190
+ const rules = { field: 'alpha_num' };
191
+
192
+ it(`Passes when the field contains alphabets`, async () => {
193
+ const validator = new Validator({ field: 'abc' }, rules);
194
+ assert(await validator.passes());
195
+ });
196
+
197
+ it(`Passes when the field contains numbers`, async () => {
198
+ const validator = new Validator({ field: 'abc123' }, rules);
199
+ assert(await validator.passes());
200
+ });
201
+
202
+ it(`Fails when the field contains symbols`, async () => {
203
+ const validator = new Validator({ field: 'abc!@' }, rules);
204
+ assert(await validator.fails());
205
+ });
206
+ });
207
+
208
+ describe(`Rule 'array'`, () => {
209
+ const rules = { field: 'array' };
210
+ const rules2 = { field: 'array:x,y,z' };
211
+
212
+ it(`Passes when the field is an array or a plain object`, async () => {
213
+ const validator = new Validator({ field: [1, 2, 3] }, rules);
214
+ assert(await validator.passes());
215
+
216
+ validator.setData({ field: { x: 1, y: 2, z: 3 } });
217
+ assert(await validator.passes());
218
+ });
219
+
220
+ it(`Fails when the field is not an array or a plain object`, async () => {
221
+ const validator = new Validator({ field: null }, rules);
222
+ assert(await validator.fails());
223
+
224
+ validator.setData({ field: true });
225
+ assert(await validator.fails());
226
+
227
+ validator.setData({ field: 123 });
228
+ assert(await validator.fails());
229
+
230
+ validator.setData({ field: 'abc' });
231
+ assert(await validator.fails());
232
+ });
233
+
234
+ it(`Passes when keys are present in the array`, async () => {
235
+ const validator = new Validator({ field: { x: 1, y: 2, z: 3 } }, rules2);
236
+ assert(await validator.passes());
237
+ });
238
+
239
+ it(`Fails when keys are not present in the array`, async () => {
240
+ const validator = new Validator({ field: { a: 1, b: 2 } }, rules2);
241
+ assert(await validator.fails());
242
+ });
243
+ });
244
+
245
+ describe(`Rule 'ascii'`, () => {
246
+ const rules = { field: 'ascii' };
247
+
248
+ it(`Passes when the field contains only ascii characters`, async () => {
249
+ const validator = new Validator({ field: 'abc' }, rules);
250
+ assert(await validator.passes());
251
+
252
+ validator.setData({ field: '123' });
253
+ assert(await validator.passes());
254
+ });
255
+
256
+ it(`Fails when the field contains non-ascii characters`, async () => {
257
+ const validator = new Validator({ field: 'ḁḅḉ' }, rules);
258
+ assert(await validator.fails());
259
+
260
+ validator.setData({ field: '۱۲۳' });
261
+ assert(await validator.fails());
262
+ });
263
+ });
264
+
265
+ describe(`Rule 'before'`, () => {
266
+ const date = '2022-01-01';
267
+ const rules1 = { field: `before:${date}` };
268
+ const rules2 = { field: 'before:date' };
269
+
270
+ it(`Passes when the field is before ${date}`, async () => {
271
+ const validator = new Validator({ field: '2021-12-31' }, rules1);
272
+ assert(await validator.passes());
273
+ });
274
+
275
+ it(`Passes when the field is before the other field's value`, async () => {
276
+ const validator = new Validator({ field: '2021-12-31', date }, rules2);
277
+ assert(await validator.passes());
278
+ });
279
+
280
+ it(`Fails when the field is equal to ${date}`, async () => {
281
+ const validator = new Validator({ field: '2022-01-01' }, rules1);
282
+ assert(await validator.fails());
283
+ });
284
+
285
+ it(`Fails when the field is equal to the other field's value`, async () => {
286
+ const validator = new Validator({ field: '2022-01-01', date }, rules2);
287
+ assert(await validator.fails());
288
+ });
289
+
290
+ it(`Fails when the field is after ${date}`, async () => {
291
+ const validator = new Validator({ field: '2022-01-02' }, rules1);
292
+ assert(await validator.fails());
293
+ });
294
+
295
+ it(`Fails when the field is after the other field's value`, async () => {
296
+ const validator = new Validator({ field: '2022-01-02', date }, rules2);
297
+ assert(await validator.fails());
298
+ });
299
+ });
300
+
301
+ describe(`Rule 'before_or_equal'`, () => {
302
+ const date = '2022-01-01';
303
+ const rules1 = { field: `before_or_equal:${date}` };
304
+ const rules2 = { field: 'before_or_equal:date' };
305
+
306
+ it(`Passes when the field is before ${date}`, async () => {
307
+ const validator = new Validator({ field: '2021-12-31' }, rules1);
308
+ assert(await validator.passes());
309
+ });
310
+
311
+ it(`Passes when the field is before the other field's value`, async () => {
312
+ const validator = new Validator({ field: '2021-12-31', date }, rules2);
313
+ assert(await validator.passes());
314
+ });
315
+
316
+ it(`Passes when the field is equal to ${date}`, async () => {
317
+ const validator = new Validator({ field: '2022-01-01' }, rules1);
318
+ assert(await validator.passes());
319
+ });
320
+
321
+ it(`Passes when the field is equal to the other field's value`, async () => {
322
+ const validator = new Validator({ field: '2022-01-01', date }, rules2);
323
+ assert(await validator.passes());
324
+ });
325
+
326
+ it(`Fails when the field is after ${date}`, async () => {
327
+ const validator = new Validator({ field: '2022-01-02' }, rules1);
328
+ assert(await validator.fails());
329
+ });
330
+
331
+ it(`Fails when the field is after the other field's value`, async () => {
332
+ const validator = new Validator({ field: '2022-01-02', date }, rules2);
333
+ assert(await validator.fails());
334
+ });
335
+ });
336
+
337
+ describe(`Rule 'between'`, () => {
338
+ const min = 5;
339
+ const max = 10;
340
+
341
+ it(`Passes when the array's size is between ${min} and ${max}`, async () => {
342
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: `array|between:${min},${max}` });
343
+ assert(await validator.passes());
344
+ });
345
+
346
+ it(`Fails when the array's size is not between ${min} and ${max}`, async () => {
347
+ const validator = new Validator({ field: [1, 2, 3] }, { field: `array|between:${min},${max}` });
348
+ assert(await validator.fails());
349
+ });
350
+
351
+ it(`Passes when the file's size is between ${min} and ${max}`, async () => {
352
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: `file|between:${min},${max}` });
353
+ assert(await validator.passes());
354
+ });
355
+
356
+ it(`Fails when the file's size is not between ${min} and ${max}`, async () => {
357
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: `file|between:${min},${max}` });
358
+ assert(await validator.fails());
359
+ });
360
+
361
+ it(`Passes when the number is between ${min} and ${max}`, async () => {
362
+ const validator = new Validator({ field: 7 }, { field: `numeric|between:${min},${max}` });
363
+ assert(await validator.passes());
364
+ });
365
+
366
+ it(`Fails when the number is not between ${min} and ${max}`, async () => {
367
+ const validator = new Validator({ field: 3 }, { field: `numeric|between:${min},${max}` });
368
+ assert(await validator.fails());
369
+ });
370
+
371
+ it(`Passes when the string's length is between ${min} and ${max}`, async () => {
372
+ const validator = new Validator({ field: 'abcdefg' }, { field: `string|between:${min},${max}` });
373
+ assert(await validator.passes());
374
+ });
375
+
376
+ it(`Fails when the string's length is not between ${min} and ${max}`, async () => {
377
+ const validator = new Validator({ field: 'abc' }, { field: `string|between:${min},${max}` });
378
+ assert(await validator.fails());
379
+ });
380
+ });
381
+
382
+ describe(`Rule 'boolean'`, () => {
383
+ const rules = { field: 'boolean' };
384
+
385
+ it(`Passes when the field is a boolean`, async () => {
386
+ const validator = new Validator({}, rules);
387
+
388
+ for (const field of [true, false, 0, 1, '0', '1']) {
389
+ validator.setData({ field });
390
+ assert(await validator.passes());
391
+ }
392
+ });
393
+
394
+ it(`Fails when the field is not a boolean`, async () => {
395
+ const validator = new Validator({ field: null }, rules);
396
+ assert(await validator.fails());
397
+
398
+ validator.setData({ field: 123 });
399
+ assert(await validator.fails());
400
+
401
+ validator.setData({ field: 'abc' });
402
+ assert(await validator.fails());
403
+
404
+ validator.setData({ field: [1, 2, 3] });
405
+ assert(await validator.fails());
406
+ });
407
+ });
408
+
409
+ describe(`Rule 'confirmed'`, () => {
410
+ const rules = { field: 'confirmed' };
411
+
412
+ it(`Passes when the field is confirmed`, async () => {
413
+ const validator = new Validator({ field: 'abc', field_confirmation: 'abc' }, rules);
414
+ assert(await validator.passes());
415
+ });
416
+
417
+ it(`Fails when the field is not confirmed`, async () => {
418
+ const validator = new Validator({ field: 'abc', field_confirmation: 'xyz' }, rules);
419
+ assert(await validator.fails());
420
+ });
421
+
422
+ it(`Fails when the confirmation field is not present`, async () => {
423
+ const validator = new Validator({ field: 'abc' }, rules);
424
+ assert(await validator.fails());
425
+ });
426
+ });
427
+
428
+ describe(`Rule 'date'`, () => {
429
+ const rules = { field: 'date' };
430
+
431
+ it(`Passes when the field is a valid date`, async () => {
432
+ const validator = new Validator({ field: '2023-01-01' }, rules);
433
+ assert(await validator.passes());
434
+
435
+ validator.setData({ field: '01-01-2023' });
436
+ assert(await validator.passes());
437
+ });
438
+
439
+ it(`Fails when the field is not a valid date`, async () => {
440
+ const validator = new Validator({ field: null }, rules);
441
+ assert(await validator.fails());
442
+
443
+ validator.setData({ field: true });
444
+ assert(await validator.fails());
445
+
446
+ validator.setData({ field: 123 });
447
+ assert(await validator.fails());
448
+
449
+ validator.setData({ field: 'abc' });
450
+ assert(await validator.fails());
451
+
452
+ validator.setData({ field: [1, 2, 3] });
453
+ assert(await validator.fails());
454
+ });
455
+ });
456
+
457
+ describe(`Rule 'date_equals'`, () => {
458
+ const date = '2022-01-01';
459
+ const rules1 = { field: `date_equals:${date}` };
460
+ const rules2 = { field: 'date_equals:date' };
461
+
462
+ it(`Passes when the field is equal to ${date}`, async () => {
463
+ const validator = new Validator({ field: '2022-01-01' }, rules1);
464
+ assert(await validator.passes());
465
+ });
466
+
467
+ it(`Passes when the field is equal to other field's value`, async () => {
468
+ const validator = new Validator({ field: '2022-01-01', date }, rules2);
469
+ assert(await validator.passes());
470
+ });
471
+
472
+ it(`Fails when the field is after ${date}`, async () => {
473
+ const validator = new Validator({ field: '2022-01-02' }, rules1);
474
+ assert(await validator.fails());
475
+ });
476
+
477
+ it(`Fails when the field is after the other field's value`, async () => {
478
+ const validator = new Validator({ field: '2022-01-02', date }, rules2);
479
+ assert(await validator.fails());
480
+ });
481
+
482
+ it(`Fails when the field is before ${date}`, async () => {
483
+ const validator = new Validator({ field: '2021-12-31' }, rules1);
484
+ assert(await validator.fails());
485
+ });
486
+
487
+ it(`Fails when the field is before the other field's value`, async () => {
488
+ const validator = new Validator({ field: '2021-12-31', date }, rules2);
489
+ assert(await validator.fails());
490
+ });
491
+ });
492
+
493
+ describe(`Rule 'date_format'`, () => {
494
+ for (const [format, date] of Object.entries({
495
+ 'Y-m-d': '2023-12-31',
496
+ 'd-m-Y': '31-12-2023',
497
+ 'm-d-Y': '12-31-2023',
498
+ 'Y-m': '2023-31',
499
+ })) {
500
+ it(`Passes when the field matches the format ${format}`, async () => {
501
+ const validator = new Validator({ field: date }, { field: `date_format:${format}` });
502
+ assert(await validator.passes());
503
+ });
504
+ }
505
+
506
+ for (const [format, date] of Object.entries({
507
+ 'Y-m-d': '2023-11',
508
+ 'd-m-Y': '31-2023',
509
+ 'm-d-Y': '31-2023',
510
+ 'Y-m': '2023-31-12',
511
+ })) {
512
+ it(`Fails when the field does not match the format ${format}`, async () => {
513
+ const validator = new Validator({ field: date }, { field: `date_format:${format}` });
514
+ assert(await validator.fails());
515
+ });
516
+ }
517
+ });
518
+
519
+ describe(`Rule 'decimal'`, () => {
520
+ it(`Passes when the field has 2 decimal places`, async () => {
521
+ const validator = new Validator({ field: '1.23' }, { field: 'decimal:2' });
522
+ assert(await validator.passes());
523
+ });
524
+
525
+ it(`Fails when the field does not have 2 decimal places`, async () => {
526
+ const validator = new Validator({ field: '1.234' }, { field: 'decimal:2' });
527
+ assert(await validator.fails());
528
+ });
529
+
530
+ it(`Passes when the field's decimal places is between 2 and 4`, async () => {
531
+ const validator = new Validator({ field: '1.234' }, { field: 'decimal:2,4' });
532
+ assert(await validator.passes());
533
+ });
534
+
535
+ it(`Fails when the field's decimal places is not between 2 and 4`, async () => {
536
+ const validator = new Validator({ field: '1.23456' }, { field: 'decimal:2,4' });
537
+ assert(await validator.fails());
538
+ });
539
+ });
540
+
541
+ describe(`Rule 'declined'`, () => {
542
+ const rules = { field: 'declined' };
543
+
544
+ it(`Passes when the field is within declined values`, async () => {
545
+ const validator = new Validator({}, rules);
546
+
547
+ for (const field of ['no', 'off', '0', 0, false, 'false']) {
548
+ validator.setData({ field });
549
+ assert(await validator.passes());
550
+ }
551
+ });
552
+
553
+ it(`Fails when the field is not within declined values`, async () => {
554
+ const validator = new Validator({}, rules);
555
+
556
+ for (const field of ['yes', 'on', '1', 1, true, 'true']) {
557
+ validator.setData({ field });
558
+ assert(await validator.fails());
559
+ }
560
+ });
561
+
562
+ it(`Fails when the field is not present, empty or null`, async () => {
563
+ const validator = new Validator({}, rules);
564
+ assert(await validator.fails());
565
+
566
+ validator.setData({ field: '' });
567
+ assert(await validator.fails());
568
+
569
+ validator.setData({ field: null });
570
+ assert(await validator.fails());
571
+ });
572
+ });
573
+
574
+ describe(`Rule 'declined_if'`, () => {
575
+ const rules = { field: 'declined_if:other,foo' };
576
+
577
+ it(`Passes when declined if the other field's value is equal to provided value`, async () => {
578
+ const validator = new Validator({ field: false, other: 'foo' }, rules);
579
+ assert(await validator.passes());
580
+ });
581
+
582
+ it(`Fails when accepted if the other field's value is equal to provided value`, async () => {
583
+ const validator = new Validator({ field: true, other: 'foo' }, rules);
584
+ assert(await validator.fails());
585
+ });
586
+
587
+ it(`Passes when declined if the other field's value is not equal to provided value`, async () => {
588
+ const validator = new Validator({ field: true, other: 'bar' }, rules);
589
+ assert(await validator.passes());
590
+ });
591
+
592
+ it(`Passes when accepted if the other field's value is not equal to provided value`, async () => {
593
+ const validator = new Validator({ field: true, other: 'bar' }, rules);
594
+ assert(await validator.passes());
595
+ });
596
+ });
597
+
598
+ describe(`Rule 'different'`, () => {
599
+ const rules = { field: 'different:other,foo' };
600
+
601
+ it(`Passes when the field is different to the other field`, async () => {
602
+ const validator = new Validator({ field: 'foo', other: 'bar' }, rules);
603
+ assert(await validator.passes());
604
+ });
605
+
606
+ it(`Fails when the field is same to the other field`, async () => {
607
+ const validator = new Validator({ field: 'foo', other: 'foo' }, rules);
608
+ assert(await validator.fails());
609
+ });
610
+
611
+ it(`Passes when the other field is not present`, async () => {
612
+ const validator = new Validator({ field: 'foo' }, rules);
613
+ assert(await validator.passes());
614
+ });
615
+ });
616
+
617
+ describe(`Rule 'digits'`, () => {
618
+ const rules = { field: 'digits:3' };
619
+
620
+ it(`Passes when the field has 3 digits`, async () => {
621
+ const validator = new Validator({ field: '123' }, rules);
622
+ assert(await validator.passes());
623
+ });
624
+
625
+ it(`Fails when the field has less than 3 digits`, async () => {
626
+ const validator = new Validator({ field: '12' }, rules);
627
+ assert(await validator.fails());
628
+ });
629
+
630
+ it(`Fails when the field has more than 3 digits`, async () => {
631
+ const validator = new Validator({ field: '1234' }, rules);
632
+ assert(await validator.fails());
633
+ });
634
+ });
635
+
636
+ describe(`Rule 'digits_between'`, () => {
637
+ const min = 3;
638
+ const max = 5;
639
+ const rules = { field: `digits_between:${min},${max}` };
640
+
641
+ it(`Passes when the field has digits in between ${min} and ${max} digits`, async () => {
642
+ const validator = new Validator({ field: '1234' }, rules);
643
+ assert(await validator.passes());
644
+ });
645
+
646
+ it(`Fails when the field has less than ${min} digits`, async () => {
647
+ const validator = new Validator({ field: '12' }, rules);
648
+ assert(await validator.fails());
649
+ });
650
+
651
+ it(`Fails when the field has more than ${max} digits`, async () => {
652
+ const validator = new Validator({ field: '123456' }, rules);
653
+ assert(await validator.fails());
654
+ });
655
+ });
656
+
657
+ describe(`Rule 'distinct'`, () => {
658
+ it(`Passes when the field are distinctive`, async () => {
659
+ const validator = new Validator({ field: [1, 2, 3, 4] }, { 'field.*': 'distinct' });
660
+ assert(await validator.passes());
661
+ });
662
+
663
+ it(`Passes when the field are distinctive in strict mode`, async () => {
664
+ const validator = new Validator({ field: [1, 3, '3', 7] }, { 'field.*': 'distinct:strict' });
665
+ assert(await validator.passes());
666
+ });
667
+
668
+ it(`Passes when the field are distinctive in ignore_case mode`, async () => {
669
+ const validator = new Validator({ field: ['a', 'b', 'c', 'd'] }, { 'field.*': 'distinct:ignore_case' });
670
+ assert(await validator.passes());
671
+ });
672
+
673
+ it(`Fails when the field are not distinctive`, async () => {
674
+ const validator = new Validator({ field: [1, 3, '3', 7] }, { 'field.*': 'distinct' });
675
+ assert(await validator.fails());
676
+ });
677
+
678
+ it(`Fails when the field are not distinctive in strict mode`, async () => {
679
+ const validator = new Validator({ field: [1, 3, 3, 7] }, { 'field.*': 'distinct:strict' });
680
+ assert(await validator.fails());
681
+ });
682
+
683
+ it(`Fails when the field are not distinctive in ignore_case mode`, async () => {
684
+ const validator = new Validator({ field: ['a', 'b', 'B', 'A'] }, { 'field.*': 'distinct:ignore_case' });
685
+ assert(await validator.fails());
686
+ });
687
+ });
688
+
689
+ describe(`Rule 'doesnt_end_with'`, () => {
690
+ const rules = { field: 'doesnt_end_with:foo,bar' };
691
+
692
+ it(`Passes when the field does not end with any value`, async () => {
693
+ const validator = new Validator({ field: 'yes' }, rules);
694
+ assert(await validator.passes());
695
+ });
696
+
697
+ it(`Fails when the field ends with the first value`, async () => {
698
+ const validator = new Validator({ field: 'yesfoo' }, rules);
699
+ assert(await validator.fails());
700
+ });
701
+
702
+ it(`Fails when the field ends with the second value`, async () => {
703
+ const validator = new Validator({ field: 'yesbar' }, rules);
704
+ assert(await validator.fails());
705
+ });
706
+ });
707
+
708
+ describe(`Rule 'doesnt_start_with'`, () => {
709
+ const rules = { field: 'doesnt_start_with:foo,bar' };
710
+
711
+ it(`Passes when the field does not start with any value`, async () => {
712
+ const validator = new Validator({ field: 'yes' }, rules);
713
+ assert(await validator.passes());
714
+ });
715
+
716
+ it(`Fails when the field starts with the first value`, async () => {
717
+ const validator = new Validator({ field: 'fooyes' }, rules);
718
+ assert(await validator.fails());
719
+ });
720
+
721
+ it(`Fails when the field starts with the second value`, async () => {
722
+ const validator = new Validator({ field: 'baryes' }, rules);
723
+ assert(await validator.fails());
724
+ });
725
+ });
726
+
727
+ describe(`Rule 'email'`, () => {
728
+ const rules = { field: 'email' };
729
+
730
+ it(`Passes when the field has a valid email address`, async () => {
731
+ const validator = new Validator({ field: 'hafizuddin_83@yahoo.com' }, rules);
732
+ assert(await validator.passes());
733
+ });
734
+
735
+ it(`Fails when the field does not contains '@'`, async () => {
736
+ const validator = new Validator({ field: 'hafizuddin_83yahoo.com' }, rules);
737
+ assert(await validator.fails());
738
+ });
739
+
740
+ it(`Fails when the field does not end with a domain name`, async () => {
741
+ const validator = new Validator({ field: 'hafizuddin_83@yahoo' }, rules);
742
+ assert(await validator.fails());
743
+ });
744
+ });
745
+
746
+ describe(`Rule 'ends_with'`, () => {
747
+ const rules = { field: 'ends_with:foo,bar' };
748
+
749
+ it(`Passes when the field ends with the first values`, async () => {
750
+ const validator = new Validator({ field: 'yesfoo' }, rules);
751
+ assert(await validator.passes());
752
+ });
753
+
754
+ it(`Passes when the field ends with the second values`, async () => {
755
+ const validator = new Validator({ field: 'yesbar' }, rules);
756
+ assert(await validator.passes());
757
+ });
758
+
759
+ it(`Fails when the field does not end with any value`, async () => {
760
+ const validator = new Validator({ field: 'yes' }, rules);
761
+ assert(await validator.fails());
762
+ });
763
+ });
764
+
765
+ describe(`Rule 'file'`, () => {
766
+ const rules = { field: 'file' };
767
+
768
+ it(`Passes when the field is a file`, async () => {
769
+ const validator = new Validator({ field: new File('', 5 * 1024, '') }, rules);
770
+ assert(await validator.passes());
771
+ });
772
+
773
+ it(`Fails when the field is not a file`, async () => {
774
+ const validator = new Validator({ field: null }, rules);
775
+ assert(await validator.fails());
776
+
777
+ validator.setData({ field: true });
778
+ assert(await validator.fails());
779
+
780
+ validator.setData({ field: 123 });
781
+ assert(await validator.fails());
782
+
783
+ validator.setData({ field: 'abc' });
784
+ assert(await validator.fails());
785
+
786
+ validator.setData({ field: [1, 2, 3] });
787
+ assert(await validator.fails());
788
+ });
789
+ });
790
+
791
+ describe(`Rule 'filled'`, () => {
792
+ const rules = { field: 'filled' };
793
+
794
+ it(`Passes when the field is filled`, async () => {
795
+ const validator = new Validator({ field: 'abc' }, rules);
796
+ assert(await validator.passes());
797
+ });
798
+
799
+ it(`Passes when the field is not present`, async () => {
800
+ const validator = new Validator({}, rules);
801
+ assert(await validator.passes());
802
+ });
803
+
804
+ it(`Fails when the field is empty or null`, async () => {
805
+ const validator = new Validator({ field: '' }, rules);
806
+ assert(await validator.fails());
807
+
808
+ validator.setData({ field: null });
809
+ assert(await validator.fails());
810
+ });
811
+ });
812
+
813
+ describe(`Rule 'gt'`, () => {
814
+ it(`Passes when the array's size is greater than provided value`, async () => {
815
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: 'array|gt:3' });
816
+ assert(await validator.passes());
817
+ });
818
+
819
+ it(`Passes when the array's size is greater than other field`, async () => {
820
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6], other: [1, 2, 3] }, { field: 'array|gt:other', other: 'array' });
821
+ assert(await validator.passes());
822
+ });
823
+
824
+ it(`Passes when the file's size is greater than provided value`, async () => {
825
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: 'file|gt:3' });
826
+ assert(await validator.passes());
827
+ });
828
+
829
+ it(`Passes when the file's size is greater than other field`, async () => {
830
+ const validator = new Validator({ field: new File('', 6 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|gt:other', other: 'file' });
831
+ assert(await validator.passes());
832
+ });
833
+
834
+ it(`Passes when the number is greater than provided value`, async () => {
835
+ const validator = new Validator({ field: 6 }, { field: 'numeric|gt:3' });
836
+ assert(await validator.passes());
837
+ });
838
+
839
+ it(`Passes when the number is greater than other field`, async () => {
840
+ const validator = new Validator({ field: 6, other: 3 }, { field: 'numeric|gt:other', other: 'numeric' });
841
+ assert(await validator.passes());
842
+ });
843
+
844
+ it(`Passes when the string's length is greater than provided value`, async () => {
845
+ const validator = new Validator({ field: 'abcdef' }, { field: 'string|gt:3' });
846
+ assert(await validator.passes());
847
+ });
848
+
849
+ it(`Passes when the string's length is greater than other field`, async () => {
850
+ const validator = new Validator({ field: 'abcdef', other: 'abc' }, { field: 'string|gt:other', other: 'string' });
851
+ assert(await validator.passes());
852
+ });
853
+
854
+ it(`Fails when the array's size is equal to provided value`, async () => {
855
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|gt:3' });
856
+ assert(await validator.fails());
857
+ });
858
+
859
+ it(`Fails when the array's size is equal to other field`, async () => {
860
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3] }, { field: 'array|gt:other', other: 'array' });
861
+ assert(await validator.fails());
862
+ });
863
+
864
+ it(`Fails when the file's size is is equal to provided value`, async () => {
865
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|gt:3' });
866
+ assert(await validator.fails());
867
+ });
868
+
869
+ it(`Fails when the file's size is is equal to other field`, async () => {
870
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|gt:other', other: 'file' });
871
+ assert(await validator.fails());
872
+ });
873
+
874
+ it(`Fails when the number is equal to provided value`, async () => {
875
+ const validator = new Validator({ field: 3 }, { field: 'numeric|gt:3' });
876
+ assert(await validator.fails());
877
+ });
878
+
879
+ it(`Fails when the number is equal to other field`, async () => {
880
+ const validator = new Validator({ field: 3, other: 3 }, { field: 'numeric|gt:other', other: 'numeric' });
881
+ assert(await validator.fails());
882
+ });
883
+
884
+ it(`Fails when the string's length is equal to provided value`, async () => {
885
+ const validator = new Validator({ field: 'abc' }, { field: 'string|gt:3' });
886
+ assert(await validator.fails());
887
+ });
888
+
889
+ it(`Fails when the string's length is equal to other field`, async () => {
890
+ const validator = new Validator({ field: 'abc', other: 'abc' }, { field: 'string|gt:other', other: 'string' });
891
+ assert(await validator.fails());
892
+ });
893
+
894
+ it(`Fails when the array's size is lesser than provided value`, async () => {
895
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|gt:6' });
896
+ assert(await validator.fails());
897
+ });
898
+
899
+ it(`Fails when the array's size is lesser than other field`, async () => {
900
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3, 4, 5, 6] }, { field: 'array|gt:other', other: 'array' });
901
+ assert(await validator.fails());
902
+ });
903
+
904
+ it(`Fails when the file's size is lesser than provided value`, async () => {
905
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|gt:6' });
906
+ assert(await validator.fails());
907
+ });
908
+
909
+ it(`Fails when the file's size is lesser than other field`, async () => {
910
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 6 * 1024, '') }, { field: 'file|gt:other', other: 'file' });
911
+ assert(await validator.fails());
912
+ });
913
+
914
+ it(`Fails when the number is lesser than provided value`, async () => {
915
+ const validator = new Validator({ field: 3 }, { field: 'numeric|gt:6' });
916
+ assert(await validator.fails());
917
+ });
918
+
919
+ it(`Fails when the number is lesser than other field`, async () => {
920
+ const validator = new Validator({ field: 3, other: 6 }, { field: 'numeric|gt:other', other: 'numeric' });
921
+ assert(await validator.fails());
922
+ });
923
+
924
+ it(`Fails when the string's length is lesser than provided value`, async () => {
925
+ const validator = new Validator({ field: 'abc' }, { field: 'string|gt:6' });
926
+ assert(await validator.fails());
927
+ });
928
+
929
+ it(`Fails when the string's length is lesser than other field`, async () => {
930
+ const validator = new Validator({ field: 'abc', other: 'abcdef' }, { field: 'string|gt:other', other: 'string' });
931
+ assert(await validator.fails());
932
+ });
933
+ });
934
+
935
+ describe(`Rule 'gte'`, () => {
936
+ it(`Passes when the array's size is greater than provided value`, async () => {
937
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: 'array|gte:3' });
938
+ assert(await validator.passes());
939
+ });
940
+
941
+ it(`Passes when the array's size is greater than other field`, async () => {
942
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6], other: [1, 2, 3] }, { field: 'array|gte:other', other: 'array' });
943
+ assert(await validator.passes());
944
+ });
945
+
946
+ it(`Passes when the file's size is greater than provided value`, async () => {
947
+ const validator = new Validator({ field: new File('', 66 * 1024, '') }, { field: 'file|gte:3' });
948
+ assert(await validator.passes());
949
+ });
950
+
951
+ it(`Passes when the file's size is greater than other field`, async () => {
952
+ const validator = new Validator({ field: new File('', 66 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|gte:other', other: 'file' });
953
+ assert(await validator.passes());
954
+ });
955
+
956
+ it(`Passes when the number is greater than provided value`, async () => {
957
+ const validator = new Validator({ field: 6 }, { field: 'numeric|gte:3' });
958
+ assert(await validator.passes());
959
+ });
960
+
961
+ it(`Passes when the number is greater than other field`, async () => {
962
+ const validator = new Validator({ field: 6, other: 3 }, { field: 'numeric|gte:other', other: 'numeric' });
963
+ assert(await validator.passes());
964
+ });
965
+
966
+ it(`Passes when the string's length is greater than provided value`, async () => {
967
+ const validator = new Validator({ field: 'abcdef' }, { field: 'string|gte:3' });
968
+ assert(await validator.passes());
969
+ });
970
+
971
+ it(`Passes when the string's length is greater than other field`, async () => {
972
+ const validator = new Validator({ field: 'abcdef', other: 'abc' }, { field: 'string|gte:other', other: 'string' });
973
+ assert(await validator.passes());
974
+ });
975
+
976
+ it(`Passes when the array's size is equal to provided value`, async () => {
977
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|gte:3' });
978
+ assert(await validator.passes());
979
+ });
980
+
981
+ it(`Passes when the array's size is equal to other field`, async () => {
982
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3] }, { field: 'array|gte:other', other: 'array' });
983
+ assert(await validator.passes());
984
+ });
985
+
986
+ it(`Passes when the file's size is equal to provided value`, async () => {
987
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|gte:3' });
988
+ assert(await validator.passes());
989
+ });
990
+
991
+ it(`Passes when the file's size is equal to other field`, async () => {
992
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|gte:other', other: 'file' });
993
+ assert(await validator.passes());
994
+ });
995
+
996
+ it(`Passes when the number is equal to provided value`, async () => {
997
+ const validator = new Validator({ field: 3 }, { field: 'numeric|gte:3' });
998
+ assert(await validator.passes());
999
+ });
1000
+
1001
+ it(`Passes when the number is equal to other field`, async () => {
1002
+ const validator = new Validator({ field: 3, other: 3 }, { field: 'numeric|gte:other', other: 'numeric' });
1003
+ assert(await validator.passes());
1004
+ });
1005
+
1006
+ it(`Passes when the string's length is equal to provided value`, async () => {
1007
+ const validator = new Validator({ field: 'abc' }, { field: 'string|gte:3' });
1008
+ assert(await validator.passes());
1009
+ });
1010
+
1011
+ it(`Passes when the string's length is equal to other field`, async () => {
1012
+ const validator = new Validator({ field: 'abc', other: 'abc' }, { field: 'string|gte:other', other: 'string' });
1013
+ assert(await validator.passes());
1014
+ });
1015
+
1016
+ it(`Fails when the array's size is lesser than provided value`, async () => {
1017
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|gte:6' });
1018
+ assert(await validator.fails());
1019
+ });
1020
+
1021
+ it(`Fails when the array's size is lesser than other field`, async () => {
1022
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3, 4, 5, 6] }, { field: 'array|gte:other', other: 'array' });
1023
+ assert(await validator.fails());
1024
+ });
1025
+
1026
+ it(`Fails when the file's size is lesser than provided value`, async () => {
1027
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|gte:6' });
1028
+ assert(await validator.fails());
1029
+ });
1030
+
1031
+ it(`Fails when the file's size is lesser than other field`, async () => {
1032
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 6 * 1024, '') }, { field: 'file|gte:other', other: 'file' });
1033
+ assert(await validator.fails());
1034
+ });
1035
+
1036
+ it(`Fails when the number is lesser than provided value`, async () => {
1037
+ const validator = new Validator({ field: 3 }, { field: 'numeric|gte:6' });
1038
+ assert(await validator.fails());
1039
+ });
1040
+
1041
+ it(`Fails when the number is lesser than other field`, async () => {
1042
+ const validator = new Validator({ field: 3, other: 6 }, { field: 'numeric|gte:other', other: 'numeric' });
1043
+ assert(await validator.fails());
1044
+ });
1045
+
1046
+ it(`Fails when the string's length is lesser than provided value`, async () => {
1047
+ const validator = new Validator({ field: 'abc' }, { field: 'string|gte:6' });
1048
+ assert(await validator.fails());
1049
+ });
1050
+
1051
+ it(`Fails when the string's length is lesser than other field`, async () => {
1052
+ const validator = new Validator({ field: 'abc', other: 'abcdef' }, { field: 'string|gte:other', other: 'string' });
1053
+ assert(await validator.fails());
1054
+ });
1055
+ });
1056
+
1057
+ describe(`Rule 'image'`, () => {
1058
+ const rules = { field: 'image' };
1059
+
1060
+ it(`Passes when the field is an image`, async () => {
1061
+ const validator = new Validator({ field: new File('hello.jpg', 5 * 1024, 'image/jpg') }, rules);
1062
+ assert(await validator.passes());
1063
+ });
1064
+
1065
+ it(`Fails when the field is not an image`, async () => {
1066
+ const validator = new Validator({ field: new File('hello.txt', 5 * 1024, 'text/plain') }, rules);
1067
+ assert(await validator.fails());
1068
+ });
1069
+ });
1070
+
1071
+ describe(`Rule 'in'`, () => {
1072
+ it(`Passes when the field's value is in the list`, async () => {
1073
+ const validator = new Validator({ field: 'y' }, { field: 'in:x,y,z' });
1074
+ assert(await validator.passes());
1075
+ });
1076
+
1077
+ it(`Passes when the field's values are in the list`, async () => {
1078
+ const validator = new Validator({ field: ['y', 'z'] }, { field: 'array|in:x,y,z' });
1079
+ assert(await validator.passes());
1080
+ });
1081
+
1082
+ it(`Fails when the field's value is not in the list`, async () => {
1083
+ const validator = new Validator({ field: 'a' }, { field: 'in:x,y,z' });
1084
+ assert(await validator.fails());
1085
+ });
1086
+
1087
+ it(`Fails when the field's values are not in the list`, async () => {
1088
+ const validator = new Validator({ field: ['a', 'z'] }, { field: 'array|in:x,y,z' });
1089
+ assert(await validator.fails());
1090
+ });
1091
+ });
1092
+
1093
+ describe(`Rule 'in_array'`, () => {
1094
+ const rules = { field: 'in_array:other.*' };
1095
+
1096
+ it(`Passes when the field is in the other field`, async () => {
1097
+ const validator = new Validator({ field: 'y', other: ['x', 'y', 'z'] }, rules);
1098
+ assert(await validator.passes());
1099
+ });
1100
+
1101
+ it(`Fails when the field is not in the other field`, async () => {
1102
+ const validator = new Validator({ field: 'c', other: ['x', 'y', 'z'] }, rules);
1103
+ assert(await validator.fails());
1104
+ });
1105
+ });
1106
+
1107
+ describe(`Rule 'integer'`, () => {
1108
+ const rules = { field: 'integer' };
1109
+
1110
+ it(`Passes when the field is an integer`, async () => {
1111
+ const validator = new Validator({ field: 123 }, rules);
1112
+ assert(await validator.passes());
1113
+
1114
+ validator.setData({ field: '123' });
1115
+ assert(await validator.passes());
1116
+ });
1117
+
1118
+ it(`Fails when the field is a decimal`, async () => {
1119
+ const validator = new Validator({ field: 123.45 }, rules);
1120
+ assert(await validator.fails());
1121
+ });
1122
+
1123
+ it(`Fails when the field is not a number`, async () => {
1124
+ const validator = new Validator({ field: null }, rules);
1125
+ assert(await validator.fails());
1126
+
1127
+ validator.setData({ field: true });
1128
+ assert(await validator.fails());
1129
+
1130
+ validator.setData({ field: 'abc' });
1131
+ assert(await validator.fails());
1132
+
1133
+ validator.setData({ field: [1, 2, 3] });
1134
+ assert(await validator.fails());
1135
+ });
1136
+ });
1137
+
1138
+ describe(`Rule 'ip'`, () => {
1139
+ const rules = { field: 'ip' };
1140
+
1141
+ it(`Passes when the field is a valid IP address`, async () => {
1142
+ const validator = new Validator({ field: '127.0.0.1' }, rules);
1143
+ assert(await validator.passes());
1144
+ });
1145
+
1146
+ it(`Fails when the field is an invalid IP address`, async () => {
1147
+ const validator = new Validator({ field: '127.x.x.1' }, rules);
1148
+ assert(await validator.fails());
1149
+ });
1150
+
1151
+ it(`Fails when the field is a regular string`, async () => {
1152
+ const validator = new Validator({ field: 'abc' }, rules);
1153
+ assert(await validator.fails());
1154
+ });
1155
+ });
1156
+
1157
+ describe(`Rule 'ipv4'`, () => {
1158
+ const rules = { field: 'ipv4' };
1159
+
1160
+ it(`Passes when the field is a valid IPv4 address`, async () => {
1161
+ const validator = new Validator({ field: '127.0.0.1' }, rules);
1162
+ assert(await validator.passes());
1163
+ });
1164
+
1165
+ it(`Fails when the field is a regular string`, async () => {
1166
+ const validator = new Validator({ field: 'abc' }, rules);
1167
+ assert(await validator.fails());
1168
+ });
1169
+
1170
+ it(`Fails when the field is a valid IPv6 address`, async () => {
1171
+ const validator = new Validator({ field: '2001:0db8:0000:0000:0000:ff00:0042:8329' }, rules);
1172
+ assert(await validator.fails());
1173
+ });
1174
+ });
1175
+
1176
+ describe(`Rule 'ipv6'`, () => {
1177
+ const rules = { field: 'ipv6' };
1178
+
1179
+ it(`Passes when the field is a normal IPv6 address`, async () => {
1180
+ const validator = new Validator({ field: '2001:0db8:0000:0000:0000:ff00:0042:8329' }, rules);
1181
+ assert(await validator.passes());
1182
+ });
1183
+
1184
+ it(`Passes when the field is a compressed IPv6 address`, async () => {
1185
+ const validator = new Validator({ field: '2001:db8::ff00:42:8329' }, rules);
1186
+ assert(await validator.passes());
1187
+ });
1188
+
1189
+ it(`Passes when the field is a loopback IPv6 address`, async () => {
1190
+ const validator = new Validator({ field: '::1' }, rules);
1191
+ assert(await validator.passes());
1192
+ });
1193
+
1194
+ it(`Fails when the field is a valid IPv4 address`, async () => {
1195
+ const validator = new Validator({ field: '127.0.0.1' }, rules);
1196
+ assert(await validator.fails());
1197
+ });
1198
+
1199
+ it(`Fails when the field is a regular string`, async () => {
1200
+ const validator = new Validator({ field: 'abc' }, rules);
1201
+ assert(await validator.fails());
1202
+ });
1203
+ });
1204
+
1205
+ describe(`Rule 'json'`, () => {
1206
+ const rules = { field: 'json' };
1207
+
1208
+ it(`Passes when the field is a valid JSON string`, async () => {
1209
+ const validator = new Validator({ field: '[1,2,3]' }, rules);
1210
+ assert(await validator.passes());
1211
+ });
1212
+
1213
+ it(`Fails when the field is an invalid JSON string`, async () => {
1214
+ const validator = new Validator({ field: '[1,2,3' }, rules);
1215
+ assert(await validator.fails());
1216
+ });
1217
+ });
1218
+
1219
+ describe(`Rule 'lowercase'`, () => {
1220
+ const rules = { field: 'lowercase' };
1221
+
1222
+ it(`Passes when the field is fully lowercased`, async () => {
1223
+ const validator = new Validator({ field: 'abc' }, rules);
1224
+ assert(await validator.passes());
1225
+ });
1226
+
1227
+ it(`Fails when the field is partially lowercased`, async () => {
1228
+ const validator = new Validator({ field: 'ABc' }, rules);
1229
+ assert(await validator.fails());
1230
+ });
1231
+
1232
+ it(`Fails when the field is fully uppercased`, async () => {
1233
+ const validator = new Validator({ field: 'ABC' }, rules);
1234
+ assert(await validator.fails());
1235
+ });
1236
+ });
1237
+
1238
+ describe(`Rule 'lt'`, () => {
1239
+ it(`Passes when the array's size is lesser than provided value`, async () => {
1240
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|lt:6' });
1241
+ assert(await validator.passes());
1242
+ });
1243
+
1244
+ it(`Passes when the array's size is lesser than other field`, async () => {
1245
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3, 4, 5, 6] }, { field: 'array|lt:other', other: 'array' });
1246
+ assert(await validator.passes());
1247
+ });
1248
+
1249
+ it(`Passes when the file's size is lesser than provided value`, async () => {
1250
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|lt:6' });
1251
+ assert(await validator.passes());
1252
+ });
1253
+
1254
+ it(`Passes when the file's size is lesser than other field`, async () => {
1255
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 6 * 1024, '') }, { field: 'file|lt:other', other: 'file' });
1256
+ assert(await validator.passes());
1257
+ });
1258
+
1259
+ it(`Passes when the number is lesser than provided value`, async () => {
1260
+ const validator = new Validator({ field: 3 }, { field: 'numeric|lt:6' });
1261
+ assert(await validator.passes());
1262
+ });
1263
+
1264
+ it(`Passes when the number is lesser than other field`, async () => {
1265
+ const validator = new Validator({ field: 3, other: 6 }, { field: 'numeric|lt:other', other: 'numeric' });
1266
+ assert(await validator.passes());
1267
+ });
1268
+
1269
+ it(`Passes when the string's length is lesser than provided value`, async () => {
1270
+ const validator = new Validator({ field: 'abc' }, { field: 'string|lt:6' });
1271
+ assert(await validator.passes());
1272
+ });
1273
+
1274
+ it(`Passes when the string's length is lesser than other field`, async () => {
1275
+ const validator = new Validator({ field: 'abc', other: 'abcdef' }, { field: 'string|lt:other', other: 'string' });
1276
+ assert(await validator.passes());
1277
+ });
1278
+
1279
+ it(`Fails when the array's size is equal to provided value`, async () => {
1280
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|lt:3' });
1281
+ assert(await validator.fails());
1282
+ });
1283
+
1284
+ it(`Fails when the array's size is equal to other field`, async () => {
1285
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3] }, { field: 'array|lt:other', other: 'array' });
1286
+ assert(await validator.fails());
1287
+ });
1288
+
1289
+ it(`Fails when the file's size is equal to provided value`, async () => {
1290
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|lt:3' });
1291
+ assert(await validator.fails());
1292
+ });
1293
+
1294
+ it(`Fails when the file's size is equal to other field`, async () => {
1295
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|lt:other', other: 'file' });
1296
+ assert(await validator.fails());
1297
+ });
1298
+
1299
+ it(`Fails when the number is equal to provided value`, async () => {
1300
+ const validator = new Validator({ field: 3 }, { field: 'numeric|lt:3' });
1301
+ assert(await validator.fails());
1302
+ });
1303
+
1304
+ it(`Fails when the number is equal to other field`, async () => {
1305
+ const validator = new Validator({ field: 3, other: 3 }, { field: 'numeric|lt:other', other: 'numeric' });
1306
+ assert(await validator.fails());
1307
+ });
1308
+
1309
+ it(`Fails when the string's length is equal to provided value`, async () => {
1310
+ const validator = new Validator({ field: 'abc' }, { field: 'string|lt:3' });
1311
+ assert(await validator.fails());
1312
+ });
1313
+
1314
+ it(`Fails when the string's length is equal to other field`, async () => {
1315
+ const validator = new Validator({ field: 'abc', other: 'abc' }, { field: 'string|lt:other', other: 'string' });
1316
+ assert(await validator.fails());
1317
+ });
1318
+
1319
+ it(`Fails when the array's size is greater than provided value`, async () => {
1320
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: 'array|lt:3' });
1321
+ assert(await validator.fails());
1322
+ });
1323
+
1324
+ it(`Fails when the array's size is greater than other field`, async () => {
1325
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6], other: [1, 2, 3] }, { field: 'array|lt:other', other: 'array' });
1326
+ assert(await validator.fails());
1327
+ });
1328
+
1329
+ it(`Fails when the file's size is greater than provided value`, async () => {
1330
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: 'file|lt:3' });
1331
+ assert(await validator.fails());
1332
+ });
1333
+
1334
+ it(`Fails when the file's size is greater than other field`, async () => {
1335
+ const validator = new Validator({ field: new File('', 6 * 1024, ''), other: new File('', 6 * 1024, '') }, { field: 'file|lt:other', other: 'file' });
1336
+ assert(await validator.fails());
1337
+ });
1338
+
1339
+ it(`Fails when the number is greater than provided value`, async () => {
1340
+ const validator = new Validator({ field: 6 }, { field: 'numeric|lt:3' });
1341
+ assert(await validator.fails());
1342
+ });
1343
+
1344
+ it(`Fails when the number is greater than other field`, async () => {
1345
+ const validator = new Validator({ field: 6, other: 3 }, { field: 'numeric|lt:other', other: 'numeric' });
1346
+ assert(await validator.fails());
1347
+ });
1348
+
1349
+ it(`Fails when the string's length is greater than provided value`, async () => {
1350
+ const validator = new Validator({ field: 'abcdef' }, { field: 'string|lt:3' });
1351
+ assert(await validator.fails());
1352
+ });
1353
+
1354
+ it(`Fails when the string's length is greater than other field`, async () => {
1355
+ const validator = new Validator({ field: 'abcdef', other: 'abc' }, { field: 'string|lt:other', other: 'string' });
1356
+ assert(await validator.fails());
1357
+ });
1358
+ });
1359
+
1360
+ describe(`Rule 'lte'`, () => {
1361
+ it(`Passes when the array's size is lesser than provided value`, async () => {
1362
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|lte:6' });
1363
+ assert(await validator.passes());
1364
+ });
1365
+
1366
+ it(`Passes when the array's size is lesser than other field`, async () => {
1367
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3, 4, 5, 6] }, { field: 'array|lte:other', other: 'array' });
1368
+ assert(await validator.passes());
1369
+ });
1370
+
1371
+ it(`Passes when the file's size is lesser than provided value`, async () => {
1372
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|lte:6' });
1373
+ assert(await validator.passes());
1374
+ });
1375
+
1376
+ it(`Passes when the file's size is lesser than other field`, async () => {
1377
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 6 * 1024, '') }, { field: 'file|lte:other', other: 'file' });
1378
+ assert(await validator.passes());
1379
+ });
1380
+
1381
+ it(`Passes when the number is lesser than provided value`, async () => {
1382
+ const validator = new Validator({ field: 3 }, { field: 'numeric|lte:6' });
1383
+ assert(await validator.passes());
1384
+ });
1385
+
1386
+ it(`Passes when the number is lesser than other field`, async () => {
1387
+ const validator = new Validator({ field: 3, other: 6 }, { field: 'numeric|lte:other', other: 'numeric' });
1388
+ assert(await validator.passes());
1389
+ });
1390
+
1391
+ it(`Passes when the string's length is lesser than provided value`, async () => {
1392
+ const validator = new Validator({ field: 'abc' }, { field: 'string|lte:6' });
1393
+ assert(await validator.passes());
1394
+ });
1395
+
1396
+ it(`Passes when the string's length is lesser than other field`, async () => {
1397
+ const validator = new Validator({ field: 'abc', other: 'abcdef' }, { field: 'string|lte:other', other: 'string' });
1398
+ assert(await validator.passes());
1399
+ });
1400
+
1401
+ it(`Passes when the array's size is equal to provided value`, async () => {
1402
+ const validator = new Validator({ field: [1, 2, 3] }, { field: 'array|lte:3' });
1403
+ assert(await validator.passes());
1404
+ });
1405
+
1406
+ it(`Passes when the array's size is equal to other field`, async () => {
1407
+ const validator = new Validator({ field: [1, 2, 3], other: [1, 2, 3] }, { field: 'array|lte:other', other: 'array' });
1408
+ assert(await validator.passes());
1409
+ });
1410
+
1411
+ it(`Passes when the file's size is equal to provided value`, async () => {
1412
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: 'file|lte:3' });
1413
+ assert(await validator.passes());
1414
+ });
1415
+
1416
+ it(`Passes when the file's size is equal to other field`, async () => {
1417
+ const validator = new Validator({ field: new File('', 3 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|lte:other', other: 'file' });
1418
+ assert(await validator.passes());
1419
+ });
1420
+
1421
+ it(`Passes when the number is equal to provided value`, async () => {
1422
+ const validator = new Validator({ field: 3 }, { field: 'numeric|lte:3' });
1423
+ assert(await validator.passes());
1424
+ });
1425
+
1426
+ it(`Passes when the number is equal to other field`, async () => {
1427
+ const validator = new Validator({ field: 3, other: 3 }, { field: 'numeric|lte:other', other: 'numeric' });
1428
+ assert(await validator.passes());
1429
+ });
1430
+
1431
+ it(`Passes when the string's length is equal to provided value`, async () => {
1432
+ const validator = new Validator({ field: 'abc' }, { field: 'string|lte:3' });
1433
+ assert(await validator.passes());
1434
+ });
1435
+
1436
+ it(`Passes when the string's length is equal to other field`, async () => {
1437
+ const validator = new Validator({ field: 'abc', other: 'abc' }, { field: 'string|lte:other', other: 'string' });
1438
+ assert(await validator.passes());
1439
+ });
1440
+
1441
+ it(`Fails when the array's size is greater than provided value`, async () => {
1442
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: 'array|lte:3' });
1443
+ assert(await validator.fails());
1444
+ });
1445
+
1446
+ it(`Fails when the array's size is greater than other field`, async () => {
1447
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6], other: [1, 2, 3] }, { field: 'array|lte:other', other: 'array' });
1448
+ assert(await validator.fails());
1449
+ });
1450
+
1451
+ it(`Fails when the file's size is greater than provided value`, async () => {
1452
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: 'file|lte:3' });
1453
+ assert(await validator.fails());
1454
+ });
1455
+
1456
+ it(`Fails when the file's size is greater than other field`, async () => {
1457
+ const validator = new Validator({ field: new File('', 6 * 1024, ''), other: new File('', 3 * 1024, '') }, { field: 'file|lte:other', other: 'file' });
1458
+ assert(await validator.fails());
1459
+ });
1460
+
1461
+ it(`Fails when the number is greater than provided value`, async () => {
1462
+ const validator = new Validator({ field: 6 }, { field: 'numeric|lte:3' });
1463
+ assert(await validator.fails());
1464
+ });
1465
+
1466
+ it(`Fails when the number is greater than other field`, async () => {
1467
+ const validator = new Validator({ field: 6, other: 3 }, { field: 'numeric|lte:other', other: 'numeric' });
1468
+ assert(await validator.fails());
1469
+ });
1470
+
1471
+ it(`Fails when the string's length is greater than provided value`, async () => {
1472
+ const validator = new Validator({ field: 'abcdef' }, { field: 'string|lte:3' });
1473
+ assert(await validator.fails());
1474
+ });
1475
+
1476
+ it(`Fails when the string's length is greater than other field`, async () => {
1477
+ const validator = new Validator({ field: 'abcdef', other: 'abc' }, { field: 'string|lte:other', other: 'string' });
1478
+ assert(await validator.fails());
1479
+ });
1480
+ });
1481
+
1482
+ describe(`Rule 'mac_address'`, () => {
1483
+ const rules = { field: 'mac_address' };
1484
+
1485
+ it(`Passes when the field contains a valid MAC address`, async () => {
1486
+ const validator = new Validator({ field: '01-23-45-67-89-AB' }, rules);
1487
+ assert(await validator.passes());
1488
+
1489
+ validator.setData({ field: '01:23:45:67:89:AB' });
1490
+ assert(await validator.passes());
1491
+
1492
+ validator.setData({ field: '0123.4567.89AB' });
1493
+ assert(await validator.passes());
1494
+ });
1495
+
1496
+ it(`Fails when the field contains an invalid MAC address`, async () => {
1497
+ const validator = new Validator({ field: 'X1-23-45-67-89-AB' }, rules);
1498
+ assert(await validator.fails());
1499
+
1500
+ validator.setData({ field: '01:23:45:67:89' });
1501
+ assert(await validator.fails());
1502
+
1503
+ validator.setData({ field: '0123.4567.89AB.1337' });
1504
+ assert(await validator.fails());
1505
+ });
1506
+ });
1507
+
1508
+ describe(`Rule 'max'`, () => {
1509
+ const max = 5;
1510
+
1511
+ it(`Passes when the array's size is lesser than ${max}`, async () => {
1512
+ const validator = new Validator({ field: [1, 2, 3] }, { field: `array|max:${max}` });
1513
+ assert(await validator.passes());
1514
+ });
1515
+
1516
+ it(`Passes when the array's size is equal to ${max}`, async () => {
1517
+ const validator = new Validator({ field: [1, 2, 3, 4, 5] }, { field: `array|max:${max}` });
1518
+ assert(await validator.passes());
1519
+ });
1520
+
1521
+ it(`Fails when the array's size is greater than ${max}`, async () => {
1522
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: `array|max:${max}` });
1523
+ assert(await validator.fails());
1524
+ });
1525
+
1526
+ it(`Passes when the file's size is lesser than ${max}`, async () => {
1527
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: `file|max:${max}` });
1528
+ assert(await validator.passes());
1529
+ });
1530
+
1531
+ it(`Passes when the file's size is equal to ${max}`, async () => {
1532
+ const validator = new Validator({ field: new File('', 5 * 1024, '') }, { field: `file|max:${max}` });
1533
+ assert(await validator.passes());
1534
+ });
1535
+
1536
+ it(`Fails when the file's size is greater than ${max}`, async () => {
1537
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: `file|max:${max}` });
1538
+ assert(await validator.fails());
1539
+ });
1540
+
1541
+ it(`Passes when the number is lesser than ${max}`, async () => {
1542
+ const validator = new Validator({ field: 4 }, { field: `numeric|max:${max}` });
1543
+ assert(await validator.passes());
1544
+ });
1545
+
1546
+ it(`Passes when the number is equal to ${max}`, async () => {
1547
+ const validator = new Validator({ field: 5 }, { field: `numeric|max:${max}` });
1548
+ assert(await validator.passes());
1549
+ });
1550
+
1551
+ it(`Fails when the number is greater than ${max}`, async () => {
1552
+ const validator = new Validator({ field: 9 }, { field: `numeric|max:${max}` });
1553
+ assert(await validator.fails());
1554
+ });
1555
+
1556
+ it(`Passes when the string's length is lesser than ${max}`, async () => {
1557
+ const validator = new Validator({ field: 'abcd' }, { field: `string|max:${max}` });
1558
+ assert(await validator.passes());
1559
+ });
1560
+
1561
+ it(`Passes when the string's length is equal to ${max}`, async () => {
1562
+ const validator = new Validator({ field: 'abcde' }, { field: `string|max:${max}` });
1563
+ assert(await validator.passes());
1564
+ });
1565
+
1566
+ it(`Fails when the string's length is greater than ${max}`, async () => {
1567
+ const validator = new Validator({ field: 'abcdefg' }, { field: `string|max:${max}` });
1568
+ assert(await validator.fails());
1569
+ });
1570
+ });
1571
+
1572
+ describe(`Rule 'max_digits'`, () => {
1573
+ const rules = { field: 'max_digits:3' };
1574
+
1575
+ it(`Passes when the field has 3 digits`, async () => {
1576
+ const validator = new Validator({ field: '123' }, rules);
1577
+ assert(await validator.passes());
1578
+ });
1579
+
1580
+ it(`Passes when the field has less than 3 digits`, async () => {
1581
+ const validator = new Validator({ field: '12' }, rules);
1582
+ assert(await validator.passes());
1583
+ });
1584
+
1585
+ it(`Fails when the field has more than 3 digits`, async () => {
1586
+ const validator = new Validator({ field: '1234' }, rules);
1587
+ assert(await validator.fails());
1588
+ });
1589
+ });
1590
+
1591
+ describe(`Rule 'mimes'`, () => {
1592
+ const rules = { field: 'mimes:jpg,png' };
1593
+
1594
+ it(`Passes when the field has a valid extension`, async () => {
1595
+ const validator = new Validator({ field: new File('hello.jpg', 5 * 1024, 'image/jpg') }, rules);
1596
+ assert(await validator.passes());
1597
+
1598
+ validator.setData({ field: new File('hello.png', 5 * 1024, 'image/png') });
1599
+ assert(await validator.passes());
1600
+ });
1601
+
1602
+ it(`Fails when the field has an invalid extension`, async () => {
1603
+ const validator = new Validator({ field: new File('hello.doc', 5 * 1024, 'application/msword') }, rules);
1604
+ assert(await validator.fails());
1605
+
1606
+ validator.setData({ field: new File('hello.txt', 5 * 1024, 'text/plain') });
1607
+ assert(await validator.fails());
1608
+ });
1609
+ });
1610
+
1611
+ describe(`Rule 'mimetypes'`, () => {
1612
+ const rules = { field: 'mimetypes:image/jpg,image/png' };
1613
+
1614
+ it(`Passes when the field has a valid type`, async () => {
1615
+ const validator = new Validator({ field: new File('hello.jpg', 5 * 1024, 'image/jpg') }, rules);
1616
+ assert(await validator.passes());
1617
+
1618
+ validator.setData({ field: new File('hello.png', 5 * 1024, 'image/png') });
1619
+ assert(await validator.passes());
1620
+ });
1621
+
1622
+ it(`Fails when the field has an invalid type`, async () => {
1623
+ const validator = new Validator({ field: new File('hello.doc', 5 * 1024, 'application/msword') }, rules);
1624
+ assert(await validator.fails());
1625
+
1626
+ validator.setData({ field: new File('hello.txt', 5 * 1024, 'text/plain') });
1627
+ assert(await validator.fails());
1628
+ });
1629
+ });
1630
+
1631
+ describe(`Rule 'min'`, () => {
1632
+ const min = 5;
1633
+
1634
+ it(`Passes when the array's size is greater than ${min}`, async () => {
1635
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: `array|min:${min}` });
1636
+ assert(await validator.passes());
1637
+ });
1638
+
1639
+ it(`Passes when the array's size is equal to ${min}`, async () => {
1640
+ const validator = new Validator({ field: [1, 2, 3, 4, 5] }, { field: `array|min:${min}` });
1641
+ assert(await validator.passes());
1642
+ });
1643
+
1644
+ it(`Fails when the array's size is lesser than ${min}`, async () => {
1645
+ const validator = new Validator({ field: [1, 2, 3] }, { field: `array|min:${min}` });
1646
+ assert(await validator.fails());
1647
+ });
1648
+
1649
+ it(`Passes when the file's size is greater than ${min}`, async () => {
1650
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: `file|min:${min}` });
1651
+ assert(await validator.passes());
1652
+ });
1653
+
1654
+ it(`Passes when the file's size is equal to ${min}`, async () => {
1655
+ const validator = new Validator({ field: new File('', 5 * 1024, '') }, { field: `file|min:${min}` });
1656
+ assert(await validator.passes());
1657
+ });
1658
+
1659
+ it(`Fails when the file's size is lesser than ${min}`, async () => {
1660
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: `file|min:${min}` });
1661
+ assert(await validator.fails());
1662
+ });
1663
+
1664
+ it(`Passes when the number is greater than ${min}`, async () => {
1665
+ const validator = new Validator({ field: 9 }, { field: `numeric|min:${min}` });
1666
+ assert(await validator.passes());
1667
+ });
1668
+
1669
+ it(`Passes when the number is equal to ${min}`, async () => {
1670
+ const validator = new Validator({ field: 5 }, { field: `numeric|min:${min}` });
1671
+ assert(await validator.passes());
1672
+ });
1673
+
1674
+ it(`Fails when the number is lesser than ${min}`, async () => {
1675
+ const validator = new Validator({ field: 4 }, { field: `numeric|min:${min}` });
1676
+ assert(await validator.fails());
1677
+ });
1678
+
1679
+ it(`Passes when the string's length is greater than ${min}`, async () => {
1680
+ const validator = new Validator({ field: 'abcdefg' }, { field: `string|min:${min}` });
1681
+ assert(await validator.passes());
1682
+ });
1683
+
1684
+ it(`Passes when the string's length is equal to ${min}`, async () => {
1685
+ const validator = new Validator({ field: 'abcde' }, { field: `string|min:${min}` });
1686
+ assert(await validator.passes());
1687
+ });
1688
+
1689
+ it(`Fails when the string's length is lesser than ${min}`, async () => {
1690
+ const validator = new Validator({ field: 'abcd' }, { field: `string|min:${min}` });
1691
+ assert(await validator.fails());
1692
+ });
1693
+ });
1694
+
1695
+ describe(`Rule 'min_digits'`, () => {
1696
+ const rules = { field: 'min_digits:3' };
1697
+
1698
+ it(`Passes when the field has 3 digits`, async () => {
1699
+ const validator = new Validator({ field: '123' }, rules);
1700
+ assert(await validator.passes());
1701
+ });
1702
+
1703
+ it(`Passes when the field has more than 3 digits`, async () => {
1704
+ const validator = new Validator({ field: '1234' }, rules);
1705
+ assert(await validator.passes());
1706
+ });
1707
+
1708
+ it(`Fails when the field has less than 3 digits`, async () => {
1709
+ const validator = new Validator({ field: '12' }, rules);
1710
+ assert(await validator.fails());
1711
+ });
1712
+ });
1713
+
1714
+ describe(`Rule 'missing'`, () => {
1715
+ const rules = { field: 'missing' };
1716
+
1717
+ it(`Passes when the field is not present`, async () => {
1718
+ const validator = new Validator({}, rules);
1719
+ assert(await validator.passes());
1720
+ });
1721
+
1722
+ it(`Fails when the field is empty or null`, async () => {
1723
+ const validator = new Validator({ field: '' }, rules);
1724
+ assert(await validator.fails());
1725
+
1726
+ validator.setData({ field: null });
1727
+ assert(await validator.fails());
1728
+ });
1729
+
1730
+ it(`Fails when the field is filled`, async () => {
1731
+ const validator = new Validator({ field: 'abc' }, rules);
1732
+ assert(await validator.fails());
1733
+ });
1734
+ });
1735
+
1736
+ describe(`Rule 'missing_if'`, () => {
1737
+ const rules = { field: 'missing_if:other,foo,bar' };
1738
+
1739
+ it(`Passes when the field is present and the other field's value is not equal to any value`, async () => {
1740
+ const validator = new Validator({ field: '', other: 'bob' }, rules);
1741
+ assert(await validator.passes());
1742
+ });
1743
+
1744
+ it(`Fails when the field is present and the other field's value is equal to the first value`, async () => {
1745
+ const validator = new Validator({ field: '', other: 'foo' }, rules);
1746
+ assert(await validator.fails());
1747
+ });
1748
+
1749
+ it(`Fails when the field is present and the other field's value is equal to the second value`, async () => {
1750
+ const validator = new Validator({ field: '', other: 'bar' }, rules);
1751
+ assert(await validator.fails());
1752
+ });
1753
+ });
1754
+
1755
+ describe(`Rule 'missing_unless'`, () => {
1756
+ const rules = { field: 'missing_unless:other,foo,bar' };
1757
+
1758
+ it(`Passes when the field is not present and the other field's value is equal to any value`, async () => {
1759
+ const validator = new Validator({ other: 'foo' }, rules);
1760
+ assert(await validator.passes());
1761
+ });
1762
+
1763
+ it(`Passes when the field is present and the other field's value is equal to the first value`, async () => {
1764
+ const validator = new Validator({ field: '', other: 'foo' }, rules);
1765
+ assert(await validator.passes());
1766
+ });
1767
+
1768
+ it(`Passes when the field is present and the other field's value is equal to the second value`, async () => {
1769
+ const validator = new Validator({ field: '', other: 'bar' }, rules);
1770
+ assert(await validator.passes());
1771
+ });
1772
+
1773
+ it(`Fails when the field is present and the other field's value is not equal to any value`, async () => {
1774
+ const validator = new Validator({ field: '', other: 'bob' }, rules);
1775
+ assert(await validator.fails());
1776
+ });
1777
+ });
1778
+
1779
+ describe(`Rule 'missing_with'`, () => {
1780
+ const rules = { field: 'missing_with:foo,bar' };
1781
+
1782
+ it(`Passes when the field is present and no other fields are present`, async () => {
1783
+ const validator = new Validator({ field: '' }, rules);
1784
+ assert(await validator.passes());
1785
+ });
1786
+
1787
+ it(`Passes when the field is not present and no other fields present`, async () => {
1788
+ const validator = new Validator({ foo: '' }, rules);
1789
+ assert(await validator.passes());
1790
+ });
1791
+
1792
+ it(`Fails when the field and the first field are present`, async () => {
1793
+ const validator = new Validator({ field: '', foo: '' }, rules);
1794
+ assert(await validator.fails());
1795
+ });
1796
+
1797
+ it(`Fails when the field and the second field are present`, async () => {
1798
+ const validator = new Validator({ field: '', bar: '' }, rules);
1799
+ assert(await validator.fails());
1800
+ });
1801
+ });
1802
+
1803
+ describe(`Rule 'missing_with_all'`, () => {
1804
+ const rules = { field: 'missing_with_all:foo,bar' };
1805
+
1806
+ it(`Passes when the field is present and all other fields are not present`, async () => {
1807
+ const validator = new Validator({ field: '' }, rules);
1808
+ assert(await validator.passes());
1809
+ });
1810
+
1811
+ it(`Passes when the field is not present and all other fields are present`, async () => {
1812
+ const validator = new Validator({ foo: '', bar: '' }, rules);
1813
+ assert(await validator.passes());
1814
+ });
1815
+
1816
+ it(`Passes when the field is present and any other field is present`, async () => {
1817
+ const validator = new Validator({ field: '', foo: '' }, rules);
1818
+ assert(await validator.passes());
1819
+ });
1820
+
1821
+ it(`Fails when the field is present and all other fields are present`, async () => {
1822
+ const validator = new Validator({ field: '', foo: '', bar: '' }, rules);
1823
+ assert(await validator.fails());
1824
+ });
1825
+ });
1826
+
1827
+ describe(`Rule 'multiple_of'`, () => {
1828
+ it(`Passes when the field is multiple of 4`, async () => {
1829
+ const validator = new Validator({ field: 16 }, { field: 'multiple_of:4' });
1830
+ assert(await validator.passes());
1831
+ });
1832
+
1833
+ it(`Passes when the field is multiple of 11`, async () => {
1834
+ const validator = new Validator({ field: 66 }, { field: 'multiple_of:11' });
1835
+ assert(await validator.passes());
1836
+ });
1837
+
1838
+ it(`Fails when the field is not multiple of 4`, async () => {
1839
+ const validator = new Validator({ field: 21 }, { field: 'multiple_of:4' });
1840
+ assert(await validator.fails());
1841
+ });
1842
+ it(`Fails when the field is not multiple of 11`, async () => {
1843
+ const validator = new Validator({ field: 58 }, { field: 'multiple_of:11' });
1844
+ assert(await validator.fails());
1845
+ });
1846
+ });
1847
+
1848
+ describe(`Rule 'not_in'`, () => {
1849
+ it(`Passes when the field's value is not in the list`, async () => {
1850
+ const validator = new Validator({ field: 'a' }, { field: 'not_in:x,y,z' });
1851
+ assert(await validator.passes());
1852
+ });
1853
+
1854
+ it(`Passes when the field's values are not in the list`, async () => {
1855
+ const validator = new Validator({ field: ['j', 'k'] }, { field: 'array|not_in:x,y,z' });
1856
+ assert(await validator.passes());
1857
+ });
1858
+
1859
+ it(`Fails when the field's value is in the list`, async () => {
1860
+ const validator = new Validator({ field: 'z' }, { field: 'not_in:x,y,z' });
1861
+ assert(await validator.fails());
1862
+ });
1863
+
1864
+ it(`Fails when the field's values are in the list`, async () => {
1865
+ const validator = new Validator({ field: ['x', 'z'] }, { field: 'array|not_in:x,y,z' });
1866
+ assert(await validator.fails());
1867
+ });
1868
+ });
1869
+
1870
+ describe(`Rule 'not_regex'`, () => {
1871
+ it(`Passes when the field does not contain alphabets`, async () => {
1872
+ const validator = new Validator({ field: '!123' }, { field: 'not_regex:/^[a-z]+$/i' });
1873
+ assert(await validator.passes());
1874
+ });
1875
+
1876
+ it(`Passes when the field does not contain numbers`, async () => {
1877
+ const validator = new Validator({ field: 'abc' }, { field: 'not_regex:/^[0-9]+$/' });
1878
+ assert(await validator.passes());
1879
+ });
1880
+
1881
+ it(`Passes when the field does not contain symbols`, async () => {
1882
+ const validator = new Validator({ field: 'abc123' }, { field: 'not_regex:/^[^a-z0-9]+$/i' });
1883
+ assert(await validator.passes());
1884
+ });
1885
+ });
1886
+
1887
+ describe(`Rule 'numeric'`, () => {
1888
+ const rules = { field: 'numeric' };
1889
+
1890
+ it(`Passes when the field is a number`, async () => {
1891
+ const validator = new Validator({ field: 123 }, rules);
1892
+ assert(await validator.passes());
1893
+
1894
+ validator.setData({ field: '123.45' });
1895
+ assert(await validator.passes());
1896
+ });
1897
+
1898
+ it(`Fails when the field is not a number`, async () => {
1899
+ const validator = new Validator({ field: null }, rules);
1900
+ assert(await validator.fails());
1901
+
1902
+ validator.setData({ field: true });
1903
+ assert(await validator.fails());
1904
+
1905
+ validator.setData({ field: 'abc' });
1906
+ assert(await validator.fails());
1907
+
1908
+ validator.setData({ field: [1, 2, 3] });
1909
+ assert(await validator.fails());
1910
+ });
1911
+ });
1912
+
1913
+ describe(`Rule 'present'`, () => {
1914
+ const rules = { field: 'present' };
1915
+
1916
+ it(`Passes when the field is present`, async () => {
1917
+ const validator = new Validator({ field: '' }, rules);
1918
+ assert(await validator.passes());
1919
+ });
1920
+
1921
+ it(`Fails when the field is not present`, async () => {
1922
+ const validator = new Validator({}, rules);
1923
+ assert(await validator.fails());
1924
+ });
1925
+ });
1926
+
1927
+ describe(`Rule 'prohibited'`, () => {
1928
+ const rules = { field: 'prohibited' };
1929
+
1930
+ it(`Passes when the field is not present`, async () => {
1931
+ const validator = new Validator({}, rules);
1932
+ assert(await validator.passes());
1933
+ });
1934
+
1935
+ it(`Passes when the field is empty`, async () => {
1936
+ const validator = new Validator({ field: '' }, rules);
1937
+ assert(await validator.passes());
1938
+ });
1939
+
1940
+ it(`Passes when the field is null`, async () => {
1941
+ const validator = new Validator({ field: null }, rules);
1942
+ assert(await validator.passes());
1943
+ });
1944
+
1945
+ it(`Passes when the array's size is 0`, async () => {
1946
+ const validator = new Validator({ field: [] }, rules);
1947
+ assert(await validator.passes());
1948
+ });
1949
+
1950
+ it(`Fails when the field is filled`, async () => {
1951
+ const validator = new Validator({ field: 'abc' }, rules);
1952
+ assert(await validator.fails());
1953
+ });
1954
+ });
1955
+
1956
+ describe(`Rule 'prohibited_if'`, () => {
1957
+ const rules = { field: 'prohibited_if:other,foo,bar' };
1958
+
1959
+ it(`Passes when the field is filled and the other field's value is not equal to any value`, async () => {
1960
+ const validator = new Validator({ field: 'abc', other: 'bob' }, rules);
1961
+ assert(await validator.passes());
1962
+ });
1963
+
1964
+ it(`Fails when the field is filled and the other field's value is equal to the first value`, async () => {
1965
+ const validator = new Validator({ field: 'abc', other: 'foo' }, rules);
1966
+ assert(await validator.fails());
1967
+ });
1968
+
1969
+ it(`Fails when the field is filled and the other field's value is equal to the second value`, async () => {
1970
+ const validator = new Validator({ field: 'abc', other: 'bar' }, rules);
1971
+ assert(await validator.fails());
1972
+ });
1973
+ });
1974
+
1975
+ describe(`Rule 'prohibited_unless'`, () => {
1976
+ const rules = { field: 'prohibited_unless:other,foo,bar' };
1977
+
1978
+ it(`Passes when the field is filled and the other field's value is equal to the first value`, async () => {
1979
+ const validator = new Validator({ field: 'abc', other: 'foo' }, rules);
1980
+ assert(await validator.passes());
1981
+ });
1982
+
1983
+ it(`Passes when the field is filled and the other field's value is equal to the second value`, async () => {
1984
+ const validator = new Validator({ field: 'abc', other: 'bar' }, rules);
1985
+ assert(await validator.passes());
1986
+ });
1987
+
1988
+ it(`Passes when the field is empty and the other field's value is not equal to any value`, async () => {
1989
+ const validator = new Validator({ field: '', other: 'foo' }, rules);
1990
+ assert(await validator.passes());
1991
+ });
1992
+
1993
+ it(`Fails when the field is filled and the other field's value is not equal to any value`, async () => {
1994
+ const validator = new Validator({ field: 'abc', other: 'bob' }, rules);
1995
+ assert(await validator.fails());
1996
+ });
1997
+ });
1998
+
1999
+ describe(`Rule 'prohibits'`, () => {
2000
+ const rules = { field: 'prohibits:foo,bar' };
2001
+
2002
+ it(`Passes when the field is filled and all other fields are empty or null`, async () => {
2003
+ const validator = new Validator({ field: 'abc', foo: '', bar: null }, rules);
2004
+ assert(await validator.passes());
2005
+ });
2006
+
2007
+ it(`Passes when the field is empty and all other fields are filled`, async () => {
2008
+ const validator = new Validator({ field: '', foo: 'abc', bar: 123 }, rules);
2009
+ assert(await validator.passes());
2010
+ });
2011
+
2012
+ it(`Fails when the field is filled and all other fields are filled`, async () => {
2013
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: 123 }, rules);
2014
+ assert(await validator.fails());
2015
+ });
2016
+
2017
+ it(`Fails when the field is filled and any other field is filled`, async () => {
2018
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: null }, rules);
2019
+ assert(await validator.fails());
2020
+ });
2021
+ });
2022
+
2023
+ describe(`Rule 'regex'`, () => {
2024
+ it(`Passes when the field contains alphabets`, async () => {
2025
+ const validator = new Validator({ field: 'abc' }, { field: 'regex:/^[a-z]+$/i' });
2026
+ assert(await validator.passes());
2027
+ });
2028
+
2029
+ it(`Passes when the field contains numbers`, async () => {
2030
+ const validator = new Validator({ field: '123' }, { field: 'regex:/^[0-9]+$/' });
2031
+ assert(await validator.passes());
2032
+ });
2033
+
2034
+ it(`Passes when the field contains symbols`, async () => {
2035
+ const validator = new Validator({ field: '!@#' }, { field: 'regex:/^[^a-z0-9]+$/i' });
2036
+ assert(await validator.passes());
2037
+ });
2038
+ });
2039
+
2040
+ describe(`Rule 'required'`, () => {
2041
+ const rules = { field: 'required' };
2042
+
2043
+ it(`Passes when the field is filled`, async () => {
2044
+ const validator = new Validator({ field: 'abc' }, rules);
2045
+ assert(await validator.passes());
2046
+ });
2047
+
2048
+ it(`Fails when the field is not present, empty or null`, async () => {
2049
+ const validator = new Validator({}, rules);
2050
+ assert(await validator.fails());
2051
+
2052
+ validator.setData({ field: '' });
2053
+ assert(await validator.fails());
2054
+
2055
+ validator.setData({ field: null });
2056
+ assert(await validator.fails());
2057
+ });
2058
+
2059
+ it(`Fails when the array's size is 0`, async () => {
2060
+ const validator = new Validator({ field: [] }, rules);
2061
+ assert(await validator.fails());
2062
+ });
2063
+
2064
+ it(`Fails when the file's size is 0`, async () => {
2065
+ const validator = new Validator({ field: new File('', 0, '') }, rules);
2066
+ assert(await validator.fails());
2067
+ });
2068
+ });
2069
+
2070
+ describe(`Rule 'required_array_keys'`, () => {
2071
+ const rules = { field: 'required_array_keys:x,y,z' };
2072
+
2073
+ it(`Passes when all required keys are present`, async () => {
2074
+ const validator = new Validator({ field: { x: 1, y: 2, z: 3 } }, rules);
2075
+ assert(await validator.passes());
2076
+ });
2077
+
2078
+ it(`Passes when all required keys are present, along with undeclared keys`, async () => {
2079
+ const validator = new Validator({ field: { w: 0, x: 1, y: 2, z: 3 } }, rules);
2080
+ assert(await validator.passes());
2081
+ });
2082
+
2083
+ it(`Fails when the any required key is missing`, async () => {
2084
+ const validator = new Validator({ field: { x: 1, y: 2 } }, rules);
2085
+ assert(await validator.fails());
2086
+ });
2087
+ });
2088
+
2089
+ describe(`Rule 'required_if'`, () => {
2090
+ const rules = { field: 'required_if:other,foo,bar' };
2091
+
2092
+ it(`Passes when the field is filled and the other field's value is equal to the first value`, async () => {
2093
+ const validator = new Validator({ field: 'abc', other: 'foo' }, rules);
2094
+ assert(await validator.passes());
2095
+ });
2096
+
2097
+ it(`Passes when the field is filled and the other field's value is equal to the second value`, async () => {
2098
+ const validator = new Validator({ field: 'abc', other: 'bar' }, rules);
2099
+ assert(await validator.passes());
2100
+ });
2101
+
2102
+ it(`Passes when the field is filled and the other field's value is not equal to any value`, async () => {
2103
+ const validator = new Validator({ field: 'abc', other: 'bob' }, rules);
2104
+ assert(await validator.passes());
2105
+ });
2106
+
2107
+ it(`Fails when the field is empty and the other field's value is equal to any value`, async () => {
2108
+ const validator = new Validator({ field: '', other: 'bar' }, rules);
2109
+ assert(await validator.fails());
2110
+ });
2111
+ });
2112
+
2113
+ describe(`Rule 'required_if_accepted'`, () => {
2114
+ const rules = { field: 'required_if_accepted:foo' };
2115
+
2116
+ it(`Passes when the field is filled and the other field is accepted`, async () => {
2117
+ const validator = new Validator({ field: 'abc', foo: true }, rules);
2118
+ assert(await validator.passes());
2119
+ });
2120
+
2121
+ it(`Passes when the field is filled and the other field is declined`, async () => {
2122
+ const validator = new Validator({ field: 'abc', foo: false }, rules);
2123
+ assert(await validator.passes());
2124
+ });
2125
+
2126
+ it(`Passes when the field is empty and the other field is declined`, async () => {
2127
+ const validator = new Validator({ field: '', foo: false }, rules);
2128
+ assert(await validator.passes());
2129
+ });
2130
+
2131
+ it(`Fails when the field is empty and the other field is declined`, async () => {
2132
+ const validator = new Validator({ field: '', foo: true }, rules);
2133
+ assert(await validator.fails());
2134
+ });
2135
+ });
2136
+
2137
+ describe(`Rule 'required_unless'`, () => {
2138
+ const rules = { field: 'required_unless:other,foo,bar' };
2139
+
2140
+ it(`Passes when the field is empty and the other field's value is equal to the first value`, async () => {
2141
+ const validator = new Validator({ field: '', other: 'foo' }, rules);
2142
+ assert(await validator.passes());
2143
+ });
2144
+
2145
+ it(`Passes when the field is empty and the other field's value is equal to the second value`, async () => {
2146
+ const validator = new Validator({ field: '', other: 'bar' }, rules);
2147
+ assert(await validator.passes());
2148
+ });
2149
+
2150
+ it(`Passes when the field is filled and the other field's value is not equal to any value`, async () => {
2151
+ const validator = new Validator({ field: 'abc', other: 'bob' }, rules);
2152
+ assert(await validator.passes());
2153
+ });
2154
+
2155
+ it(`Fails when the field is empty and the other field's value is not equal to any value`, async () => {
2156
+ const validator = new Validator({ field: '', other: 'bob' }, rules);
2157
+ assert(await validator.fails());
2158
+ });
2159
+ });
2160
+
2161
+ describe(`Rule 'required_with'`, () => {
2162
+ const rules = { field: 'required_with:foo,bar' };
2163
+
2164
+ it(`Passes when the field is filled and the first field is filled`, async () => {
2165
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: '' }, rules);
2166
+ assert(await validator.passes());
2167
+ });
2168
+
2169
+ it(`Passes when the field is filled and the second field is filled`, async () => {
2170
+ const validator = new Validator({ field: 'abc', foo: '', bar: 123 }, rules);
2171
+ assert(await validator.passes());
2172
+ });
2173
+
2174
+ it(`Passes when the field is filled and all other fields are empty`, async () => {
2175
+ const validator = new Validator({ field: 'abc', foo: '', bar: '' }, rules);
2176
+ assert(await validator.passes());
2177
+ });
2178
+
2179
+ it(`Passes when the field is empty and all other fields are empty`, async () => {
2180
+ const validator = new Validator({ field: '', foo: '', bar: '' }, rules);
2181
+ assert(await validator.passes());
2182
+ });
2183
+
2184
+ it(`Fails when the field is empty and the first field is filled`, async () => {
2185
+ const validator = new Validator({ field: '', foo: 'abc', bar: '' }, rules);
2186
+ assert(await validator.fails());
2187
+ });
2188
+
2189
+ it(`Fails when the field is empty and the second field is filled`, async () => {
2190
+ const validator = new Validator({ field: '', foo: '', bar: 123 }, rules);
2191
+ assert(await validator.fails());
2192
+ });
2193
+ });
2194
+
2195
+ describe(`Rule 'required_with_all'`, () => {
2196
+ const rules = { field: 'required_with_all:foo,bar' };
2197
+
2198
+ it(`Passes when the field is filled and all fields are filled`, async () => {
2199
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: 123 }, rules);
2200
+ assert(await validator.passes());
2201
+ });
2202
+
2203
+ it(`Passes when the field is filled and any field is filled`, async () => {
2204
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: '' }, rules);
2205
+ assert(await validator.passes());
2206
+ });
2207
+
2208
+ it(`Passes when the field is empty and all fields are empty`, async () => {
2209
+ const validator = new Validator({ field: '', foo: '', bar: '' }, rules);
2210
+ assert(await validator.passes());
2211
+ });
2212
+
2213
+ it(`Passes when the field is empty and any field is empty`, async () => {
2214
+ const validator = new Validator({ field: '', foo: 'abc', bar: '' }, rules);
2215
+ assert(await validator.passes());
2216
+ });
2217
+
2218
+ it(`Fails when the field is empty and all fields are filled`, async () => {
2219
+ const validator = new Validator({ field: '', foo: 'abc', bar: 123 }, rules);
2220
+ assert(await validator.fails());
2221
+ });
2222
+ });
2223
+
2224
+ describe(`Rule 'required_without'`, () => {
2225
+ const rules = { field: 'required_without:foo,bar' };
2226
+
2227
+ it(`Passes when the field is filled and the first field is empty`, async () => {
2228
+ const validator = new Validator({ field: 'abc', foo: '', bar: 123 }, rules);
2229
+ assert(await validator.passes());
2230
+ });
2231
+
2232
+ it(`Passes when the field is filled and the second field is empty`, async () => {
2233
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: '' }, rules);
2234
+ assert(await validator.passes());
2235
+ });
2236
+
2237
+ it(`Passes when the field is filled and all other fields are empty`, async () => {
2238
+ const validator = new Validator({ field: 'abc', foo: '', bar: '' }, rules);
2239
+ assert(await validator.passes());
2240
+ });
2241
+
2242
+ it(`Fails when the field is empty and all other fields are empty`, async () => {
2243
+ const validator = new Validator({ field: '', foo: '', bar: '' }, rules);
2244
+ assert(await validator.fails());
2245
+ });
2246
+
2247
+ it(`Fails when the field is empty and the first field is filled`, async () => {
2248
+ const validator = new Validator({ field: '', foo: 'abc', bar: '' }, rules);
2249
+ assert(await validator.fails());
2250
+ });
2251
+
2252
+ it(`Fails when the field is empty and the second field is filled`, async () => {
2253
+ const validator = new Validator({ field: '', foo: '', bar: 123 }, rules);
2254
+ assert(await validator.fails());
2255
+ });
2256
+ });
2257
+
2258
+ describe(`Rule 'required_without_all'`, () => {
2259
+ const rules = { field: 'required_without_all:foo,bar' };
2260
+
2261
+ it(`Passes when the field is filled and all fields are filled`, async () => {
2262
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: 123 }, rules);
2263
+ assert(await validator.passes());
2264
+ });
2265
+
2266
+ it(`Passes when the field is filled and any field is filled`, async () => {
2267
+ const validator = new Validator({ field: 'abc', foo: 'abc', bar: '' }, rules);
2268
+ assert(await validator.passes());
2269
+ });
2270
+
2271
+ it(`Passes when the field is empty and any field is empty`, async () => {
2272
+ const validator = new Validator({ field: '', foo: 'abc', bar: '' }, rules);
2273
+ assert(await validator.passes());
2274
+ });
2275
+
2276
+ it(`Passes when the field is empty and all fields are filled`, async () => {
2277
+ const validator = new Validator({ field: '', foo: 'abc', bar: 123 }, rules);
2278
+ assert(await validator.passes());
2279
+ });
2280
+
2281
+ it(`Fails when the field is empty and all fields are empty`, async () => {
2282
+ const validator = new Validator({ field: '', foo: '', bar: '' }, rules);
2283
+ assert(await validator.fails());
2284
+ });
2285
+ });
2286
+
2287
+ describe(`Rule 'same'`, () => {
2288
+ const rules = { field: 'same:other' };
2289
+
2290
+ it(`Passes when the field is equal to the other field`, async () => {
2291
+ const validator = new Validator({ field: 'foo', other: 'foo' }, rules);
2292
+ assert(await validator.passes());
2293
+ });
2294
+
2295
+ it(`Fails when the field is different to the other field`, async () => {
2296
+ const validator = new Validator({ field: 'foo', other: 'bar' }, rules);
2297
+ assert(await validator.fails());
2298
+ });
2299
+
2300
+ it(`Fails when the other field is not present`, async () => {
2301
+ const validator = new Validator({ field: 'foo' }, rules);
2302
+ assert(await validator.fails());
2303
+ });
2304
+ });
2305
+
2306
+ describe(`Rule 'size'`, () => {
2307
+ const size = 5;
2308
+
2309
+ it(`Passes when the array's size is equal to ${size}`, async () => {
2310
+ const validator = new Validator({ field: [1, 2, 3, 4, 5] }, { field: `array|size:${size}` });
2311
+ assert(await validator.passes());
2312
+ });
2313
+
2314
+ it(`Fails when the array's size is lesser than ${size}`, async () => {
2315
+ const validator = new Validator({ field: [1, 2, 3] }, { field: `array|size:${size}` });
2316
+ assert(await validator.fails());
2317
+ });
2318
+
2319
+ it(`Fails when the array's size is greater than ${size}`, async () => {
2320
+ const validator = new Validator({ field: [1, 2, 3, 4, 5, 6] }, { field: `array|size:${size}` });
2321
+ assert(await validator.fails());
2322
+ });
2323
+
2324
+ it(`Passes when the file's size is equal to ${size}`, async () => {
2325
+ const validator = new Validator({ field: new File('', 5 * 1024, '') }, { field: `file|size:${size}` });
2326
+ assert(await validator.passes());
2327
+ });
2328
+
2329
+ it(`Fails when the file's size is lesser than ${size}`, async () => {
2330
+ const validator = new Validator({ field: new File('', 3 * 1024, '') }, { field: `file|size:${size}` });
2331
+ assert(await validator.fails());
2332
+ });
2333
+
2334
+ it(`Fails when the file's size is greater than ${size}`, async () => {
2335
+ const validator = new Validator({ field: new File('', 6 * 1024, '') }, { field: `file|size:${size}` });
2336
+ assert(await validator.fails());
2337
+ });
2338
+
2339
+ it(`Passes when the number is equal to ${size}`, async () => {
2340
+ const validator = new Validator({ field: 5 }, { field: `numeric|size:${size}` });
2341
+ assert(await validator.passes());
2342
+ });
2343
+
2344
+ it(`Fails when the number is lesser than ${size}`, async () => {
2345
+ const validator = new Validator({ field: 4 }, { field: `numeric|size:${size}` });
2346
+ assert(await validator.fails());
2347
+ });
2348
+
2349
+ it(`Fails when the number is greater than ${size}`, async () => {
2350
+ const validator = new Validator({ field: 9 }, { field: `numeric|size:${size}` });
2351
+ assert(await validator.fails());
2352
+ });
2353
+
2354
+ it(`Passes when the string's length is equal to ${size}`, async () => {
2355
+ const validator = new Validator({ field: 'abcde' }, { field: `string|size:${size}` });
2356
+ assert(await validator.passes());
2357
+ });
2358
+
2359
+ it(`Fails when the string's length is lesser than ${size}`, async () => {
2360
+ const validator = new Validator({ field: 'abcd' }, { field: `string|size:${size}` });
2361
+ assert(await validator.fails());
2362
+ });
2363
+
2364
+ it(`Fails when the string's length is greater than ${size}`, async () => {
2365
+ const validator = new Validator({ field: 'abcdefg' }, { field: `string|size:${size}` });
2366
+ assert(await validator.fails());
2367
+ });
2368
+ });
2369
+
2370
+ describe(`Rule 'starts_with'`, () => {
2371
+ const rules = { field: 'starts_with:foo,bar' };
2372
+
2373
+ it(`Passes when the field ends with the first values`, async () => {
2374
+ const validator = new Validator({ field: 'fooyes' }, rules);
2375
+ assert(await validator.passes());
2376
+ });
2377
+
2378
+ it(`Passes when the field ends with the second values`, async () => {
2379
+ const validator = new Validator({ field: 'baryes' }, rules);
2380
+ assert(await validator.passes());
2381
+ });
2382
+
2383
+ it(`Fails when the field does not end with any value`, async () => {
2384
+ const validator = new Validator({ field: 'yes' }, rules);
2385
+ assert(await validator.fails());
2386
+ });
2387
+ });
2388
+
2389
+ describe(`Rule 'string'`, () => {
2390
+ const rules = { field: 'string' };
2391
+
2392
+ it(`Passes when the field is a string`, async () => {
2393
+ const validator = new Validator({ field: 'abc' }, rules);
2394
+ assert(await validator.passes());
2395
+ });
2396
+
2397
+ it(`Fails when the field is not a string`, async () => {
2398
+ const validator = new Validator({ field: null }, rules);
2399
+ assert(await validator.fails());
2400
+
2401
+ validator.setData({ field: true });
2402
+ assert(await validator.fails());
2403
+
2404
+ validator.setData({ field: 123 });
2405
+ assert(await validator.fails());
2406
+
2407
+ validator.setData({ field: [1, 2, 3] });
2408
+ assert(await validator.fails());
2409
+ });
2410
+ });
2411
+
2412
+ describe(`Rule 'timezone'`, () => {
2413
+ const rules = { field: 'timezone' };
2414
+
2415
+ it(`Passes when the field is a valid timezone`, async () => {
2416
+ const validator = new Validator({ field: 'UTC' }, rules);
2417
+ assert(await validator.passes());
2418
+
2419
+ validator.setData({ field: 'Asia/Kuala_Lumpur' });
2420
+ assert(await validator.passes());
2421
+ });
2422
+
2423
+ it(`Fails when the field is an invalid timezone`, async () => {
2424
+ const validator = new Validator({ field: 'Asia/Kelantan' }, rules);
2425
+ assert(await validator.fails());
2426
+
2427
+ validator.setData({ field: 123 });
2428
+ assert(await validator.fails());
2429
+
2430
+ validator.setData({ field: 'abc' });
2431
+ assert(await validator.fails());
2432
+ });
2433
+ });
2434
+
2435
+ describe(`Rule 'uppercase'`, () => {
2436
+ const rules = { field: 'uppercase' };
2437
+
2438
+ it(`Passes when the field is fully uppercased`, async () => {
2439
+ const validator = new Validator({ field: 'ABC' }, rules);
2440
+ assert(await validator.passes());
2441
+ });
2442
+
2443
+ it(`Fails when the field is partially uppercased`, async () => {
2444
+ const validator = new Validator({ field: 'abC' }, rules);
2445
+ assert(await validator.fails());
2446
+ });
2447
+
2448
+ it(`Fails when the field is fully lowercased`, async () => {
2449
+ const validator = new Validator({ field: 'abc' }, rules);
2450
+ assert(await validator.fails());
2451
+ });
2452
+ });
2453
+
2454
+ describe(`Rule 'url'`, () => {
2455
+ const rules = { field: 'url' };
2456
+
2457
+ it(`Passes when the field is a valid URL`, async () => {
2458
+ const validator = new Validator({ field: 'https://onpay.my' }, rules);
2459
+ assert(await validator.passes());
2460
+ });
2461
+
2462
+ it(`Fails when the field is an invalid URL`, async () => {
2463
+ const validator = new Validator({ field: '//onpay.my' }, rules);
2464
+ assert(await validator.fails());
2465
+ });
2466
+ });
2467
+
2468
+ describe(`Rule 'ulid'`, () => {
2469
+ const rules = { field: 'ulid' };
2470
+
2471
+ it(`Passes when the field is a valid ULID`, async () => {
2472
+ const validator = new Validator({ field: '01GZPCVRPR6K3KQW5B9ESB8PH3' }, rules);
2473
+ assert(await validator.passes());
2474
+ });
2475
+
2476
+ it(`Fails when the field is an invalid ULID`, async () => {
2477
+ const validator = new Validator({ field: '01GZPCVRPR6K3KOW5B9ESB8PH3' }, rules);
2478
+ assert(await validator.fails());
2479
+ });
2480
+ });
2481
+
2482
+ describe(`Rule 'uuid'`, () => {
2483
+ const rules = { field: 'uuid' };
2484
+
2485
+ it(`Passes when the field is a valid UUID`, async () => {
2486
+ const validator = new Validator({ field: '395dbfe1-3451-43f0-b295-337c00074099' }, rules);
2487
+ assert(await validator.passes());
2488
+ });
2489
+
2490
+ it(`Fails when the field is an invalid UUID`, async () => {
2491
+ const validator = new Validator({ field: '395dbfe1-3451-x3f0-b295-337c00074099' }, rules);
2492
+ assert(await validator.fails());
2493
+ });
2494
+ });
2495
+ });