1 | var Valida = require('..');
|
2 |
|
3 |
|
4 | var schema = {
|
5 | code: [
|
6 | { validator: Valida.Validator.regex, pattern: /^[0-9]*$/, groups: ['not matching pattern'] },
|
7 | { validator: Valida.Validator.regex, pattern: /^a[0-9]*z$/, groups: ['matching pattern'] },
|
8 | { validator: Valida.Validator.regex, pattern: '^a[0-9]*z$', groups: ['matching string without modifiers'] },
|
9 | { validator: Valida.Validator.regex, pattern: '[A-Z]', modifiers: 'i', groups: ['string with modifiers'] }
|
10 | ]
|
11 | };
|
12 |
|
13 |
|
14 | var data = { code: 'a1234z' };
|
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 | }, 'not matching pattern');
|
22 |
|
23 |
|
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 | }, 'matching pattern');
|
29 |
|
30 |
|
31 | Valida.process(data, schema, function(err, ctx) {
|
32 | if (err) return console.log(err);
|
33 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
34 | console.log('valid');
|
35 | }, 'matching string without modifiers');
|
36 |
|
37 |
|
38 | Valida.process(data, schema, function(err, ctx) {
|
39 | if (err) return console.log(err);
|
40 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
41 | console.log('valid');
|
42 | }, 'string with modifiers');
|
43 |
|
44 |
|
45 | data.code = '123';
|
46 | Valida.process(data, schema, function(err, ctx) {
|
47 | if (err) return console.log(err);
|
48 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
49 | console.log('valid');
|
50 | }, 'string with modifiers');
|
51 |
|
52 |
|
53 | data.code = null;
|
54 |
|
55 |
|
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 | }, 'matching pattern');
|
61 |
|
62 |
|
63 | delete data.code;
|
64 |
|
65 |
|
66 | Valida.process(data, schema, function(err, ctx) {
|
67 | if (err) return console.log(err);
|
68 | if (!ctx.isValid()) return console.log('invalid', ctx.errors());
|
69 | console.log('valid');
|
70 | }, 'matching pattern');
|