UNPKG

3.39 kBJavaScriptView Raw
1var Valida = require('..');
2
3
4var 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
14var data = { code: '123456789' };
15
16
17Valida.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
24data.code = '123';
25Valida.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
32data.code = '12';
33Valida.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
40data.code = '1234567890';
41Valida.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
48data.code = '1';
49Valida.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
56data.code = '12345678901';
57Valida.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
64data.code = '12';
65Valida.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
72data.code = '1';
73Valida.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
80data.code = '1234567890';
81Valida.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
88data.code = '12345678901';
89Valida.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
96data.code = ['hello', 'world'];
97Valida.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
104data.code = ['hello', 'world', 'foo', 'bar'];
105Valida.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
112data.code = ['hello'];
113Valida.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
120data.code = [];
121Valida.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');