diff --git a/lib/revalidator.js b/lib/revalidator.js index 58476b0..4f7bd1f 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -243,8 +243,12 @@ type; function constrain(name, value, assert) { - if (schema[name] !== undefined && !assert(value, schema[name])) { - error(name, property, value, schema, errors); + if (schema[name] === undefined) + return; + var result = assert(value, schema[name]); + var message = typeof result === "string" ? result : null; + if (message !== null || !result) { + error(name, property, value, schema, errors, message); } } @@ -420,9 +424,9 @@ callback(true); } - function error(attribute, property, actual, schema, errors) { + function error(attribute, property, actual, schema, errors, message) { var lookup = { expected: schema[attribute], actual: actual, attribute: attribute, property: property }; - var message = schema.messages && schema.messages[attribute] || schema.message || validate.messages[attribute] || "no default message"; + var message = message || schema.messages && schema.messages[attribute] || schema.message || validate.messages[attribute] || "no default message"; message = message.replace(/%\{([a-z]+)\}/ig, function (_, match) { return lookup[match.toLowerCase()] || ''; }); errors.push({ attribute: attribute,