1 | var Valida = require('..');
|
2 |
|
3 |
|
4 | var schema = {
|
5 | code: [
|
6 | { validator: Valida.Validator.len, min: 2, max: 10, groups: ['both'] },
|
7 | { validator: Valida.Validator.len, min: 2, groups: ['min'] },
|
8 | { validator: Valida.Validator.len, max: 10, groups: ['max'] },
|
9 | { validator: Valida.Validator.len, min: 2, max: 3, groups: ['both array']}
|
10 | ]
|
11 | };
|
12 |
|
13 |
|
14 | var data = { code: '123456789' };
|
15 |
|
16 |
|
17 | Valida.process(data, schema, function(err, ctx) {
|
18 | if (err) return console.log(err);
|
19 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
20 | console.log('valid');
|
21 | }, 'both');
|
22 |
|
23 |
|
24 | data.code = '123';
|
25 | Valida.process(data, schema, function(err, ctx) {
|
26 | if (err) return console.log(err);
|
27 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
28 | console.log('valid');
|
29 | }, 'both');
|
30 |
|
31 |
|
32 | data.code = '12';
|
33 | Valida.process(data, schema, function(err, ctx) {
|
34 | if (err) return console.log(err);
|
35 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
36 | console.log('valid');
|
37 | }, 'both');
|
38 |
|
39 |
|
40 | data.code = '1234567890';
|
41 | Valida.process(data, schema, function(err, ctx) {
|
42 | if (err) return console.log(err);
|
43 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
44 | console.log('valid');
|
45 | }, 'both');
|
46 |
|
47 |
|
48 | data.code = '1';
|
49 | Valida.process(data, schema, function(err, ctx) {
|
50 | if (err) return console.log(err);
|
51 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
52 | console.log('valid');
|
53 | }, 'both');
|
54 |
|
55 |
|
56 | data.code = '12345678901';
|
57 | Valida.process(data, schema, function(err, ctx) {
|
58 | if (err) return console.log(err);
|
59 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
60 | console.log('valid');
|
61 | }, 'both');
|
62 |
|
63 |
|
64 | data.code = '12';
|
65 | Valida.process(data, schema, function(err, ctx) {
|
66 | if (err) return console.log(err);
|
67 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
68 | console.log('valid');
|
69 | }, 'min');
|
70 |
|
71 |
|
72 | data.code = '1';
|
73 | Valida.process(data, schema, function(err, ctx) {
|
74 | if (err) return console.log(err);
|
75 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
76 | console.log('valid');
|
77 | }, 'min');
|
78 |
|
79 |
|
80 | data.code = '1234567890';
|
81 | Valida.process(data, schema, function(err, ctx) {
|
82 | if (err) return console.log(err);
|
83 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
84 | console.log('valid');
|
85 | }, 'max');
|
86 |
|
87 |
|
88 | data.code = '12345678901';
|
89 | Valida.process(data, schema, function(err, ctx) {
|
90 | if (err) return console.log(err);
|
91 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
92 | console.log('valid');
|
93 | }, 'max');
|
94 |
|
95 |
|
96 | data.code = ['hello', 'world'];
|
97 | Valida.process(data, schema, function(err, ctx) {
|
98 | if (err) return console.log(err);
|
99 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
100 | console.log('valid');
|
101 | }, 'both array');
|
102 |
|
103 |
|
104 | data.code = ['hello', 'world', 'foo', 'bar'];
|
105 | Valida.process(data, schema, function(err, ctx) {
|
106 | if (err) return console.log(err);
|
107 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
108 | console.log('valid');
|
109 | }, 'both array');
|
110 |
|
111 |
|
112 | data.code = ['hello'];
|
113 | Valida.process(data, schema, function(err, ctx) {
|
114 | if (err) return console.log(err);
|
115 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
116 | console.log('valid');
|
117 | }, 'both array');
|
118 |
|
119 |
|
120 | data.code = [];
|
121 | Valida.process(data, schema, function(err, ctx) {
|
122 | if (err) return console.log(err);
|
123 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
124 | console.log('valid');
|
125 | }, 'both array');
|