4
0

validateObjectWithProps.mjs 622 B

12345678910111213141516171819202122232425262728
  1. import { isPlainObject } from './validateTypes.mjs';
  2. /**
  3. * Check whether the variable is an object and all its properties agree with the provided validator.
  4. *
  5. * @example
  6. * config = {
  7. * value1: 1,
  8. * value2: 2,
  9. * value3: 3,
  10. * };
  11. * validateObjectWithProps(isNumber)(config);
  12. * //=> true
  13. *
  14. * @param {(value: unknown) => boolean} validator
  15. * @returns {(value: unknown) => boolean}
  16. */
  17. export default function validateObjectWithProps(validator) {
  18. return (value) => {
  19. if (!isPlainObject(value)) {
  20. return false;
  21. }
  22. return Object.values(value).every((item) => {
  23. return validator(item);
  24. });
  25. };
  26. }