@stoplight/spectral
Version:
A flexible object linter with out of the box support for OpenAPI v2 and v3.
57 lines • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const jp = require("jsonpath");
const linter_1 = require("./linter");
exports.runRules = (parsedResult, rules, functions, opts) => {
let results = [];
for (const name in rules) {
if (!rules.hasOwnProperty(name))
continue;
const rule = rules[name];
if (!rule)
continue;
if (rule.hasOwnProperty('enabled') && !rule.enabled) {
continue;
}
try {
results = results.concat(runRule(parsedResult, rule, functions, opts));
}
catch (e) {
console.error(`Unable to run rule '${name}':\n${e}`);
}
}
return results;
};
const runRule = (parsedResult, rule, functions, opts) => {
const { parsed } = parsedResult;
const { data: target } = parsed;
let results = [];
let nodes = [];
if (rule.given && rule.given !== '$') {
nodes = jp.nodes(target, rule.given);
}
else {
nodes.push({
path: ['$'],
value: target,
});
}
for (const node of nodes) {
try {
const thens = Array.isArray(rule.then) ? rule.then : [rule.then];
for (const then of thens) {
const func = functions[then.function];
if (!func) {
console.warn(`Function ${then.function} not found. Called by rule ${rule.name}.`);
continue;
}
results = results.concat(linter_1.lintNode(node, rule, then, func, opts, parsedResult));
}
}
catch (e) {
console.warn(`Encountered error when running rule '${rule.name}' on node at path '${node.path}':\n${e}`);
}
}
return results;
};
//# sourceMappingURL=runner.js.map