How to use @stoplight/json - 10 common examples

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 / spectral.ts View on Github external
public async runWithResolved(
    target: IParsedResult | object | string,
    opts: IRunOpts = {},
  ): Promise {
    let results: IRuleResult[] = [];

    let parsedResult: IParsedResult | IParsedResult>;
    if (!isParsedResult(target)) {
      parsedResult = {
        parsed: parseYaml(typeof target === 'string' ? target : safeStringify(target, undefined, 2)),
        getLocationForJsonPath: getLocationForJsonPathYaml,
        // we need to normalize the path in case path with forward slashes is given
        source: opts.resolve?.documentUri && normalize(opts.resolve.documentUri),
      };
    } else {
      parsedResult = target;
    }

    results = results.concat(formatParserDiagnostics(parsedResult.parsed.diagnostics, parsedResult.source));

    const documentUri = opts.resolve && opts.resolve.documentUri;
    const refDiagnostics: IRuleResult[] = [];

    const resolved = new Resolved(
      parsedResult,
      await this._resolver.resolve(parsedResult.parsed.data, {
github stoplightio / spectral / src / resolved.ts View on Github external
public doesBelongToDoc(path: JsonPath): boolean {
    if (path.length === 0) {
      // todo: each rule and their function should be context-aware, meaning they should aware of the fact they operate on resolved content
      // let's assume the error was reported correctly by any custom rule /shrug
      return true;
    }

    let piece = this.unresolved;

    for (let i = 0; i < path.length; i++) {
      if (!isObject(piece)) return false;

      if (path[i] in piece) {
        piece = piece[path[i]];
      } else if (hasRef(piece)) {
        return this.doesBelongToDoc([...pointerToPath(piece.$ref), ...path.slice(i)]);
      }
    }

    return true;
  }
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);
    }
  }
}
github stoplightio / json-schema-viewer / src / components / Additional.tsx View on Github external
  const content = React.useMemo(() => safeStringify(additional, undefined, 2), [additional]);
github stoplightio / json-schema-viewer / src / components / Validations.tsx View on Github external
Object.entries(validations).map(([name, value]) => {
        if (typeof value === 'object') {
          value = safeStringify(value, undefined, 2);
        }

        return (
          
            {name}: {String(value)}
          
        );
      }),
    [validations]
github stoplightio / json-schema-viewer / src / PropValidations.tsx View on Github external
{Object.entries(validations).map(([k, v]) => {
          let type = typeof v;

          if (k === 'default' && ['object', 'boolean'].includes(type)) {
            v = safeStringify(v);

            type = typeof v;
          }

          if (type === 'boolean') {
            return (
              <div>
                {k}: {v.toString()}
              </div>
            );
          }

          if (type !== 'object') {
            return (
              <div>
                {k}: {v}</div>
github stoplightio / prism / packages / http / src / utils / runtimeExpression.ts View on Github external
          O.chain(part => O.tryCatch(() => pointerToPath('#' + part))),
          O.chain(path => O.fromNullable(_get(body, path)))
github stoplightio / spectral / src / utils / refs.ts View on Github external
export const safePointerToPath = (pointer: string): JsonPath => {
  const rawPointer = extractPointerFromRef(pointer);
  return rawPointer ? pointerToPath(rawPointer) : [];
};