How to use the @stoplight/json.hasRef function in @stoplight/json

To help you get started, we’ve selected a few @stoplight/json examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stoplightio / spectral / src / utils / refs.ts View on Github external
export const traverseObjUntilRef = (obj: unknown, path: JsonPath): string | null => {
  let piece: unknown = obj;

  for (const segment of path.slice()) {
    if (!isObject(piece)) {
      throw new TypeError('Segment is not a part of the object');
    }

    if (segment in piece) {
      piece = piece[segment];
    } else if (hasRef(piece)) {
      return piece.$ref;
    } else {
      throw new Error('Segment is not a part of the object');
    }

    path.shift();
  }

  if (isObject(piece) && hasRef(piece) && Object.keys(piece).length === 1) {
    return piece.$ref;
  }

  return null;
};
github stoplightio / spectral / src / utils / refs.ts View on Github external
if (!isObject(piece)) {
      throw new TypeError('Segment is not a part of the object');
    }

    if (segment in piece) {
      piece = piece[segment];
    } else if (hasRef(piece)) {
      return piece.$ref;
    } else {
      throw new Error('Segment is not a part of the object');
    }

    path.shift();
  }

  if (isObject(piece) && hasRef(piece) && Object.keys(piece).length === 1) {
    return piece.$ref;
  }

  return null;
};
github stoplightio / spectral / src / rulesets / oas / functions / refSiblings.ts View on Github external
function* siblingIterator(obj: object, path: JsonPath): IterableIterator {
  for (const key in obj) {
    if (!Object.hasOwnProperty.call(obj, key)) continue;
    const scopedPath = [...path, key];
    if (hasRef(obj) && key !== '$ref') {
      yield scopedPath;
    }

    if (key !== '$ref' && typeof obj[key] === 'object' && obj[key] !== null) {
      yield* siblingIterator(obj[key], scopedPath);
    }
  }
}