UNPKG

1.05 kBJavaScriptView 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
20Valida.process(person, schema, function(err, ctx) {
21 if (err) return console.log(err);
22 if (!ctx.isValid()) return console.log(ctx.errors());
23 console.log('valid', person);
24}, ['create', 'update']);
25
26
27Valida.process(person, schema, function(err, ctx) {
28 if (err) return console.log(err);
29 if (!ctx.isValid()) return console.log(ctx.errors());
30 console.log('valid', person);
31}, 'update');
32
33
34Valida.process(person, schema, function(err, ctx) {
35 if (err) return console.log(err);
36 if (!ctx.isValid()) return console.log(ctx.errors());
37 console.log('valid', person);
38}, 'create');
39
40
41Valida.process(person, schema, function(err, ctx) {
42 if (err) return console.log(err);
43 if (!ctx.isValid()) return console.log(ctx.errors());
44 console.log('valid', person);
45});