UNPKG

1.74 kBJavaScriptView Raw
1const assert = require('assert');
2
3const { Validator, bailable } = require('../lib/index');
4
5
6describe('Non bailable', () => {
7 it('should return multiple errors', async () => {
8 // bailable(false);
9 const v = new Validator(
10 { name: '12' },
11 {
12 name: ['required', ['minLength', '5'], ['maxLength', '10'], 'alpha'],
13 },
14 );
15
16 v.bail(false);
17
18 const matched = await v.check();
19
20 assert.equal(matched, false);
21 assert.equal(Array.isArray(v.errors.name), true);
22 });
23
24 it('should toggle multiple errors on current instance', async () => {
25 // bailable(false);
26 const v = new Validator(
27 { name: '12' },
28 {
29 name: ['required', ['minLength', '5'], ['maxLength', '10'], 'alpha'],
30 },
31 );
32
33 v.bail(false);
34
35 const matched = await v.check();
36
37 assert.equal(matched, false);
38 assert.equal(Array.isArray(v.errors.name), true);
39
40 v.bail(true);
41
42 const matchedAgain = await v.check();
43
44 assert.equal(matchedAgain, false);
45 assert.equal(!Array.isArray(v.errors.name), true);
46 });
47
48 it('should toggle multiple errors', async () => {
49 // enable multiple errors
50 bailable(false);
51 const v = new Validator(
52 { name: 'art' },
53 {
54 name: ['required', ['minLength', '5'], ['maxLength', '10'], 'alpha'],
55 },
56 );
57
58 // multiple errors should be enabled
59 assert.equal(v.breakWhenFailed, false);
60
61 // disable mutliple errors
62 bailable(true);
63
64 // global disable should not effect pre-created instance
65 assert.equal(v.breakWhenFailed, false);
66
67 // disable current instance multiple errors
68 v.bail(true);
69
70 // multiple errors should be turned off
71 assert.equal(v.breakWhenFailed, true);
72 });
73});