UNPKG

@vitruvius-labs/ts-predicate

Version:
40 lines (39 loc) 1.56 kB
import { isArray } from "../type-guard/is-array.mjs"; import { buildArrayConstraints } from "../utils/build-array-constraints.mjs"; import { itemAssertion } from "./utils/item-assertion.mjs"; import { ValidationError } from "./utils/validation-error.mjs"; import { rethrowUnexpectedError } from "../utils/rethrow-unexpected-error.mjs"; function assertArray(value, constraints) { const CONSTRAINTS = buildArrayConstraints(constraints); if (!isArray(value)) { throw new ValidationError("The value is not an array."); } if (CONSTRAINTS === undefined) { return; } const ERRORS = []; if (CONSTRAINTS.minLength !== undefined && value.length < CONSTRAINTS.minLength) { if (CONSTRAINTS.minLength === 1) { ERRORS.push(new ValidationError("It must not be empty.")); } else { ERRORS.push(new ValidationError(`It must have at least ${CONSTRAINTS.minLength.toFixed(0)} items.`)); } } if (CONSTRAINTS.itemTest !== undefined) { const GUARD = CONSTRAINTS.itemTest; value.forEach((item, index) => { try { itemAssertion(item, GUARD); } catch (error) { rethrowUnexpectedError(error); ERRORS.push(new ValidationError(`The value at index ${index.toFixed(0)} is incorrect.`, [error])); } }); } if (ERRORS.length > 0) { throw new ValidationError("The value is an array, but its content is incorrect.", ERRORS); } } export { assertArray };