@stoplight/spectral-core
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -5,3 +5,3 @@ "use strict"; | ||
const types_1 = require("@stoplight/types"); | ||
exports.SPECTRAL_PKG_VERSION = '1.3.0'; | ||
exports.SPECTRAL_PKG_VERSION = '1.4.0'; | ||
exports.DEFAULT_PARSER_OPTIONS = Object.freeze({ | ||
@@ -8,0 +8,0 @@ incompatibleValues: types_1.DiagnosticSeverity.Error, |
import { JsonPath, Optional } from '@stoplight/types'; | ||
import { JSONPathExpression } from 'nimma'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
import { IGivenNode } from '../../types'; | ||
import { Ruleset } from '../ruleset'; | ||
@@ -40,4 +38,2 @@ import { Format } from '../format'; | ||
documentationUrl: string | null; | ||
expressions?: JSONPathExpression[] | null; | ||
get isOptimized(): boolean; | ||
constructor(name: string, definition: RuleDefinition, owner: Ruleset); | ||
@@ -58,6 +54,4 @@ overrides?: { | ||
matchesFormat(formats: Set<Format> | null): boolean; | ||
optimize(): boolean; | ||
clone(): Rule; | ||
hookup(cb: (rule: Rule, node: IGivenNode) => void): void; | ||
toJSON(): StringifiedRule; | ||
} |
@@ -6,3 +6,2 @@ "use strict"; | ||
const tslib_1 = require("tslib"); | ||
const nimma_1 = require("nimma"); | ||
const path_1 = require("@stoplight/path"); | ||
@@ -36,5 +35,2 @@ const json_1 = require("@stoplight/json"); | ||
} | ||
get isOptimized() { | ||
return Array.isArray(this.expressions); | ||
} | ||
get enabled() { | ||
@@ -110,32 +106,5 @@ return tslib_1.__classPrivateFieldGet(this, _Rule_enabled, "f") || this.overrides !== void 0; | ||
} | ||
optimize() { | ||
if (this.expressions !== void 0) | ||
return this.isOptimized; | ||
try { | ||
this.expressions = this.given.map(given => { | ||
const expr = new nimma_1.JSONPathExpression(given, stub, stub); | ||
if (expr.matches === null) { | ||
throw new Error(`Rule "${this.name}": cannot optimize ${given}`); | ||
} | ||
return expr; | ||
}); | ||
} | ||
catch { | ||
this.expressions = null; | ||
} | ||
return this.isOptimized; | ||
} | ||
clone() { | ||
return new Rule(this.name, this.definition, this.owner); | ||
} | ||
hookup(cb) { | ||
for (const expr of this.expressions) { | ||
expr.onMatch = (value, path) => { | ||
cb(this, { | ||
path, | ||
value, | ||
}); | ||
}; | ||
} | ||
} | ||
toJSON() { | ||
@@ -189,4 +158,2 @@ return { | ||
}; | ||
function stub() { | ||
} | ||
//# sourceMappingURL=rule.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Runner = void 0; | ||
const jsonpath_plus_1 = require("jsonpath-plus"); | ||
const lodash_1 = require("lodash"); | ||
const nimma_1 = require("nimma"); | ||
const utils_1 = require("../utils"); | ||
const lintNode_1 = require("./lintNode"); | ||
const lodash_2 = require("lodash"); | ||
const runRule = (context, rule) => { | ||
const target = rule.resolved ? context.documentInventory.resolved : context.documentInventory.unresolved; | ||
for (const given of rule.given) { | ||
if (given === '$') { | ||
lintNode_1.lintNode(context, { | ||
path: ['$'], | ||
value: target, | ||
}, rule); | ||
} | ||
else if (lodash_1.isObject(target)) { | ||
jsonpath_plus_1.JSONPath({ | ||
path: given, | ||
json: target, | ||
resultType: 'all', | ||
callback: (result => { | ||
lintNode_1.lintNode(context, { | ||
path: lodash_2.toPath(result.path.slice(1)), | ||
value: result.value, | ||
}, rule); | ||
}), | ||
}); | ||
} | ||
} | ||
}; | ||
const legacy_1 = require("nimma/legacy"); | ||
const fallbacks_1 = require("nimma/fallbacks"); | ||
const json_1 = require("@stoplight/json"); | ||
const lodash_1 = require("lodash"); | ||
class Runner { | ||
@@ -48,2 +24,4 @@ constructor(runtime, inventory) { | ||
async run(ruleset) { | ||
var _a; | ||
var _b; | ||
this.runtime.emit('setup'); | ||
@@ -58,31 +36,20 @@ const { inventory: documentInventory } = this; | ||
}; | ||
const relevantRules = Object.values(rules).filter(rule => rule.enabled && rule.matchesFormat(documentInventory.formats)); | ||
const optimizedRules = []; | ||
const optimizedUnresolvedRules = []; | ||
const unoptimizedRules = []; | ||
const traverseCb = (rule, node) => { | ||
lintNode_1.lintNode(runnerContext, node, rule); | ||
const enabledRules = Object.values(rules).filter(rule => rule.enabled); | ||
const relevantRules = enabledRules.filter(rule => rule.matchesFormat(documentInventory.formats)); | ||
const callbacks = { | ||
resolved: {}, | ||
unresolved: {}, | ||
}; | ||
for (const rule of relevantRules) { | ||
if (!rule.isOptimized) { | ||
unoptimizedRules.push(rule); | ||
continue; | ||
for (const given of rule.given) { | ||
const cb = (scope) => { | ||
lintNode_1.lintNode(runnerContext, scope, rule); | ||
}; | ||
((_a = (_b = callbacks[rule.resolved ? 'resolved' : 'unresolved'])[given]) !== null && _a !== void 0 ? _a : (_b[given] = [])).push(cb); | ||
} | ||
if (rule.resolved) { | ||
optimizedRules.push(rule); | ||
} | ||
else { | ||
optimizedUnresolvedRules.push(rule); | ||
} | ||
rule.hookup(traverseCb); | ||
} | ||
if (optimizedRules.length > 0) { | ||
nimma_1.traverse(Object(runnerContext.documentInventory.resolved), optimizedRules.flatMap(pickExpressions)); | ||
execute(runnerContext.documentInventory.resolved, callbacks.resolved, relevantRules.flatMap(r => (r.resolved ? r.given : []))); | ||
if (Object.keys(callbacks.unresolved).length > 0) { | ||
execute(runnerContext.documentInventory.unresolved, callbacks.unresolved, relevantRules.flatMap(r => (!r.resolved ? r.given : []))); | ||
} | ||
if (optimizedUnresolvedRules.length > 0) { | ||
nimma_1.traverse(Object(runnerContext.documentInventory.unresolved), optimizedUnresolvedRules.flatMap(pickExpressions)); | ||
} | ||
for (const rule of unoptimizedRules) { | ||
runRule(runnerContext, rule); | ||
} | ||
this.runtime.emit('beforeTeardown'); | ||
@@ -103,5 +70,40 @@ try { | ||
exports.Runner = Runner; | ||
function pickExpressions({ expressions }) { | ||
return expressions; | ||
function execute(input, callbacks, jsonPathExpressions) { | ||
var _a; | ||
if (!json_1.isPlainObject(input) && !Array.isArray(input)) { | ||
for (const cb of (_a = callbacks.$) !== null && _a !== void 0 ? _a : []) { | ||
cb({ | ||
path: [], | ||
value: input, | ||
}); | ||
} | ||
return; | ||
} | ||
try { | ||
const nimma = new legacy_1.default(jsonPathExpressions, { | ||
fallback: fallbacks_1.jsonPathPlus, | ||
unsafe: false, | ||
output: 'auto', | ||
}); | ||
nimma.query(input, Object.entries(callbacks).reduce((mapped, [key, cbs]) => { | ||
mapped[key] = scope => { | ||
for (const cb of cbs) { | ||
cb(scope); | ||
} | ||
}; | ||
return mapped; | ||
}, {})); | ||
} | ||
catch (e) { | ||
if (isAggregateError(e) && e.errors.length === 1) { | ||
throw e.errors[0]; | ||
} | ||
else { | ||
throw e; | ||
} | ||
} | ||
} | ||
function isAggregateError(maybeAggregateError) { | ||
return lodash_1.isError(maybeAggregateError) && maybeAggregateError.constructor.name === 'AggregateError'; | ||
} | ||
//# sourceMappingURL=runner.js.map |
@@ -6,3 +6,2 @@ import { IDiagnostic, JsonPath } from '@stoplight/types'; | ||
resolver?: Resolver; | ||
useNimma?: boolean; | ||
} | ||
@@ -9,0 +8,0 @@ export interface IRunOpts { |
{ | ||
"name": "@stoplight/spectral-core", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"main": "dist/index.js", | ||
@@ -39,6 +39,7 @@ "types": "dist/index.d.ts", | ||
"json-schema": "0.3.0", | ||
"jsonpath-plus": "5.0.7", | ||
"jsonpath-plus": "6.0.1", | ||
"lodash": "~4.17.21", | ||
"lodash.topath": "^4.5.2", | ||
"minimatch": "3.0.4", | ||
"nimma": "0.0.0", | ||
"nimma": "0.1.1", | ||
"tslib": "~2.3.0" | ||
@@ -45,0 +46,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155782
20
2366
+ Addedlodash.topath@^4.5.2
+ Addedjsonpath-plus@6.0.1(transitive)
+ Addedlodash.topath@4.5.2(transitive)
+ Addednimma@0.1.1(transitive)
- Removedjsonpath-plus@5.0.7(transitive)
- Removednimma@0.0.0(transitive)
Updatedjsonpath-plus@6.0.1
Updatednimma@0.1.1