min-length-validator.js 822 B

12345678910111213141516171819202122232425262728
  1. import {InvalidArgumentError} from '../../../../../errors/index.js';
  2. /**
  3. * Min length validator.
  4. *
  5. * @param {*} value
  6. * @param {number|boolean} options
  7. * @param {object} context
  8. * @returns {boolean}
  9. */
  10. export function minLengthValidator(value, options, context) {
  11. if (value == null || options === false) return true;
  12. if (typeof options !== 'number')
  13. throw new InvalidArgumentError(
  14. 'The validator %v requires the "options" argument ' +
  15. 'as a Number, but %v was given.',
  16. context.validatorName,
  17. options,
  18. );
  19. if (typeof value === 'string' || Array.isArray(value))
  20. return value.length >= options;
  21. throw new InvalidArgumentError(
  22. 'The property validator %v requires a String ' +
  23. 'or an Array value, but %v was given.',
  24. context.validatorName,
  25. value,
  26. );
  27. }