UNPKG

4.99 kBJavaScriptView Raw
1var rsvp = require('rsvp'),
2 Valida = require('./valida'),
3 isFloat = require('is-float');
4
5
6var validator = module.exports = {};
7
8
9validator.required = function(ctx, options, value) {
10 if (typeof value == 'undefined' || value === null) {
11 var err = { validator: 'required' };
12 addMessage(err, options);
13
14 return err;
15 }
16};
17
18
19validator.empty = function(ctx, options, value) {
20 if (typeof value !== 'undefined' && value !== null && !value.length) {
21 var err = { validator: 'empty' };
22 addMessage(err, options);
23
24 return err;
25 }
26};
27
28
29/*
30 * options = { pattern, modified }
31 */
32validator.regex = function(ctx, options, value) {
33 if (typeof value == 'undefined' || value === null) return;
34
35 value = value + '';
36
37 if (Object.prototype.toString.call(options.pattern).slice(8, -1) !== 'RegExp') {
38 options.pattern = new RegExp(options.pattern, options.modifiers);
39 }
40
41 if (!options.pattern.test(value)) {
42 var mod = '';
43 if (options.pattern.global) mod += 'g';
44 if (options.pattern.ignoreCase) mod += 'i';
45 if (options.pattern.multiline) mod += 'm';
46
47 var err = { validator: 'regex', pattern: options.pattern.source, modifiers: mod };
48 addMessage(err, options);
49
50 return err;
51 }
52};
53
54
55/*
56 * options = { min, max }
57 * value = array and non-array
58 */
59validator.len = function(ctx, options, value) {
60 if (typeof value == 'undefined' || value === null) return;
61
62 if (!Array.isArray(value)) value = value + '';
63
64 var valid = true;
65 if (options.min !== undefined && options.min > value.length) valid = false;
66 if (options.max !== undefined && options.max < value.length) valid = false;
67
68 var err = { validator: 'len' };
69
70 if (options.min) {
71 err.min = options.min;
72 }
73 if (options.max) {
74 err.max = options.max;
75 }
76
77 addMessage(err, options);
78
79 if (!valid) return err;
80};
81
82
83validator.array = function(ctx, options, value) {
84 if (typeof value == 'undefined' || value === null) return;
85
86 if (!Array.isArray(value)) {
87 var err = { validator: 'array' };
88 addMessage(err, options);
89
90 return err;
91 }
92};
93
94
95validator.schema = function(ctx, options, value, cb) {
96 if (typeof value == 'undefined' || value === null) return cb();
97 if (typeof options.schema === 'undefined') return cb(new Error('validator requires schema option'));
98
99 value = Array.isArray(value) ? value : [ value ];
100
101 var errors = {};
102
103 var verify = function () {
104 if (Object.keys(errors).length) return cb(null, { validator: 'schema', errors: errors });
105 cb();
106 };
107
108 var isValid = function (i, ctx) {
109 if (ctx.isValid()) return;
110
111 errors[i] = ctx.errors();
112 };
113
114 var parallel = value.map(function (permission, i) {
115 return Valida.process(permission, options.schema)
116 .then(isValid.bind(null, i));
117 });
118
119 return rsvp.all(parallel)
120 .then(verify);
121};
122
123
124validator.plainObject = function(ctx, options, value) {
125 if (typeof value == 'undefined' || value === null) return;
126
127 if (typeof value !== 'object' || Array.isArray(value)) {
128 var err = { validator: 'plainObject' };
129 addMessage(err, options);
130
131 return err;
132 }
133};
134
135
136validator.date = function(ctx, options, value) {
137 if (typeof value === 'undefined' || value === null) return;
138
139 var date = Date.parse(value);
140 if (isNaN(date)) {
141 var err = {validator: 'date'};
142 addMessage(err, options);
143
144 return err;
145 }
146}
147
148validator.integer = function(ctx, options, value) {
149 if (typeof value === 'undefined' || value === null) return;
150
151 if (!isInteger(value)) {
152 var err = {validator: 'integer'};
153 addMessage(err, options);
154
155 return err;
156 }
157}
158
159validator.enum = function(ctx, options, value) {
160 if (typeof value === 'undefined' || value === null) return;
161
162 if (options.items.indexOf(value) == -1) {
163 var err = {validator: 'enum'};
164 addMessage(err, options);
165
166 return err;
167 }
168}
169
170validator.bool = function(ctx, options, value) {
171 if (typeof value === 'undefined' || value === null) {
172 return;
173 }
174
175 if (typeof value !== 'boolean') {
176 return {
177 validator: 'bool',
178 msg: 'Invalid bool value.',
179 };
180 }
181};
182
183validator.float = function(ctx, options, value) {
184 if (typeof value === 'undefined' || value === null) {
185 return;
186 }
187
188 if (!isFloat(value)) {
189 return {
190 validator: 'float',
191 msg: 'Invalid float value.',
192 };
193 }
194};
195
196validator.range = function(ctx, options, value) {
197 if (typeof value == 'undefined' || value === null) return;
198
199 var valid = true;
200 if (options.min !== undefined && options.min > value) valid = false;
201 if (options.max !== undefined && options.max < value) valid = false;
202
203 var err = { validator: 'range' };
204
205 if (options.min) {
206 err.min = options.min;
207 }
208 if (options.max) {
209 err.max = options.max;
210 }
211
212 addMessage(err, options);
213
214 if (!valid) return err;
215};
216
217function addMessage (err, opt) {
218 if (opt.msg) err.msg = opt.msg;
219}
220
221function isInteger(number) {
222 return typeof number === "number" &&
223 isFinite(number) &&
224 Math.floor(number) === number;
225}