-
Notifications
You must be signed in to change notification settings - Fork 57
/
index.js
60 lines (43 loc) · 1.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Module dependencies
*/
var _ = require('@sailshq/lodash');
var match = require('./lib/match');
/**
* Public access
*/
module.exports = function (entity, ruleset) {
var errors = [];
// If ruleset doesn't contain any explicit rule keys,
// assume that this is a type
// Look for explicit rules
for (var rule in ruleset) {
// TODO: In next major version, remove this: It should definitely not be here.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Normalize the value if it looks like a boolean
if(ruleset[rule] === 'true') {
ruleset[rule] = true;
}
if(ruleset[rule] === 'false') {
ruleset[rule] = false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// If the value is false, then we shouldn't even run the validation
if(ruleset[rule] === false) {
break;
}
// If the rule value is a boolean we don't need to pass the value along.
// Otherwise we can pass it along so it's options are available in
// the validation.
var ruleVal = _.isBoolean(ruleset[rule]) ? undefined : ruleset[rule];
errors = errors.concat(match(entity, rule, ruleVal));
}
// If errors exist, return the list of them
if (errors.length) {
return errors;
}
// No errors, so return false
else {
return [];
}
};