All files / lib validate-item.js

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

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     18x 18x 1x 1x   17x       1x   17x             2x   17x     1x  
const isObject = require('lodash.isobject');
 
function validateItem(item, key) {
  const errors = [];
  if (!isObject(item)) {
    errors.push(new Error(`config item must be object for key "${key}"`));
    return errors;
  }
  if (
    Object.prototype.hasOwnProperty.call(item, 'value')
    && Object.prototype.hasOwnProperty.call(item, 'values')
  ) {
    errors.push(new Error(`config items cannot have both value and values for key "${key}"`));
  }
  if (
    Object.prototype.hasOwnProperty.call(item, 'values')
    && (
      !Array.isArray(item.values)
      || item.values.length === 0
    )
  ) {
    errors.push(new Error(`config item values must be an array with at least one value for key "${key}"`));
  }
  return errors;
}
 
module.exports = validateItem;