UNPKG

777 BJavaScriptView Raw
1var Valida = require('..');
2
3
4Valida.setValidator('timeout1', function(ctx, options, value, cb) {
5 setTimeout(function() {
6 console.log('timeout1', new Date());
7 cb();
8 }, 2000);
9});
10
11
12Valida.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
20var schema = {
21 id: [
22 { validator: 'timeout1' },
23 { sanitizer: Valida.Sanitizer.toInt },
24 { validator: Valida.Validator.required },
25 { validator: 'timeout2' }
26 ]
27};
28
29
30var obj = {
31 id: '10'
32};
33
34
35Valida.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});