Compare commits

...

3 Commits

@ -3,6 +3,7 @@
// TODO: Rename this to something more appropriate, like any/anyOf? How to make sure that doesn't cause confusion with `oneOf`?
const splitFilter = require("split-filter");
const flatten = require("flatten");
const ValidationError = require("@validatem/error");
const combinator = require("@validatem/combinator");
@ -10,6 +11,18 @@ const validationResult = require("@validatem/validation-result");
const areErrorsFromSameOrigin = require("./src/are-errors-from-same-origin");
function unpackNestedEithers(errors) {
// NOTE: We only unpack `either` errors that occurred *at the same level* as this error, ie. where there's directly a case of `either(either(...), ...)`, without any kind of data nesting (like `arrayOf` or `hasShape`) inbetween. Nested-data failures should still be shown separately, as their resolution strategy is actually different; unlike same-level nested `either` errors, where the nesting is purely an implementation detail that allows composing sets of alternatives together.
return flatten(errors.map((error) => {
if (error.path.length === 0 && error.__isValidatemEitherError) {
return error.subErrors;
} else {
return error;
}
}));
}
module.exports = function (alternatives) {
if (!Array.isArray(alternatives)) {
throw new Error(`Must specify an array of alternatives`);
@ -27,8 +40,12 @@ module.exports = function (alternatives) {
if (errors.length === 0) {
return newValue;
} else if (errors.length === 1) {
allErrors.push(errors[0]);
} else {
allErrors.push(...errors);
allErrors.push(new ValidationError("Must satisfy all of the following:", {
subErrors: errors
}));
}
}
@ -44,8 +61,8 @@ module.exports = function (alternatives) {
- At options -> credentials -> 1: Encountered an unexpected property 'foo'"
*/
let [ sameLevelErrors, nestedErrors ] = splitFilter(allErrors, (error) => {
return (error.path.length === 0);
let nestedErrors = allErrors.filter((error) => {
return (error.path.length !== 0);
});
let sameOrigin = areErrorsFromSameOrigin(nestedErrors);
@ -54,7 +71,10 @@ module.exports = function (alternatives) {
// One of the alternatives *did* match, but it failed somewhere further down the tree, and we don't have any errors originating from *other* nested rules. Chances are that the user intended to match the failing branch, so we pretend that the other alternatives don't exist, and pass through the original error(s).
return validationResult({ errors: nestedErrors });
} else {
throw new ValidationError(`Must satisfy at least one of:`, { subErrors: allErrors });
throw new ValidationError(`Must satisfy at least one of:`, {
subErrors: unpackNestedEithers(allErrors),
__isValidatemEitherError: true
});
}
});
}

@ -6,7 +6,7 @@
"validator",
"combinator"
],
"version": "0.1.4",
"version": "0.1.5",
"main": "index.js",
"repository": "http://git.cryto.net/validatem/either.git",
"author": "Sven Slootweg <admin@cryto.net>",
@ -20,6 +20,6 @@
"@validatem/combinator": "^0.1.1",
"@validatem/error": "^1.0.0",
"@validatem/validation-result": "^0.1.2",
"split-filter": "^1.1.3"
"flatten": "^1.0.3"
}
}

@ -208,11 +208,6 @@ is-string@^1.0.5:
resolved "https://registry.yarnpkg.com/is-string/-/is-string-1.0.5.tgz#40493ed198ef3ff477b8c7f92f644ec82a5cd3a6"
integrity sha512-buY6VNRjhQMiF1qWDouloZlQbRhDPCebwxSjxMjxgemYT46YMd2NR0/H+fBhEfWX4A/w9TBJ+ol+okqJKFE6vQ==
split-filter@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/split-filter/-/split-filter-1.1.3.tgz#c68cc598783d88f60d16e7b452dacfe95ba60539"
integrity sha512-2xXwhWeJUFrYE8CL+qoy9mCohu5/E+uglvpqL1FVXz1XbvTwivafVC6oTDeg/9ksOAxg6DvyCF44Dvf5crFU0w==
supports-color@^7.1.0:
version "7.1.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1"

Loading…
Cancel
Save