-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathrange.js
58 lines (52 loc) · 1.54 KB
/
range.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
module.exports = function() {
/**
* Rule for validating minimum and maximum allowed values.
*/
this.range = function range() {
var rule = this.rule
, value = this.value
, val = value
, key = null
, arr = Array.isArray(value)
, len = typeof rule.len === 'number'
, min = typeof rule.min === 'number'
, max = typeof rule.max === 'number'
, num = typeof(value) === 'number'
, str = typeof(value) === 'string'
, fun = typeof(value) === 'function';
if(num || str || fun) {
key = typeof(value);
}else if(arr) {
key = 'array';
}
// if the value is not a supported range type ignore validation
if(!key) {
return false;
}
// use `length` property of `value`
if(str || arr || fun) {
val = value.length;
}
// length equality test
if(len && (val !== rule.len)) {
this.raise(
this.reasons.length,
this.messages[key].len, this.field, rule.len);
// minimum value
}else if(min && !max && val < rule.min ) {
this.raise(
this.reasons.min,
this.messages[key].min, this.field, rule.min);
// maximum value
}else if( max && !min && val > rule.max ) {
this.raise(
this.reasons.max,
this.messages[key].max, this.field, rule.max);
// range test
}else if(min && max && (val < rule.min || val > rule.max) ) {
this.raise(
val < rule.min ? this.reasons.min : this.reasons.max,
this.messages[key].range, this.field, rule.min, rule.max);
}
}
}