Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@stoplight/spectral-core

Package Overview
Dependencies
Maintainers
24
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stoplight/spectral-core - npm Package Compare versions

Comparing version 1.5.1 to 1.6.0

dist/guards/isAggregateError.d.ts

8

dist/meta/rule.schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/stoplightio/spectral/blob/develop/src/meta/rule.schema.json",
"$id": "@stoplight/spectral-core/meta/rule.schema",
"$defs": {

@@ -23,3 +23,3 @@ "Then": {

"Severity": {
"$ref": "shared.json#/$defs/Severity"
"$ref": "shared#severity"
}

@@ -70,3 +70,3 @@ },

"formats": {
"$ref": "shared.json#/$defs/Formats"
"$ref": "shared#formats"
},

@@ -100,3 +100,3 @@ "then": {

{
"$ref": "shared.json#/$defs/HumanReadableSeverity"
"$ref": "shared#/$defs/HumanReadableSeverity"
},

@@ -103,0 +103,0 @@ {

{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/stoplightio/spectral/blob/develop/src/meta/ruleset.schema.json",
"$id": "@stoplight/spectral-core/meta/ruleset.schema",
"type": "object",

@@ -15,7 +15,7 @@ "additionalProperties": false,

"additionalProperties": {
"$ref": "rule.schema.json#"
"$ref": "rule.schema#"
}
},
"formats": {
"$ref": "shared.json#/$defs/Formats"
"$ref": "shared#formats"
},

@@ -60,6 +60,6 @@ "extends": {

"duplicateKeys": {
"$ref": "shared.json#/$defs/Severity"
"$ref": "shared#severity"
},
"incompatibleValues": {
"$ref": "shared.json#/$defs/Severity"
"$ref": "shared#severity"
}

@@ -96,3 +96,3 @@ },

"additionalProperties": {
"$ref": "shared.json#/$defs/Severity"
"$ref": "shared#severity"
},

@@ -136,3 +136,3 @@ "errorMessage": {

"formats": {
"$ref": "#/properties/formats"
"$ref": "shared#formats"
},

@@ -178,5 +178,42 @@ "extends": {

"additionalProperties": {
"type": "string",
"pattern": "^[$#]",
"errorMessage": "the value of an alias must be a valid JSON Path expression or a reference to the existing Alias optionally paired with a JSON Path expression subset"
"oneOf": [
{
"$id": "alias-value",
"type": "string",
"pattern": "^[$#]",
"errorMessage": {
"_": "the value of an alias must be a valid JSON Path expression, a reference to the existing Alias optionally paired with a JSON Path expression subset, or contain a valid set of targets",
"pattern": "the value of an alias must be a valid JSON Path expression or a reference to the existing Alias optionally paired with a JSON Path expression subset"
}
},
{
"type": "object",
"properties": {
"description": {
"type": "string"
},
"targets": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"formats": {
"$ref": "shared#formats"
},
"given": {
"$ref": "alias-value"
}
},
"required": ["formats", "given"],
"errorMessage": "a valid target must contain given and non-empty formats"
},
"errorMessage": {
"minItems": "targets must have at least a single alias definition"
}
}
},
"required": ["targets"]
}
]
}

@@ -183,0 +220,0 @@ }

{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/stoplightio/spectral/blob/develop/src/meta/shared.json",
"$id": "@stoplight/spectral-core/meta/shared",
"$defs": {
"Formats": {
"$anchor": "formats",
"type": "array",
"items": {
"$ref": "#/$defs/Format"
"$ref": "#format"
},

@@ -13,2 +14,3 @@ "errorMessage": "must be an array of formats"

"Format": {
"$anchor": "format",
"spectral-runtime": "spectral-format",

@@ -24,2 +26,3 @@ "errorMessage": "must be a valid format"

"Severity": {
"$anchor": "severity",
"oneOf": [

@@ -26,0 +29,0 @@ {

@@ -1,6 +0,6 @@

import { JsonPath, Optional } from '@stoplight/types';
import { DiagnosticSeverity } from '@stoplight/types';
import { JsonPath, Optional, DiagnosticSeverity } from '@stoplight/types';
import { Ruleset } from '../ruleset';
import { Format } from '../format';
import { HumanReadableDiagnosticSeverity, IRuleThen, RuleDefinition } from '../types';
import type { HumanReadableDiagnosticSeverity, IRuleThen, RuleDefinition } from '../types';
import { FormatsSet } from '../utils/formatsSet';
export interface IRule {

@@ -20,3 +20,3 @@ description: string | null;

name: string;
formats: string[] | null;
formats: FormatsSet | null;
then: (Pick<IRuleThen, 'field'> & {

@@ -36,3 +36,3 @@ function: string;

resolved: boolean;
formats: Set<Format> | null;
formats: FormatsSet | null;
recommended: boolean;

@@ -54,2 +54,3 @@ documentationUrl: string | null;

set given(given: RuleDefinition['given']);
getGivenForFormats(formats: Set<Format<any>> | null): string[];
matchesFormat(formats: Set<Format> | null): boolean;

@@ -56,0 +57,0 @@ clone(): Rule;

"use strict";
var _Rule_instances, _Rule_severity, _Rule_enabled, _Rule_then, _Rule_given, _Rule_resolveAlias;
var _Rule_instances, _Rule_severity, _Rule_enabled, _Rule_then, _Rule_given, _Rule_resolveAlias, _Rule_resolveAliasForFormats;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rule = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const path_1 = require("@stoplight/path");
const json_1 = require("@stoplight/json");
const spectral_runtime_1 = require("@stoplight/spectral-runtime");
const severity_1 = require("../utils/severity");
const minimatch_1 = require("../utils/minimatch");
const spectral_runtime_1 = require("@stoplight/spectral-runtime");
const formatsSet_1 = require("../utils/formatsSet");
const ALIAS = /^#([A-Za-z0-9_-]+)/;

@@ -30,3 +32,3 @@ class Rule {

this.resolved = definition.resolved !== false;
this.formats = 'formats' in definition ? new Set(definition.formats) : null;
this.formats = 'formats' in definition ? new formatsSet_1.FormatsSet(definition.formats) : null;
this.then = definition.then;

@@ -86,7 +88,15 @@ this.given = definition.given;

get given() {
return tslib_1.__classPrivateFieldGet(this, _Rule_given, "f").map(tslib_1.__classPrivateFieldGet(this, _Rule_instances, "m", _Rule_resolveAlias), this);
return tslib_1.__classPrivateFieldGet(this, _Rule_given, "f");
}
set given(given) {
tslib_1.__classPrivateFieldSet(this, _Rule_given, Array.isArray(given) ? given : [given], "f");
const actualGiven = Array.isArray(given) ? given : [given];
tslib_1.__classPrivateFieldSet(this, _Rule_given, this.owner.hasComplexAliases
? actualGiven
: actualGiven.map(expr => tslib_1.__classPrivateFieldGet(this, _Rule_instances, "m", _Rule_resolveAlias).call(this, expr, null)).filter(lodash_1.isString), "f");
}
getGivenForFormats(formats) {
return this.owner.hasComplexAliases
? tslib_1.__classPrivateFieldGet(this, _Rule_given, "f").map(expr => tslib_1.__classPrivateFieldGet(this, _Rule_instances, "m", _Rule_resolveAlias).call(this, expr, formats)).filter(lodash_1.isString)
: tslib_1.__classPrivateFieldGet(this, _Rule_given, "f");
}
matchesFormat(formats) {

@@ -119,3 +129,3 @@ if (this.formats === null) {

resolved: this.resolved,
formats: this.formats === null ? null : Array.from(this.formats).map(fn => { var _a; return (_a = fn.displayName) !== null && _a !== void 0 ? _a : fn.name; }),
formats: this.formats,
then: this.then.map(then => ({

@@ -126,3 +136,3 @@ ...then.function,

})),
given: tslib_1.__classPrivateFieldGet(this, _Rule_given, "f"),
given: Array.isArray(this.definition.given) ? this.definition.given : [this.definition.given],
owner: this.owner.id,

@@ -133,3 +143,3 @@ };

exports.Rule = Rule;
_Rule_severity = new WeakMap(), _Rule_enabled = new WeakMap(), _Rule_then = new WeakMap(), _Rule_given = new WeakMap(), _Rule_instances = new WeakSet(), _Rule_resolveAlias = function _Rule_resolveAlias(expr) {
_Rule_severity = new WeakMap(), _Rule_enabled = new WeakMap(), _Rule_then = new WeakMap(), _Rule_given = new WeakMap(), _Rule_instances = new WeakSet(), _Rule_resolveAlias = function _Rule_resolveAlias(expr, formats) {
var _a;

@@ -151,11 +161,35 @@ let resolvedExpr = expr;

}
if (alias.length + 1 === expr.length) {
resolvedExpr = this.owner.aliases[alias];
const aliasValue = this.owner.aliases[alias];
let actualAliasValue;
if (typeof aliasValue === 'string') {
actualAliasValue = aliasValue;
}
else {
resolvedExpr = this.owner.aliases[alias] + resolvedExpr.slice(alias.length + 1);
actualAliasValue = tslib_1.__classPrivateFieldGet(this, _Rule_instances, "m", _Rule_resolveAliasForFormats).call(this, aliasValue, formats);
}
if (actualAliasValue === null) {
return null;
}
if (actualAliasValue.length + 1 === expr.length) {
resolvedExpr = actualAliasValue;
}
else {
resolvedExpr = actualAliasValue + resolvedExpr.slice(alias.length + 1);
}
}
return resolvedExpr;
}, _Rule_resolveAliasForFormats = function _Rule_resolveAliasForFormats({ targets }, formats) {
if (formats === null || formats.size === 0) {
return null;
}
for (let i = targets.length - 1; i >= 0; i--) {
const target = targets[i];
for (const format of target.formats) {
if (formats.has(format)) {
return target.given;
}
}
}
return null;
};
//# sourceMappingURL=rule.js.map
import { Rule, StringifiedRule } from './rule/rule';
import { FileRulesetSeverityDefinition, ParserOptions, RulesetAliasesDefinition, RulesetDefinition, RulesetOverridesDefinition } from './types';
import { Format } from './format';
import { FormatsSet } from './utils/formatsSet';
declare const STACK_SYMBOL: unique symbol;

@@ -15,3 +15,3 @@ declare type RulesetContext = {

aliases: RulesetAliasesDefinition | null;
formats: string[] | null;
formats: FormatsSet | null;
rules: Record<string, StringifiedRule>;

@@ -26,5 +26,6 @@ overrides: RulesetOverridesDefinition | null;

protected readonly extends: Ruleset[] | null;
readonly formats: Set<Format<void>>;
readonly formats: FormatsSet<import("./format").Format<void>>;
readonly overrides: RulesetOverridesDefinition | null;
readonly aliases: RulesetAliasesDefinition | null;
readonly hasComplexAliases: boolean;
readonly rules: Record<string, Rule>;

@@ -31,0 +32,0 @@ readonly definition: RulesetDefinition;

@@ -13,2 +13,3 @@ "use strict";

const json_1 = require("@stoplight/json");
const formatsSet_1 = require("./utils/formatsSet");
const STACK_SYMBOL = Symbol('@stoplight/spectral/ruleset/#stack');

@@ -23,3 +24,3 @@ const DEFAULT_RULESET_FILE = /^\.?spectral\.(ya?ml|json|m?js)$/;

this.id = SEED++;
this.formats = new Set();
this.formats = new formatsSet_1.FormatsSet();
_Ruleset_context.set(this, void 0);

@@ -41,3 +42,19 @@ let definition;

}, "f");
this.aliases = definition.aliases === void 0 ? null : { ...definition.aliases };
let hasComplexAliases = false;
this.aliases =
definition.aliases === void 0
? null
: Object.fromEntries(Object.entries(definition.aliases).map(alias => {
const [name, value] = alias;
if (typeof value === 'string') {
return alias;
}
hasComplexAliases = true;
const targets = value.targets.map(target => ({
formats: new formatsSet_1.FormatsSet(target.formats),
given: target.given,
}));
return [name, { ...value, targets }];
}));
this.hasComplexAliases = hasComplexAliases;
const stack = (_a = context === null || context === void 0 ? void 0 : context[STACK_SYMBOL]) !== null && _a !== void 0 ? _a : new Map();

@@ -171,3 +188,3 @@ stack.set(this.definition, this);

aliases: this.aliases,
formats: this.formats.size === 0 ? null : Array.from(this.formats).map(fn => { var _a; return (_a = fn.displayName) !== null && _a !== void 0 ? _a : fn.name; }),
formats: this.formats.size === 0 ? null : this.formats,
rules: this.rules,

@@ -205,6 +222,7 @@ overrides: this.overrides,

else if (rule.owner !== this) {
rule.formats = rule.owner.definition.formats === void 0 ? null : new Set(rule.owner.definition.formats);
rule.formats =
rule.owner.definition.formats === void 0 ? null : new formatsSet_1.FormatsSet(rule.owner.definition.formats);
}
else if (this.definition.formats !== void 0) {
rule.formats = new Set(this.definition.formats);
rule.formats = new formatsSet_1.FormatsSet(this.definition.formats);
}

@@ -211,0 +229,0 @@ if (this.definition.documentationUrl !== void 0 && rule.documentationUrl === null) {

import { DiagnosticSeverity } from '@stoplight/types';
import { Format } from './format';
import { RulesetFunction, RulesetFunctionWithValidator } from '../types';
import { FormatsSet } from './utils/formatsSet';
export declare type HumanReadableDiagnosticSeverity = 'error' | 'warn' | 'info' | 'hint' | 'off';

@@ -41,3 +42,10 @@ export declare type FileRuleSeverityDefinition = DiagnosticSeverity | HumanReadableDiagnosticSeverity | boolean;

} & RulesetOverrideDefinition>;
export declare type RulesetAliasesDefinition = Record<string, string>;
export declare type RulesetScopedAliasDefinition = {
description?: string;
targets: {
formats: FormatsSet;
given: string;
}[];
};
export declare type RulesetAliasesDefinition = Record<string, string | RulesetScopedAliasDefinition>;
export declare type RulesetDefinition = Readonly<{

@@ -44,0 +52,0 @@ documentationUrl?: string;

@@ -13,3 +13,3 @@ "use strict";

const message = ajv_1._ `'spectral-message'`;
const ajv = new ajv_1.default({ allErrors: true, strict: true, strictRequired: false });
const ajv = new ajv_1.default({ allErrors: true, strict: true, strictRequired: false, keywords: ['$anchor'] });
ajv_formats_1.default(ajv);

@@ -47,3 +47,3 @@ ajv_errors_1.default(ajv);

const RULE_INSTANCE_PATH = /^\/rules\/[^/]+/;
const GENERIC_INSTANCE_PATH = /^\/(?:extends|overrides(?:\/\d+\/extends)?)/;
const GENERIC_INSTANCE_PATH = /^\/(?:aliases|extends|overrides(?:\/\d+\/extends)?)/;
class RulesetAjvValidationError extends RulesetValidationError {

@@ -50,0 +50,0 @@ constructor(ruleset, errors) {

@@ -9,3 +9,3 @@ "use strict";

const json_1 = require("@stoplight/json");
const lodash_1 = require("lodash");
const isAggregateError_1 = require("../guards/isAggregateError");
class Runner {

@@ -25,7 +25,8 @@ constructor(runtime, inventory) {

async run(ruleset) {
var _a;
var _b;
var _a, _b;
var _c;
this.runtime.emit('setup');
const { inventory: documentInventory } = this;
const { rules } = ruleset;
const formats = (_a = this.document.formats) !== null && _a !== void 0 ? _a : null;
const runnerContext = {

@@ -44,13 +45,17 @@ ruleset,

for (const rule of relevantRules) {
for (const given of rule.given) {
for (const given of rule.getGivenForFormats(formats)) {
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);
((_b = (_c = callbacks[rule.resolved ? 'resolved' : 'unresolved'])[given]) !== null && _b !== void 0 ? _b : (_c[given] = [])).push(cb);
}
}
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 : [])));
const resolvedJsonPaths = Object.keys(callbacks.resolved);
const unresolvedJsonPaths = Object.keys(callbacks.unresolved);
if (resolvedJsonPaths.length > 0) {
execute(runnerContext.documentInventory.resolved, callbacks.resolved, resolvedJsonPaths);
}
if (unresolvedJsonPaths.length > 0) {
execute(runnerContext.documentInventory.unresolved, callbacks.unresolved, unresolvedJsonPaths);
}
this.runtime.emit('beforeTeardown');

@@ -98,3 +103,3 @@ try {

catch (e) {
if (isAggregateError(e) && e.errors.length === 1) {
if (isAggregateError_1.isAggregateError(e) && e.errors.length === 1) {
throw e.errors[0];

@@ -107,5 +112,2 @@ }

}
function isAggregateError(maybeAggregateError) {
return lodash_1.isError(maybeAggregateError) && maybeAggregateError.constructor.name === 'AggregateError';
}
//# sourceMappingURL=runner.js.map
{
"name": "@stoplight/spectral-core",
"version": "1.5.1",
"version": "1.6.0",
"main": "dist/index.js",

@@ -5,0 +5,0 @@ "types": "dist/index.d.ts",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc