From ebca132a6166155d27e4d01cfc0ccea3147bd4d2 Mon Sep 17 00:00:00 2001 From: Maksim Date: Mon, 7 Aug 2017 16:14:15 +0300 Subject: [PATCH 1/2] Allow custom message from conform method --- lib/revalidator.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 58476b0..63e1be8 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -243,8 +243,10 @@ type; function constrain(name, value, assert) { - if (schema[name] !== undefined && !assert(value, schema[name])) { - error(name, property, value, schema, errors); + var result = assert(value, schema[name]); + var message = typeof result === "string" ? result : null; + if (schema[name] !== undefined && (message !== null || !result)) { + error(name, property, value, schema, errors, message); } } @@ -420,9 +422,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, From 5b44a057d5a00dc8a39f6f22764b5749355bb031 Mon Sep 17 00:00:00 2001 From: Maksim Date: Mon, 7 Aug 2017 17:28:16 +0300 Subject: [PATCH 2/2] Fix error --- lib/revalidator.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/revalidator.js b/lib/revalidator.js index 63e1be8..4f7bd1f 100644 --- a/lib/revalidator.js +++ b/lib/revalidator.js @@ -243,9 +243,11 @@ type; function constrain(name, value, assert) { + if (schema[name] === undefined) + return; var result = assert(value, schema[name]); var message = typeof result === "string" ? result : null; - if (schema[name] !== undefined && (message !== null || !result)) { + if (message !== null || !result) { error(name, property, value, schema, errors, message); } }