Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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, {
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;
}
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;
};
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;
};
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);
}
}
}
const content = React.useMemo(() => safeStringify(additional, undefined, 2), [additional]);
Object.entries(validations).map(([name, value]) => {
if (typeof value === 'object') {
value = safeStringify(value, undefined, 2);
}
return (
{name}: {String(value)}
);
}),
[validations]
{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>
O.chain(part => O.tryCatch(() => pointerToPath('#' + part))),
O.chain(path => O.fromNullable(_get(body, path)))
export const safePointerToPath = (pointer: string): JsonPath => {
const rawPointer = extractPointerFromRef(pointer);
return rawPointer ? pointerToPath(rawPointer) : [];
};