Immediately fail completely when a non-validation error is encountered

master
Sven Slootweg 4 years ago
parent 9edb361baf
commit 34a8b4f6bd

@ -7,6 +7,7 @@ const flatten = require("flatten");
const ValidationError = require("@validatem/error");
const combinator = require("@validatem/combinator");
const validationResult = require("@validatem/validation-result");
const matchValidationError = require("@validatem/match-validation-error");
const areErrorsFromSameOrigin = require("./src/are-errors-from-same-origin");
@ -62,6 +63,12 @@ module.exports = function (alternatives) {
for (let alternative of alternatives) {
let { errors, newValue } = applyValidators(value, alternative);
let unexpectedErrors = errors.filter((error) => !matchValidationError(error));
if (unexpectedErrors.length > 0) {
// We want to immediately stop trying alternatives when a non-ValidationError occurred, since that means that something broke internally somewhere, and it is not safe to continue executing.
throw unexpectedErrors[0];
} else {
if (errors.length === 0) {
return newValue;
} else if (errors.length === 1) {
@ -70,6 +77,7 @@ module.exports = function (alternatives) {
allErrors.push(errorAllOf(errors));
}
}
}
/*
We want to separate out the errors that occurred at this "level"; that is, errors that *didn't* originate from a nested validation combinator like `has-shape` or `array-of`.

@ -19,6 +19,7 @@
"dependencies": {
"@validatem/combinator": "^0.1.1",
"@validatem/error": "^1.0.0",
"@validatem/match-validation-error": "^0.1.0",
"@validatem/validation-result": "^0.1.2",
"flatten": "^1.0.3"
}

Loading…
Cancel
Save