@vitruvius-labs/ts-predicate
Version:
TypeScript predicates library
12 lines (11 loc) • 500 B
JavaScript
import { ValidationError } from "./_index.mjs";
function assertEnumValue(value, enum_values, enum_name) {
// @ts-expect-error: [].includes() should not care about the type of the parameter.
if (!enum_values.includes(value)) {
if (enum_name !== undefined) {
throw new ValidationError(`The value must be a ${enum_name}.`);
}
throw new ValidationError(`The value must be one of the following: ${enum_values.join(", ")}.`);
}
}
export { assertEnumValue };