All files / src/plugins/utils hasRefProperty.js

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 8/8

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 3042x                 42x 339x 337x             2x     339x 339x         769x    
const at = require('lodash/at');
 
/*
  Checks the unresolved spec to see if the object at path `path`
  has a `$ref` property. Useful when validating a resolved spec
  and want to know if a certain object was referenced or defined
  inline.
*/
 
module.exports = (jsSpec, path) => {
  if (Array.isArray(path)) {
    path = convertArrayToBracketNotation(path);
  } else {
    // if not array, assuming it is a dot separated string
    //
    // note: it is not a good idea to use this pattern,
    // as parameter names sometimes have periods in them.
    // only arrays should be passed in
    path = convertArrayToBracketNotation(path.split('.'));
  }
 
  const objectAtGivenPath = at(jsSpec, path)[0];
  return Boolean(objectAtGivenPath && objectAtGivenPath.$ref);
};
 
// returns a string with format parentKey['nextKey']['nextKey']['etc']
function convertArrayToBracketNotation(path) {
  return path.reduce((result, element) => result + `['${element}']`);
}