All files / lib validate-rule.js

100% Statements 16/16
100% Branches 14/14
100% Functions 1/1
100% Lines 16/16

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 28 29 30 31 32 33 341x 1x   1x           8x 8x 1x 7x 1x   8x 1x 7x       1x   8x       1x   8x     1x  
const isString = require('lodash.isstring');
const isFunction = require('lodash.isfunction');
 
const validRuleTypes = [
  'equal', 'list', 'percent', 'pathEqual',
  'includes', 'includesListAll', 'includesListAny',
];
 
function validateRule(rule) {
  const errors = [];
  if (!Object.prototype.hasOwnProperty.call(rule, 'type')) {
    errors.push(new Error('rule must have a type property'));
  } else if (!validRuleTypes.includes(rule.type)) {
    errors.push(new Error('rule.type must be one of the following: equal, list, percent'));
  }
  if (!Object.prototype.hasOwnProperty.call(rule, 'input')) {
    errors.push(new Error('rule must have an input property'));
  } else if (
    !isFunction(rule.input)
    && !isString(rule.input)
  ) {
    errors.push(new Error('rule.input must be a function or a string'));
  }
  if (
    rule.type !== 'percent'
    && !Object.prototype.hasOwnProperty.call(rule, 'property')
  ) {
    errors.push(new Error('rule.property must exist for all types except "percent"'));
  }
  return errors;
}
 
module.exports = validateRule;