1 | const assert = require('assert');
|
2 |
|
3 | const { Validator } = require('../lib/index');
|
4 |
|
5 | describe('Edge Cases', () => {
|
6 | describe('undefined', () => {
|
7 | it('should ignore undefined and not required fields', async () => {
|
8 | const v = new Validator({ field: undefined }, { field: 'string' });
|
9 |
|
10 | const matched = await v.check();
|
11 | assert.equal(matched, true);
|
12 | });
|
13 |
|
14 |
|
15 | it('should reject undefined and required fields', async () => {
|
16 | const v = new Validator({ field: undefined }, { field: 'required|string' });
|
17 |
|
18 | const matched = await v.check();
|
19 | assert.equal(matched, false);
|
20 | });
|
21 | });
|
22 |
|
23 | describe('null', () => {
|
24 | it('should ignore null and not required fields', async () => {
|
25 | const v = new Validator({ field: null }, { field: 'string' });
|
26 |
|
27 | const matched = await v.check();
|
28 | assert.equal(matched, true);
|
29 | });
|
30 |
|
31 |
|
32 | it('should reject null and required fields', async () => {
|
33 | const v = new Validator({ field: null }, { field: 'required|string' });
|
34 |
|
35 | const matched = await v.check();
|
36 | assert.equal(matched, false);
|
37 | });
|
38 | });
|
39 |
|
40 | describe('empty string', () => {
|
41 | it('should ignore empty string in not required fields', async () => {
|
42 | const v = new Validator({ field: '' }, { field: 'string' });
|
43 |
|
44 | const matched = await v.check();
|
45 | assert.equal(matched, true);
|
46 | });
|
47 |
|
48 |
|
49 | it('should reject empty string in required fields', async () => {
|
50 | const v = new Validator({ field: '' }, { field: 'required|string' });
|
51 |
|
52 | const matched = await v.check();
|
53 | assert.equal(matched, false);
|
54 | });
|
55 | });
|
56 |
|
57 | describe('exceptions', () => {
|
58 | it('Checking for invalid rule', async () => {
|
59 | try {
|
60 | const v = new Validator({ name: 'Harcharan Singh' }, { name: 'required|fullName' });
|
61 |
|
62 | await v.check();
|
63 |
|
64 | throw new Error('Rule was missing.');
|
65 | } catch (e) {
|
66 | assert.equal(e, 'Error: Validation Rule: fullName does not exists.');
|
67 | }
|
68 | });
|
69 | });
|
70 | });
|