1 | var Valida = require('..');
|
2 |
|
3 |
|
4 | var 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 |
|
15 | var person = {
|
16 | age: '10'
|
17 | };
|
18 |
|
19 |
|
20 | var 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 |
|
27 | var fail = function (err) {
|
28 | console.log('failed with err', err);
|
29 | };
|
30 |
|
31 |
|
32 | Valida.process(person, schema, ['create', 'update'])
|
33 | .then(done, fail);
|
34 |
|
35 | Valida.process(person, schema, ['update'])
|
36 | .then(done, fail);
|
37 |
|
38 | Valida.process(person, schema, ['create'])
|
39 | .then(done, fail);
|
40 |
|
41 | Valida.process(person, schema)
|
42 | .then(done, fail);
|