All files / lib/validate-values-by-rules validate-values-by-rule.js

100% Statements 12/12
100% Branches 8/8
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 281x     64x   64x 14x     64x 64x 108x     64x   64x       1x     64x     1x  
const validateValueByRule = require('./validate-value-by-rule');
 
function validateValuesByRule(key, values, rule) {
  let { property } = rule;
 
  if (rule.type === 'percent') {
    property = property || 'percent';
  }
 
  const errors = [];
  const reducedValues = values.reduce(
    (memo, value) => validateValueByRule(memo, value, key, property, rule),
    { errors: [], percentTotal: 0 },
  );
  errors.push(...reducedValues.errors);
 
  if (
    reducedValues.percentTotal !== 0
    && reducedValues.percentTotal !== 100
  ) {
    errors.push(new Error(`percent properties "${property}" in values do not add up to 100 for key "${key}"`));
  }
 
  return errors;
}
 
module.exports = validateValuesByRule;