You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

18 lines
712 B
JavaScript

"use strict";
const ValidationError = require("@validatem/error");
// TODO: Generate custom error message for cases where minimum is -Infinity or maximum is Infinity?
// TODO: Note in README that this module DOES NOT enforce any particular type, due to `.length` being a de facto standard of sorts
module.exports = function createHasLengthBetweenValidator(minimum, maximum) {
// NOTE: minimum/maximum are both INCLUSIVE!
// TODO: Note that in README
return function hasLengthBetween(value) {
if (value.length == null || value.length < minimum || value.length > maximum) {
throw new ValidationError(`Must have a length between ${minimum} and ${maximum}; actual length was ${value.length}`);
}
}
};