1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const json_1 = require("@stoplight/json");
|
5 | const json_ref_resolver_1 = require("@stoplight/json-ref-resolver");
|
6 | const yaml_1 = require("@stoplight/yaml");
|
7 | const lodash_1 = require("lodash");
|
8 | const functions_1 = require("./functions");
|
9 | const runner_1 = require("./runner");
|
10 | tslib_1.__exportStar(require("./types"), exports);
|
11 | class Spectral {
|
12 | constructor(opts) {
|
13 | this._rules = {};
|
14 | this._functions = functions_1.functions;
|
15 | this.resolver = opts && opts.resolver ? opts.resolver : new json_ref_resolver_1.Resolver();
|
16 | }
|
17 | run(target) {
|
18 | return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
19 | let results = [];
|
20 | let parsedResult;
|
21 | if (!exports.isParsedResult(target)) {
|
22 | parsedResult = {
|
23 | parsed: yaml_1.parseWithPointers(typeof target === 'string' ? target : json_1.safeStringify(target, undefined, 2)),
|
24 | getLocationForJsonPath: yaml_1.getLocationForJsonPath,
|
25 | };
|
26 | results = results.concat(formatParserDiagnostics(parsedResult.parsed, parsedResult.source));
|
27 | }
|
28 | else {
|
29 | parsedResult = target;
|
30 | }
|
31 | const resolvedTarget = (yield this.resolver.resolve(parsedResult.parsed.data)).result;
|
32 | return results.concat(runner_1.runRules(parsedResult, this.rules, this.functions, { resolvedTarget }));
|
33 | });
|
34 | }
|
35 | get functions() {
|
36 | return this._functions;
|
37 | }
|
38 | addFunctions(functions) {
|
39 | Object.assign(this._functions, lodash_1.merge({}, functions));
|
40 | }
|
41 | get rules() {
|
42 | const rules = {};
|
43 | for (const name in this._rules) {
|
44 | if (!this._rules.hasOwnProperty(name))
|
45 | continue;
|
46 | const rule = this._rules[name];
|
47 | rules[name] = Object.assign({ name }, rule);
|
48 | }
|
49 | return rules;
|
50 | }
|
51 | addRules(rules) {
|
52 | Object.assign(this._rules, lodash_1.merge({}, rules));
|
53 | }
|
54 | mergeRules(rules) {
|
55 | for (const ruleName in lodash_1.merge({}, rules)) {
|
56 | if (!rules.hasOwnProperty(ruleName))
|
57 | continue;
|
58 | const rule = rules[ruleName];
|
59 | if (rule) {
|
60 | this._rules[ruleName] = lodash_1.merge(this._rules[ruleName], rule);
|
61 | }
|
62 | }
|
63 | }
|
64 | applyRuleDeclarations(declarations) {
|
65 | for (const ruleName in declarations) {
|
66 | if (!declarations.hasOwnProperty(ruleName))
|
67 | continue;
|
68 | const declaration = declarations[ruleName];
|
69 | const rule = this.rules[ruleName];
|
70 | if (rule) {
|
71 | if (typeof declaration === 'boolean') {
|
72 | this._rules[ruleName].enabled = declaration;
|
73 | }
|
74 | }
|
75 | }
|
76 | }
|
77 | }
|
78 | exports.Spectral = Spectral;
|
79 | exports.isParsedResult = (obj) => {
|
80 | if (!obj || typeof obj !== 'object')
|
81 | return false;
|
82 | if (!obj.parsed || typeof obj.parsed !== 'object')
|
83 | return false;
|
84 | if (!obj.getLocationForJsonPath || typeof obj.getLocationForJsonPath !== 'function')
|
85 | return false;
|
86 | return true;
|
87 | };
|
88 | function formatParserDiagnostics(parsed, source) {
|
89 | return parsed.diagnostics.map(diagnostic => (Object.assign({}, diagnostic, { path: [], source })));
|
90 | }
|
91 |
|
\ | No newline at end of file |