1 | var rsvp = require('rsvp'),
|
2 | Valida = require('./valida');
|
3 |
|
4 |
|
5 | var validator = module.exports = {};
|
6 |
|
7 |
|
8 | validator.required = function(ctx, options, value) {
|
9 | if (typeof value == 'undefined' || value === null) {
|
10 | var err = { validator: 'required' };
|
11 | addMessage(err, options);
|
12 |
|
13 | return err;
|
14 | }
|
15 | };
|
16 |
|
17 |
|
18 | validator.empty = function(ctx, options, value) {
|
19 | if (typeof value !== 'undefined' && !value.length) {
|
20 | var err = { validator: 'empty' };
|
21 | addMessage(err, options);
|
22 |
|
23 | return err;
|
24 | }
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | validator.regex = function(ctx, options, value) {
|
32 | if (typeof value == 'undefined' || value === null) return;
|
33 |
|
34 | value = value + '';
|
35 |
|
36 | if (Object.prototype.toString.call(options.pattern).slice(8, -1) !== 'RegExp') {
|
37 | options.pattern = new RegExp(options.pattern, options.modifiers);
|
38 | }
|
39 |
|
40 | if (!options.pattern.test(value)) {
|
41 | var mod = '';
|
42 | if (options.pattern.global) mod += 'g';
|
43 | if (options.pattern.ignoreCase) mod += 'i';
|
44 | if (options.pattern.multiline) mod += 'm';
|
45 |
|
46 | var err = { validator: 'regex', pattern: options.pattern.source, modifiers: mod };
|
47 | addMessage(err, options);
|
48 |
|
49 | return err;
|
50 | }
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | validator.len = function(ctx, options, value) {
|
59 | if (typeof value == 'undefined' || value === null) return;
|
60 |
|
61 | if (!Array.isArray(value)) value = value + '';
|
62 |
|
63 | var valid = true;
|
64 | if (options.min !== undefined && options.min > value.length) valid = false;
|
65 | if (options.max !== undefined && options.max < value.length) valid = false;
|
66 |
|
67 | var err = { validator: 'len' };
|
68 |
|
69 | if (options.min) {
|
70 | err.min = options.min;
|
71 | }
|
72 | if (options.max) {
|
73 | err.max = options.max;
|
74 | }
|
75 |
|
76 | addMessage(err, options);
|
77 |
|
78 | if (!valid) return err;
|
79 | };
|
80 |
|
81 |
|
82 | validator.array = function(ctx, options, value) {
|
83 | if (typeof value == 'undefined' || value === null) return;
|
84 |
|
85 | if (!Array.isArray(value)) {
|
86 | var err = { validator: 'array' };
|
87 | addMessage(err, options);
|
88 |
|
89 | return err;
|
90 | }
|
91 | };
|
92 |
|
93 |
|
94 | validator.schema = function(ctx, options, value, cb) {
|
95 | if (typeof value == 'undefined' || value === null) return cb();
|
96 | if (typeof options.schema === 'undefined') return cb(new Error('validator requires schema option'));
|
97 |
|
98 | value = Array.isArray(value) ? value : [ value ];
|
99 |
|
100 | var errors = {};
|
101 |
|
102 | var verify = function () {
|
103 | if (Object.keys(errors).length) return cb(null, { validator: 'schema', errors: errors });
|
104 | cb();
|
105 | };
|
106 |
|
107 | var isValid = function (i, ctx) {
|
108 | if (ctx.isValid()) return;
|
109 |
|
110 | errors[i] = ctx.errors();
|
111 | };
|
112 |
|
113 | var parallel = value.map(function (permission, i) {
|
114 | return Valida.process(permission, options.schema)
|
115 | .then(isValid.bind(null, i));
|
116 | });
|
117 |
|
118 | return rsvp.all(parallel)
|
119 | .then(verify);
|
120 | };
|
121 |
|
122 |
|
123 | validator.plainObject = function(ctx, options, value) {
|
124 | if (typeof value == 'undefined' || value === null) return;
|
125 |
|
126 | if (typeof value !== 'object' || Array.isArray(value)) {
|
127 | var err = { validator: 'plainObject' };
|
128 | addMessage(err, options);
|
129 |
|
130 | return err;
|
131 | }
|
132 | };
|
133 |
|
134 |
|
135 | validator.date = function(ctx, options, value) {
|
136 | if (typeof value === 'undefined' || value === null) return;
|
137 |
|
138 | var date = Date.parse(value);
|
139 | if (isNaN(date)) {
|
140 | var err = {validator: 'date'};
|
141 | addMessage(err, options);
|
142 |
|
143 | return err;
|
144 | }
|
145 | }
|
146 |
|
147 | validator.integer = function(ctx, options, value) {
|
148 | if (typeof value === 'undefined' || value === null) return;
|
149 |
|
150 | if (!isInteger(value)) {
|
151 | var err = {validator: 'integer'};
|
152 | addMessage(err, options);
|
153 |
|
154 | return err;
|
155 | }
|
156 | }
|
157 |
|
158 | function addMessage (err, opt) {
|
159 | if (opt.msg) err.msg = opt.msg;
|
160 | }
|
161 |
|
162 | function isInteger(number) {
|
163 | return typeof number === "number" &&
|
164 | isFinite(number) &&
|
165 | Math.floor(number) === number;
|
166 | }
|