validateObjectWithArrayProps.js 760 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const { isPlainObject } = require('./validateTypes');
  3. /**
  4. * Check whether the variable is an object and all its properties are one or more values
  5. * that satisfy the specified validator(s):
  6. *
  7. * @example
  8. * ignoreProperties = {
  9. * value1: ["item11", "item12", "item13"],
  10. * value2: "item2",
  11. * };
  12. * validateObjectWithArrayProps(isString)(ignoreProperties);
  13. * //=> true
  14. *
  15. * @typedef {(value: unknown) => boolean} Validator
  16. * @param {...Validator} validators
  17. * @returns {Validator}
  18. */
  19. module.exports = function validateObjectWithArrayProps(...validators) {
  20. return (value) => {
  21. if (!isPlainObject(value)) {
  22. return false;
  23. }
  24. return Object.values(value)
  25. .flat()
  26. .every((item) => validators.some((v) => v(item)));
  27. };
  28. };