@ -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,12 +63,19 @@ module.exports = function (alternatives) {
for ( let alternative of alternatives ) {
let { errors , newValue } = applyValidators ( value , alternative ) ;
if ( errors . length === 0 ) {
return newValue ;
} else if ( errors . length === 1 ) {
allErrors . push ( errors [ 0 ] ) ;
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 {
allErrors . push ( errorAllOf ( errors ) ) ;
if ( errors . length === 0 ) {
return newValue ;
} else if ( errors . length === 1 ) {
allErrors . push ( errors [ 0 ] ) ;
} else {
allErrors . push ( errorAllOf ( errors ) ) ;
}
}
}