1 | var Valida = require('..');
|
2 |
|
3 |
|
4 | var schema = {
|
5 | code: [
|
6 | { validator: Valida.Validator.range, min: 2, max: 10, groups: ['both'] },
|
7 | { validator: Valida.Validator.range, min: 2, groups: ['min'] },
|
8 | { validator: Valida.Validator.range, max: 10, groups: ['max'] },
|
9 | ]
|
10 | };
|
11 |
|
12 |
|
13 | var data = { };
|
14 |
|
15 | data.code = 5;
|
16 | Valida.process(data, schema, function(err, ctx) {
|
17 | if (err) return console.log(err);
|
18 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
19 | console.log('valid');
|
20 | }, 'both');
|
21 |
|
22 |
|
23 | data.code = 1;
|
24 | Valida.process(data, schema, function(err, ctx) {
|
25 | if (err) return console.log(err);
|
26 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
27 | console.log('valid');
|
28 | }, 'both');
|
29 |
|
30 |
|
31 | data.code = 10;
|
32 | Valida.process(data, schema, function(err, ctx) {
|
33 | if (err) return console.log(err);
|
34 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
35 | console.log('valid');
|
36 | }, 'both');
|
37 |
|
38 |
|
39 | data.code = 20;
|
40 | Valida.process(data, schema, function(err, ctx) {
|
41 | if (err) return console.log(err);
|
42 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
43 | console.log('valid');
|
44 | }, 'both');
|
45 |
|
46 |
|
47 | data.code = 0;
|
48 | Valida.process(data, schema, function(err, ctx) {
|
49 | if (err) return console.log(err);
|
50 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
51 | console.log('valid');
|
52 | }, 'min');
|
53 |
|
54 |
|
55 | data.code = 2;
|
56 | Valida.process(data, schema, function(err, ctx) {
|
57 | if (err) return console.log(err);
|
58 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
59 | console.log('valid');
|
60 | }, 'min');
|
61 |
|
62 | data.code = 200;
|
63 | Valida.process(data, schema, function(err, ctx) {
|
64 | if (err) return console.log(err);
|
65 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
66 | console.log('valid');
|
67 | }, 'min');
|
68 |
|
69 | data.code = 20;
|
70 | Valida.process(data, schema, function(err, ctx) {
|
71 | if (err) return console.log(err);
|
72 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
73 | console.log('valid');
|
74 | }, 'max');
|
75 |
|
76 |
|
77 | data.code = 10;
|
78 | Valida.process(data, schema, function(err, ctx) {
|
79 | if (err) return console.log(err);
|
80 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
81 | console.log('valid');
|
82 | }, 'max');
|
83 |
|
84 | data.code = -3;
|
85 | Valida.process(data, schema, function(err, ctx) {
|
86 | if (err) return console.log(err);
|
87 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
88 | console.log('valid');
|
89 | }, 'max');
|