All files / lib/rules validate-rule.js

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

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 34 35 36 37 385x 5x   5x                     36x 1x 35x 1x         34x 1x 33x 1x     32x       1x       5x  
const isFunction = require('lodash.isfunction');
const isString = require('lodash.isstring');
 
const ruleTypes = [
  'equal',
  'pathEqual',
  'list',
  'percent',
  'includes',
  'includesListAny',
  'includesListAll',
];
 
function validateRule(rule) {
  if (!Object.prototype.hasOwnProperty.call(rule, 'type')) {
    throw new Error('rule must have a type property');
  } else if (!ruleTypes.includes(rule.type)) {
    throw new Error(`rule.type must be one of the following:
      ${ruleTypes.join('\n  ')}
    `);
  }
 
  if (!Object.prototype.hasOwnProperty.call(rule, 'input')) {
    throw new Error('rule must have an input property');
  } else if (!isFunction(rule.input) && !isString(rule.input)) {
    throw new Error('rule.input must be a function or a string');
  }
 
  if (
    rule.type !== 'percent'
    && !Object.prototype.hasOwnProperty.call(rule, 'property')
  ) {
    throw new Error('rule.property must exist for all types except "percent"');
  }
}
 
module.exports = validateRule;