@vitruvius-labs/ts-predicate
Version:
TypeScript predicates library
20 lines (19 loc) • 641 B
JavaScript
import { itemAssertion } from "./utils/item-assertion.mjs";
import { ValidationError } from "./utils/validation-error.mjs";
import { rethrowUnexpectedError } from "../utils/rethrow-unexpected-error.mjs";
function assertUnion(value, tests) {
const errors = [];
for (const test of tests) {
try {
itemAssertion(value, test);
}
catch (error) {
rethrowUnexpectedError(error);
errors.push(error);
}
}
if (errors.length === tests.length) {
throw new ValidationError("The value does not pass any of the assertion.", errors);
}
}
export { assertUnion };