Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ruleset): use human friendly severity levels #1099

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { getDiagnosticSeverity } from './src/rulesets/severity';
import { HumanReadableDiagnosticSeverity } from './src/types';
import { RulesetExceptionCollection } from './src/types/ruleset';

export const buildRulesetExceptionCollectionFrom = (
@@ -8,3 +11,11 @@ export const buildRulesetExceptionCollectionFrom = (
source[loc] = rules;
return source;
};

export const normalizeSeverityFromJsonRuleset = (severity: string | number | undefined): DiagnosticSeverity => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have a very similar function somewhere.
Most likely in merger, suggest consolidating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@P0lip I'm going to table this until #974 is merged. The way the oas3 tests are written make this change especially painful... Too painful.

Either I'm approaching this from the wrong angle or some refactoring should be done.

🤔

if (severity === void 0) {
return DiagnosticSeverity.Warning;
}

return getDiagnosticSeverity(severity as HumanReadableDiagnosticSeverity | DiagnosticSeverity);
};
7 changes: 4 additions & 3 deletions src/rulesets/__tests__/reader.jest.test.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { Dictionary } from '@stoplight/types';
import { DiagnosticSeverity } from '@stoplight/types';
import * as fs from 'fs';
import * as nock from 'nock';
import { normalizeSeverityFromJsonRuleset } from '../../../setupTests';
import { Spectral } from '../../spectral';
import { IRule, RuleType } from '../../types';
import { readRuleset } from '../reader';
@@ -145,7 +146,7 @@ describe('Rulesets reader', () => {
oasRules[name] = {
...rule,
formats: expect.arrayContaining([expect.any(String)]),
...((rule as IRule).severity === void 0 && { severity: DiagnosticSeverity.Warning }),
severity: normalizeSeverityFromJsonRuleset(rule.severity),
...((rule as IRule).recommended === void 0 && { recommended: true }),
then: expect.any(Object),
};
@@ -172,7 +173,7 @@ describe('Rulesets reader', () => {
rules[name] = {
...rule,
formats: expect.arrayContaining([expect.any(String)]),
...((rule as IRule).severity === undefined && { severity: DiagnosticSeverity.Warning }),
severity: normalizeSeverityFromJsonRuleset(rule.severity),
...((rule as IRule).recommended === false && { severity: -1 }),
...((rule as IRule).recommended === void 0 && { recommended: true }),
then: expect.any(Object),
@@ -213,7 +214,7 @@ describe('Rulesets reader', () => {
const formattedRule: IRule = {
...rule,
formats: expect.arrayContaining([expect.any(String)]),
...((rule as IRule).severity === void 0 && { severity: DiagnosticSeverity.Warning }),
severity: normalizeSeverityFromJsonRuleset(rule.severity),
...((rule as IRule).recommended === false && { severity: -1 }),
...((rule as IRule).recommended === void 0 && { recommended: true }),
then: expect.any(Object),
2 changes: 2 additions & 0 deletions src/rulesets/oas/__tests__/oas2-schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalizeSeverityFromJsonRuleset } from '../../../../setupTests';
import { functions } from '../../../functions';
import { RuleType, Spectral } from '../../../spectral';
import { setFunctionContext } from '../../evaluators';
@@ -15,6 +16,7 @@ describe('oas2-schema', () => {
s.setRules({
'oas2-schema': Object.assign({}, ruleset.rules['oas2-schema'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(ruleset.rules['oas2-schema'].severity),
type: RuleType[ruleset.rules['oas2-schema'].type],
then: {
...ruleset.rules['oas2-schema'].then,
2 changes: 2 additions & 0 deletions src/rulesets/oas/__tests__/oas2-valid-definition-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../setupTests';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';

@@ -8,6 +9,7 @@ describe('oas2-valid-definition-example', () => {
s.setRules({
'oas2-valid-definition-example': Object.assign(ruleset.rules['oas2-valid-definition-example'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(ruleset.rules['oas2-valid-definition-example'].severity),
type: RuleType[ruleset.rules['oas2-valid-definition-example'].type],
}),
});
2 changes: 2 additions & 0 deletions src/rulesets/oas/__tests__/oas2-valid-parameter-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../setupTests';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';

@@ -8,6 +9,7 @@ describe('oas2-valid-parameter-example', () => {
s.setRules({
'oas2-valid-parameter-example': Object.assign(ruleset.rules['oas2-valid-parameter-example'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(ruleset.rules['oas2-valid-parameter-example'].severity),
type: RuleType[ruleset.rules['oas2-valid-parameter-example'].type],
}),
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../setupTests';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';

@@ -8,6 +9,7 @@ describe('oas3-valid-oas-parameter-example', () => {
s.setRules({
'oas3-valid-oas-parameter-example': Object.assign(ruleset.rules['oas3-valid-oas-parameter-example'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(ruleset.rules['oas3-valid-oas-parameter-example'].severity),
type: RuleType[ruleset.rules['oas3-valid-oas-parameter-example'].type],
}),
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../setupTests';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';

@@ -8,6 +9,7 @@ describe('oas3-valid-parameter-schema-example', () => {
s.setRules({
'oas3-valid-parameter-schema-example': Object.assign(ruleset.rules['oas3-valid-parameter-schema-example'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(ruleset.rules['oas3-valid-parameter-schema-example'].severity),
type: RuleType[ruleset.rules['oas3-valid-parameter-schema-example'].type],
}),
});
2 changes: 2 additions & 0 deletions src/rulesets/oas/__tests__/oas3-valid-schema-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../setupTests';
import { RuleType, Spectral } from '../../../spectral';
import * as ruleset from '../index.json';

@@ -8,6 +9,7 @@ describe('oas3-valid-schema-example', () => {
s.setRules({
'oas3-valid-schema-example': Object.assign(ruleset.rules['oas3-valid-schema-example'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(ruleset.rules['oas3-valid-schema-example'].severity),
type: RuleType[ruleset.rules['oas3-valid-schema-example']!.type],
}),
});
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import { rules } from '../../index.json';
import oasDocumentSchema, { prepareResults } from '../oasDocumentSchema';

import { ErrorObject } from 'ajv';
import { normalizeSeverityFromJsonRuleset } from '../../../../../setupTests';
import * as oas2Schema from '../../schemas/schema.oas2.json';
import * as oas3Schema from '../../schemas/schema.oas3.json';

@@ -21,6 +22,7 @@ describe('oasDocumentSchema', () => {
s.setRules({
'oas2-schema': {
...rules['oas2-schema'],
severity: normalizeSeverityFromJsonRuleset(rules['oas2-schema'].severity),
type: RuleType[rules['oas2-schema'].type],
then: {
...rules['oas2-schema'].then,
@@ -32,6 +34,7 @@ describe('oasDocumentSchema', () => {
},
'oas3-schema': {
...rules['oas3-schema'],
severity: normalizeSeverityFromJsonRuleset(rules['oas3-schema'].severity),
type: RuleType[rules['oas3-schema'].type],
then: {
...rules['oas3-schema'].then,
2 changes: 2 additions & 0 deletions src/rulesets/oas/functions/__tests__/oasOpIdUnique.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RuleType, Spectral } from '../../../../index';

import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../../setupTests';
import { rules } from '../../index.json';
import oasOpIdUnique from '../oasOpIdUnique';

@@ -11,6 +12,7 @@ describe('oasOpIdUnique', () => {
s.setRules({
'operation-operationId-unique': Object.assign(rules['operation-operationId-unique'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(rules['operation-operationId-unique'].severity),
type: RuleType[rules['operation-operationId-unique'].type],
}),
});
2 changes: 2 additions & 0 deletions src/rulesets/oas/functions/__tests__/oasPathParam.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../../setupTests';
import { RuleType, Spectral } from '../../../../index';
import { rules } from '../../index.json';
import oasPathParam from '../oasPathParam';
@@ -9,6 +10,7 @@ describe('oasPathParam', () => {
s.setRules({
'path-params': Object.assign(rules['path-params'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(rules['path-params'].severity),
type: RuleType[rules['path-params'].type],
}),
});
2 changes: 2 additions & 0 deletions src/rulesets/oas/functions/__tests__/refSiblings.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiagnosticSeverity } from '@stoplight/types';
import { normalizeSeverityFromJsonRuleset } from '../../../../../setupTests';
import { Document } from '../../../../document';
import { RuleType, Spectral } from '../../../../index';
import * as Parsers from '../../../../parsers';
@@ -11,6 +12,7 @@ describe('refSiblings', () => {
s.setRules({
'no-$ref-siblings': Object.assign(oasRules['no-$ref-siblings'], {
recommended: true,
severity: normalizeSeverityFromJsonRuleset(oasRules['no-$ref-siblings'].severity),
type: RuleType[oasRules['no-$ref-siblings'].type],
}),
});
28 changes: 14 additions & 14 deletions src/rulesets/oas/index.json
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@
"description": "Every operation must have a unique `operationId`.",
"recommended": true,
"type": "validation",
"severity": 0,
"severity": "error",
"given": "$",
"then": {
"function": "oasOpIdUnique"
@@ -65,7 +65,7 @@
"description": "Path parameters should be defined and valid.",
"message": "{{error}}",
"type": "validation",
"severity": 0,
"severity": "error",
"recommended": true,
"given": "$",
"then": {
@@ -324,7 +324,7 @@
"description": "Property cannot be placed among $ref",
"message": "{{error}}",
"type": "validation",
"severity": 0,
"severity": "error",
"recommended": true,
"resolved": false,
"given": "$",
@@ -432,7 +432,7 @@
"message": "{{error}}",
"recommended": true,
"formats": ["oas2"],
"severity": 0,
"severity": "error",
"type": "validation",
"given": "$..parameters..[?(@.in == 'body')]..[?(@property !== 'properties' && @.example && ( @.type || @.format || @.$ref ))]",
"then": {
@@ -449,7 +449,7 @@
"message": "{{error}}",
"recommended": true,
"formats": ["oas2"],
"severity": 0,
"severity": "error",
"type": "validation",
"given": "$..definitions..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref))]",
"then": {
@@ -490,7 +490,7 @@
"message": "{{error}}.",
"recommended": true,
"formats": ["oas2"],
"severity": 0,
"severity": "error",
"type": "validation",
"given": "$",
"then": {
@@ -618,7 +618,7 @@
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"recommended": true,
"severity": 0,
"severity": "error",
"type": "validation",
"given": "$..parameters..[?(@.example && @.schema)]",
"then": {
@@ -634,7 +634,7 @@
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"recommended": true,
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"type": "validation",
"given": "$..headers..[?(@.example && @.schema)]",
@@ -651,7 +651,7 @@
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"recommended": true,
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"type": "validation",
"given": "$..content..[?(@.example && @.schema)]",
@@ -668,7 +668,7 @@
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"recommended": true,
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"type": "validation",
"given": "$..parameters..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref))]",
@@ -685,7 +685,7 @@
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"recommended": true,
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"type": "validation",
"given": "$..headers..[?(@property !== 'properties' && @.example && (@.type || @.format || @.$ref))]",
@@ -701,7 +701,7 @@
"oas3-valid-schema-example": {
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"recommended": true,
"type": "validation",
@@ -718,7 +718,7 @@
"oas3-valid-content-schema-example": {
"description": "Examples must be valid against their defined schema.",
"message": "{{error}}",
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"recommended": true,
"type": "validation",
@@ -735,7 +735,7 @@
"oas3-schema": {
"description": "Validate structure of OpenAPI v3 specification.",
"message": "{{error}}.",
"severity": 0,
"severity": "error",
"formats": ["oas3"],
"recommended": true,
"type": "validation",