From d13a4a8235c531c95bede4f61b4c2ed29a811e05 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Thu, 9 Jul 2020 20:57:50 +0200 Subject: [PATCH] Let users return Error objects from validators --- src/compose-rules.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/compose-rules.js b/src/compose-rules.js index 0ae37f9..486dd61 100644 --- a/src/compose-rules.js +++ b/src/compose-rules.js @@ -40,9 +40,11 @@ module.exports.compose = function composeValidators(validators) { } else { throw new Error(`The 'errors' in a validationResult must be an array`); } - } else if (result instanceof Error) { - // We could interpret returned Errors as either values or a throw substitute. Let's wait for users to file issues, so that we know what people *actually* need here. - throw new Error(`It is currently not allowed to return an Error object from a validator. If you have a reason to need this, please file a bug!`); + } else if (result instanceof Error && matchValidationError(result)) { + return { + errors: [ result ], + newValue: undefined + }; } else { return { errors: [],