From d12cb6f91756ced7806bd19ba79403aeef4fb39e Mon Sep 17 00:00:00 2001 From: MK Date: Mon, 7 Dec 2020 19:22:30 -0500 Subject: [PATCH] Allow custom validator to return an error string --- lib/helper.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index 934a1f0..c6552ca 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -20,8 +20,12 @@ module.exports = function(options, key, validations, param) { else if (_.isFunction(validations)) { try { const test = validations(param); - if (!_.isBoolean(test)) { - return { error: 'The validation response must be boolean.' }; + // If the custom test returns a string, use that as the error message + if (_.isString(test)) { + return { error: test }; + } + else if (!_.isBoolean(test)) { + return { error: 'The validation response must be boolean or an error message.' }; } return test ? { value: param } : { error: 'The validation failed' }; } catch (e) {