From 000b09d4da5ba8622a83ebb93c2129c0ab80f48a Mon Sep 17 00:00:00 2001 From: nulltoken Date: Fri, 17 Apr 2020 17:18:42 +0200 Subject: [PATCH] chore(ruleset): use human friendly severity levels --- setupTests.ts | 11 ++++++++ src/rulesets/__tests__/reader.jest.test.ts | 7 +++-- src/rulesets/oas/__tests__/oas2-schema.ts | 2 ++ .../oas2-valid-definition-example.ts | 2 ++ .../__tests__/oas2-valid-parameter-example.ts | 2 ++ .../oas3-valid-oas-parameter-example.ts | 2 ++ .../oas3-valid-parameter-schema-example.ts | 2 ++ .../__tests__/oas3-valid-schema-example.ts | 2 ++ .../__tests__/oasDocumentSchema.test.ts | 3 ++ .../functions/__tests__/oasOpIdUnique.test.ts | 2 ++ .../functions/__tests__/oasPathParam.test.ts | 2 ++ .../functions/__tests__/refSiblings.test.ts | 2 ++ src/rulesets/oas/index.json | 28 +++++++++---------- 13 files changed, 50 insertions(+), 17 deletions(-) diff --git a/setupTests.ts b/setupTests.ts index c7c271db0..49f51b17b 100644 --- a/setupTests.ts +++ b/setupTests.ts @@ -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 => { + if (severity === void 0) { + return DiagnosticSeverity.Warning; + } + + return getDiagnosticSeverity(severity as HumanReadableDiagnosticSeverity | DiagnosticSeverity); +}; diff --git a/src/rulesets/__tests__/reader.jest.test.ts b/src/rulesets/__tests__/reader.jest.test.ts index 47d303097..8cd04d64d 100644 --- a/src/rulesets/__tests__/reader.jest.test.ts +++ b/src/rulesets/__tests__/reader.jest.test.ts @@ -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), diff --git a/src/rulesets/oas/__tests__/oas2-schema.ts b/src/rulesets/oas/__tests__/oas2-schema.ts index de3284c48..c2a188480 100644 --- a/src/rulesets/oas/__tests__/oas2-schema.ts +++ b/src/rulesets/oas/__tests__/oas2-schema.ts @@ -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, diff --git a/src/rulesets/oas/__tests__/oas2-valid-definition-example.ts b/src/rulesets/oas/__tests__/oas2-valid-definition-example.ts index e1e44c1b1..4a1554c7c 100644 --- a/src/rulesets/oas/__tests__/oas2-valid-definition-example.ts +++ b/src/rulesets/oas/__tests__/oas2-valid-definition-example.ts @@ -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], }), }); diff --git a/src/rulesets/oas/__tests__/oas2-valid-parameter-example.ts b/src/rulesets/oas/__tests__/oas2-valid-parameter-example.ts index cd7625810..f5e0772a3 100644 --- a/src/rulesets/oas/__tests__/oas2-valid-parameter-example.ts +++ b/src/rulesets/oas/__tests__/oas2-valid-parameter-example.ts @@ -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], }), }); diff --git a/src/rulesets/oas/__tests__/oas3-valid-oas-parameter-example.ts b/src/rulesets/oas/__tests__/oas3-valid-oas-parameter-example.ts index 00aebdb12..2faea4209 100644 --- a/src/rulesets/oas/__tests__/oas3-valid-oas-parameter-example.ts +++ b/src/rulesets/oas/__tests__/oas3-valid-oas-parameter-example.ts @@ -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], }), }); diff --git a/src/rulesets/oas/__tests__/oas3-valid-parameter-schema-example.ts b/src/rulesets/oas/__tests__/oas3-valid-parameter-schema-example.ts index 941ecdec2..2d08633db 100644 --- a/src/rulesets/oas/__tests__/oas3-valid-parameter-schema-example.ts +++ b/src/rulesets/oas/__tests__/oas3-valid-parameter-schema-example.ts @@ -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], }), }); diff --git a/src/rulesets/oas/__tests__/oas3-valid-schema-example.ts b/src/rulesets/oas/__tests__/oas3-valid-schema-example.ts index 95aef983a..5d5f454ac 100644 --- a/src/rulesets/oas/__tests__/oas3-valid-schema-example.ts +++ b/src/rulesets/oas/__tests__/oas3-valid-schema-example.ts @@ -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], }), }); diff --git a/src/rulesets/oas/functions/__tests__/oasDocumentSchema.test.ts b/src/rulesets/oas/functions/__tests__/oasDocumentSchema.test.ts index 1ff4ec2f9..991cce950 100644 --- a/src/rulesets/oas/functions/__tests__/oasDocumentSchema.test.ts +++ b/src/rulesets/oas/functions/__tests__/oasDocumentSchema.test.ts @@ -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, diff --git a/src/rulesets/oas/functions/__tests__/oasOpIdUnique.test.ts b/src/rulesets/oas/functions/__tests__/oasOpIdUnique.test.ts index 814f58784..c8cbe3c00 100644 --- a/src/rulesets/oas/functions/__tests__/oasOpIdUnique.test.ts +++ b/src/rulesets/oas/functions/__tests__/oasOpIdUnique.test.ts @@ -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], }), }); diff --git a/src/rulesets/oas/functions/__tests__/oasPathParam.test.ts b/src/rulesets/oas/functions/__tests__/oasPathParam.test.ts index 931f5f00f..624edc703 100644 --- a/src/rulesets/oas/functions/__tests__/oasPathParam.test.ts +++ b/src/rulesets/oas/functions/__tests__/oasPathParam.test.ts @@ -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], }), }); diff --git a/src/rulesets/oas/functions/__tests__/refSiblings.test.ts b/src/rulesets/oas/functions/__tests__/refSiblings.test.ts index d414b3a06..02136841d 100644 --- a/src/rulesets/oas/functions/__tests__/refSiblings.test.ts +++ b/src/rulesets/oas/functions/__tests__/refSiblings.test.ts @@ -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], }), }); diff --git a/src/rulesets/oas/index.json b/src/rulesets/oas/index.json index efd7727ab..f05e11fe2 100644 --- a/src/rulesets/oas/index.json +++ b/src/rulesets/oas/index.json @@ -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",