1 | var Valida = require('..');
|
2 |
|
3 |
|
4 | Valida.setValidator('timeout1', function(ctx, options, value, cb) {
|
5 | setTimeout(function() {
|
6 | console.log('timeout1', new Date());
|
7 | cb();
|
8 | }, 2000);
|
9 | });
|
10 |
|
11 |
|
12 | Valida.setValidator('timeout2', function(ctx, options, value, cb) {
|
13 | setTimeout(function() {
|
14 | console.log('timeout2', new Date());
|
15 | cb(null, { key: 'timeout2', someAddInfo: 'hello world' });
|
16 | }, 5000);
|
17 | });
|
18 |
|
19 |
|
20 | var schema = {
|
21 | id: [
|
22 | { validator: 'timeout1' },
|
23 | { sanitizer: Valida.Sanitizer.toInt },
|
24 | { validator: Valida.Validator.required },
|
25 | { validator: 'timeout2' }
|
26 | ]
|
27 | };
|
28 |
|
29 |
|
30 | var obj = {
|
31 | id: '10'
|
32 | };
|
33 |
|
34 |
|
35 | Valida.process(obj, schema, function(err, ctx) {
|
36 | if (err) return console.log(err);
|
37 | if (!ctx.isValid()) console.log(ctx.errors());
|
38 | console.log(obj);
|
39 | });
|