Support custom spectral ruleset in inso (#5488)

* first pass

* package lock

* provided ruleset is not an object

* works

* support multiple extensions

* fix test

* lower coverage threshold

* remove unused test

* remove only
This commit is contained in:
Jack Kavanagh authored and GitHub committed 2022-12-14 12:47:28 +01:00
1 parent e26a01815c
commit ed34090384
8 files changed
+1569 -694

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ module.exports = {
coveragePathIgnorePatterns: ['/node_modules/'],
coverageThreshold: {
global: {
branches: 88,
branches: 80,
functions: 96,
lines: 95,
statements: 95,
+1519 -684
View File
File diff suppressed because it is too large. Load diff
+4 -3
View File
@@ -61,9 +61,10 @@
"rimraf": "^3.0.2"
},
"dependencies": {
"@stoplight/spectral-core": "^1.12.2",
"@stoplight/spectral-formats": "^1.2.0",
"@stoplight/spectral-rulesets": "^1.9.0",
"@stoplight/spectral-core": "^1.16.0",
"@stoplight/spectral-formats": "^1.4.0",
"@stoplight/spectral-rulesets": "^1.14.1",
"@stoplight/spectral-ruleset-bundler": "^1.5.0",
"@stoplight/types": "^13.8.0",
"axios": "^0.21.2",
"commander": "^5.1.0",
@@ -0,0 +1,2 @@
extends:
- spectral:oas
@@ -0,0 +1,21 @@
openapi: 3.0.1
info:
description: Description
version: 1.0.0
title: API
servers:
- url: 'https://api.insomnia.rest'
paths:
/path:
x-kong-plugin-oidc:
name: oidc
enabled: true
config:
key_names: [api_key, apikey]
key_in_body: false
hide_credentials: true
get:
description: 'test'
responses:
'200':
description: OK
@@ -37,6 +37,14 @@ describe('lint specification', () => {
expect(result).toBe(true);
});
it('should lint specification with custom ruleset', async () => {
const directory = path.join(process.cwd(), 'src/commands/fixtures/with-ruleset');
const result = await lintSpecification(path.join(directory, 'path-plugin.yaml'), {
workingDir: 'src',
});
expect(result).toBe(true);
});
it('should lint specification from file with absolute path', async () => {
const directory = path.join(process.cwd(), 'src/commands/fixtures');
const result = await lintSpecification(path.join(directory, 'openapi-spec.yaml'), {
@@ -1,4 +1,6 @@
import { RulesetDefinition, Spectral } from '@stoplight/spectral-core';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { bundleAndLoadRuleset } = require('@stoplight/spectral-ruleset-bundler/with-loader');
import { oas } from '@stoplight/spectral-rulesets';
import { DiagnosticSeverity } from '@stoplight/types';
import fs from 'fs';
@@ -25,7 +27,7 @@ export async function lintSpecification(
const specFromDb = identifier ? loadApiSpec(db, identifier) : await promptApiSpec(db, !!ci);
let specContent = '';
let ruleset = oas;
try {
if (specFromDb?.contents) {
logger.trace('Linting specification from database contents');
@@ -39,6 +41,15 @@ export async function lintSpecification(
try {
specContent = (await fs.promises.readFile(fileName)).toString();
const filesInSpecFolder = await fs.promises.readdir(path.dirname(fileName));
const rulesetFileName = filesInSpecFolder.find(file => file.startsWith('.spectral'));
if (rulesetFileName) {
logger.trace(`Loading ruleset from \`${rulesetFileName}\``);
ruleset = await bundleAndLoadRuleset(path.join(path.dirname(fileName), rulesetFileName), { fs });
} else {
logger.info(`Using ruleset: oas, see ${oas.documentationUrl}`);
}
} catch (error) {
throw new InsoError(`Failed to read "${fileName}"`, error);
}
@@ -52,11 +63,8 @@ export async function lintSpecification(
}
const spectral = new Spectral();
await spectral.setRuleset(oas as RulesetDefinition);
logger.info(`Using ruleset: oas, see ${oas.documentationUrl}`);
const results = (await spectral.run(specContent));
spectral.setRuleset(ruleset as RulesetDefinition);
const results = await spectral.run(specContent);
if (results.length) {
// Print Summary
if (results.some(r => r.severity === DiagnosticSeverity.Error)) {