UNPKG

1.82 kBJavaScriptView Raw
1const assert = require('assert');
2
3const { Validator } = require('../lib/index');
4
5
6describe('Rules as Array', () => {
7 it('should return true', async () => {
8 const v = new Validator(
9 { name: 'artisan' },
10 {
11 name: ['required', ['minLength', '5'], ['maxLength', '10'], 'alpha'],
12 },
13 );
14
15 const matched = await v.check();
16
17 // console.log(v.errors);
18
19 assert.equal(matched, true);
20 });
21
22 it('should return false due to minLength failed', async () => {
23 const v = new Validator(
24 { name: 'art' },
25 {
26 name: ['required', ['minLength', '5'], ['maxLength', '10'], 'alpha'],
27 },
28 );
29
30 const matched = await v.check();
31
32 assert.equal(matched, false);
33 });
34
35 it('should return false due to lengthBetween failed', async () => {
36 const v = new Validator(
37 { uid: 'abcdefghi' },
38 {
39 uid: ['required', ['lengthBetween', '5', '8'], 'alpha'],
40 },
41 );
42 const matched = await v.check();
43
44 assert.equal(matched, false);
45 });
46
47 it('regex delimiters fix', async () => {
48 const v = new Validator(
49 { uid: 'xyz' },
50 {
51 uid: ['required', ['regex', 'abc|xyz']],
52 },
53 );
54 const matched = await v.check();
55
56 assert.equal(matched, true);
57 });
58});
59
60
61describe('Rules as Mixed', () => {
62 it('should return true', async () => {
63 const v = new Validator(
64 {
65 name: 'artisan',
66 email: 'artisangang@gmail.com',
67 phone: '+918699987073',
68 ip: '127.0.0.1',
69 },
70 {
71 name: ['required', ['minLength', '5'], ['maxLength', '10'], 'alpha'],
72 email: 'required|email',
73 ip: ['ip'],
74 phone: 'required|phoneNumber',
75 },
76 );
77
78 const matched = await v.check();
79
80 // console.log(v.errors);
81
82 assert.equal(matched, true);
83 });
84});