UNPKG

805 BJavaScriptView Raw
1var Valida = require('..');
2
3
4var schema = {
5 age: [
6 { sanitizer: Valida.Sanitizer.toInt },
7 { validator: Valida.Validator.required, groups: ['create'] }
8 ],
9 name: [
10 { validator: Valida.Validator.required, groups: ['update'] }
11 ]
12};
13
14
15var person = {
16 age: '10'
17};
18
19
20var done = function (ctx) {
21 console.log('validation succeed, isValid =', ctx.isValid());
22 if (!ctx.isValid()) return console.log('errors', ctx.errors());
23 console.log('data', person);
24};
25
26
27var fail = function (err) {
28 console.log('failed with err', err);
29};
30
31
32Valida.process(person, schema, ['create', 'update'])
33 .then(done, fail);
34
35Valida.process(person, schema, ['update'])
36 .then(done, fail);
37
38Valida.process(person, schema, ['create'])
39 .then(done, fail);
40
41Valida.process(person, schema)
42 .then(done, fail);