Improve 'in' error reporting
This commit is contained in:
parent
b1b3fdcce8
commit
09494879ea
17
lib/rules.js
17
lib/rules.js
|
@ -5,7 +5,7 @@ var Rules = function(param, rules) {
|
|||
this.rules = rules;
|
||||
};
|
||||
|
||||
Rules.prototype.Error = function(message, rule, value) {
|
||||
Rules.prototype.Error = function(message, rule, value, args) {
|
||||
switch (typeof(this.rules.error)) {
|
||||
case 'string':
|
||||
message = this.rules.error;
|
||||
|
@ -26,14 +26,17 @@ Rules.prototype.Error = function(message, rule, value) {
|
|||
|
||||
if (_.isString(rule)) message = message.replace(/%r/, rule);
|
||||
|
||||
return message.replace(/%p/, this.param).replace(/%v/, "value").replace(/%r/, "rule");
|
||||
if (_.isArray(args)) message = message.replace(/%args/, args.join(', '));
|
||||
if (_.isString(args)) message = message.replace(/%args/, args);
|
||||
|
||||
return message.replace(/%p/, this.param).replace(/%v/, "value").replace(/%r/, "rule").replace(/%args/, "args");
|
||||
};
|
||||
|
||||
Rules.prototype.apply = function(value) {
|
||||
if (isValueEmpty(value) && !_.isUndefined(this.rules['default'])) {
|
||||
if ('function' === typeof(this.rules['default'])) {
|
||||
value = this.rules['default'].call();
|
||||
} else {
|
||||
} else {
|
||||
value = this.rules['default'];
|
||||
}
|
||||
}
|
||||
|
@ -117,9 +120,13 @@ Rules.prototype.check = function(value) {
|
|||
if (typeof(checks[property]) === "function") {
|
||||
var args = this.rules.properties[property];
|
||||
|
||||
if (!checks[property].apply(null, [value, args])) throw this.Error("%p failed %r with %v", property);
|
||||
if (property === "in") {
|
||||
if (!checks[property].apply(null, [value, args])) throw this.Error("%p failed %r with %v not one of [%args]", property, value, args);
|
||||
} else {
|
||||
if (!checks[property].apply(null, [value, args])) throw this.Error("%p failed %r with %v", property, value);
|
||||
}
|
||||
} else if (typeof(this.rules.properties[property]) === "function") {
|
||||
if (!this.rules.properties[property].apply(null, [value])) throw this.Error("%p failed on %r with %v", property);
|
||||
if (!this.rules.properties[property].apply(null, [value])) throw this.Error("%p failed on %r with %v", property, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue