All files / latest/src/helpers/pathTransformer/src pathValidator.js

95% Statements 38/40
90.91% Branches 10/11
100% Functions 3/3
95% Lines 38/40

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 38 39 40 411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1140x 1140x 1140x 6x 6x 6x 1140x 1140x 1140x 1140x     1x 1x 1x 1x 1x 1x 328x 326x 1140x 1140x 326x 326x 2x 2x 1x 1x  
const stringify = require('../../../handlers/basic/stringify');
 
/**
 * Check if input is existent and an array
 */
const isArray = (path) => path && Array.isArray(path);
 
/**
 * Check if input is string, number or query, otherwise throw error
 */
// eslint-disable-next-line complexity
const validateElement = (element) => {
  if (element
    && (
      element.string !== undefined
      || element.number !== undefined
      || element.query
      || element.wildcard
    )
  ) {
    return;
  }
  throw new Error(`Path element ${stringify(element)} is invalid.`);
};
 
/**
 * Validate path, if invalid throw error
 * @param {any} path - representation of path
 */
const pathValidator = (path) => {
  if (isArray(path)) {
    path.forEach((element) => {
      validateElement(element);
    });
    return path;
  }
  throw new Error('Input path is invalid.');
};
 
module.exports = pathValidator;