UNPKG

1.13 kBJavaScriptView Raw
1const assert = require('assert');
2
3const niv = require('../lib/index');
4
5const { Validator } = niv;
6
7
8niv.extend('asyncIn', async ({ value, args }) => {
9 const results = await new Promise((resolve) => {
10 setTimeout(() => {
11 if (args.indexOf(value) >= 0) {
12 resolve(true);
13 return;
14 }
15
16 resolve(false);
17 }, 500);
18 });
19 return results;
20});
21
22
23describe('Async Rules', () => {
24 it('should pass using async', async () => {
25 const v = new Validator(
26 { status: 'active' }, { status: 'asyncIn:active,inactive' },
27 );
28
29 const matched = await v.check();
30
31 assert.equal(matched, true);
32 });
33
34 it('should pass using async and sync', async () => {
35 const v = new Validator(
36 { status: 'active' }, { status: 'string|asyncIn:active,inactive|ascii' },
37 );
38
39 const matched = await v.check();
40
41 assert.equal(matched, true);
42 });
43
44 it('should fails using async', async () => {
45 const v = new Validator(
46 { status: 'active' }, { status: 'asyncIn:activated,deactivated' },
47 );
48
49 const matched = await v.check();
50
51 assert.equal(matched, false);
52 });
53});