UNPKG

@microsoft.azure/openapi-validator-core

Version:
119 lines 5.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LintRunner = void 0; const jsonpath_1 = require("./jsonpath"); const utils_1 = require("./utils"); const isLegacyRule = (rule) => { return rule.then.execute.name === "run"; }; class LintRunner { constructor(loader, inventory) { this.loader = loader; this.inventory = inventory; this.runRules = async (document, openapiDefinition, sendMessage, openapiType, ruleset, inventory, scope = "File") => { const rulesToRun = Object.entries(ruleset.rules).filter((rule) => rule[1].openapiType & openapiType && (rule[1].scope || "File") === scope); const getArgs = (rule, section, doc, location) => { if (isLegacyRule(rule)) { return [doc, section, location, { specPath: document, inventory }]; } else { return [ section, rule.then.options, { document: doc, location, specPath: document, inventory, }, ]; } }; for (const [ruleName, rule] of rulesToRun) { let givens = rule.given || "$"; if (!Array.isArray(givens)) { givens = [givens]; } const targetDefinition = openapiDefinition; for (const given of givens) { for (const section of (0, jsonpath_1.nodes)(targetDefinition, given)) { const fieldMatch = rule.then.fieldMatch; if (fieldMatch) { for (const subSection of (0, jsonpath_1.nodes)(section.value, fieldMatch)) { const location = section.path.slice(1).concat(subSection.path.slice(1)); await processRule(ruleName, rule, subSection.value, targetDefinition, location); } } else { const location = section.path.slice(1); await processRule(ruleName, rule, section.value, targetDefinition, location); } } } } async function processRule(ruleName, rule, section, targetDefinition, location) { try { const args = getArgs(rule, section, targetDefinition, location); for await (const message of rule.then.execute(...args)) { emitResult(ruleName, rule, message); } } catch (error) { error.message = `azure-openapi-validator/core/src/runner.ts/LintRunner.runRules/processRule error. ` + `ruleName: ${ruleName}, specFilePath: ${document}, ` + `jsonPath: ${(0, utils_1.convertJsonPath)(openapiDefinition, location)}, ` + `errorName: ${error === null || error === void 0 ? void 0 : error.name}, errorMessage: ${error === null || error === void 0 ? void 0 : error.message}`; throw error; } } function emitResult(ruleName, rule, message) { const readableCategory = rule.category; const range = (0, utils_1.getRange)(inventory, document, message.location); const msg = { id: rule.id, type: rule.severity, category: readableCategory, message: message.message, code: ruleName, sources: [document], jsonpath: (0, utils_1.convertJsonPath)(openapiDefinition, message.location), range, }; sendMessage(msg); } }; } async execute(swaggerPaths, options, cb) { const specsPromises = []; for (const spec of swaggerPaths) { specsPromises.push(this.inventory.loadDocument(spec)); } const documents = (await Promise.all(specsPromises)); const msgs = []; const sendMessage = (msg) => { msgs.push(msg); if (cb) { cb(msg); } }; const runPromises = []; let runGlobalRuleFlag = false; for (const doc of documents) { for (const scope of ["Global", "File"]) { if (scope === "Global" && runGlobalRuleFlag) { continue; } else { runGlobalRuleFlag = true; } const promise = this.runRules(doc.getDocumentPath(), doc.getObj(), sendMessage, options.openapiType, await this.loader.getRuleSet(), this.inventory, scope); runPromises.push(promise); } } await Promise.all(runPromises); return msgs; } } exports.LintRunner = LintRunner; //# sourceMappingURL=runner.js.map