validateObjectWithProps.js 592 B

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