4
0

validateObjectWithArrayProps.mjs 741 B

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