Repository: stoplightio/spectral
Pull request #298 (2019-07-05 20:30:41)

fix: rule with human-readable severity should be considered as valid
**Checklist**

- [x] Tests added / updated
- [x] Docs added / updated
- [x] Check this off Breaking change?

**Does this PR introduce a breaking change?**

- [ ] Yes
- [x] No


Patch:
diff --git a/src/meta/rule.schema.json b/src/meta/rule.schema.json
index cb6af061..9eb2ec5d 100644
--- a/src/meta/rule.schema.json
+++ b/src/meta/rule.schema.json
@@ -25,6 +25,16 @@
         "T": {
             "type": "string"
         },
+        "DiagnosticSeverity": {
+            "enum": [
+                -1,
+                0,
+                1,
+                2,
+                3
+            ],
+            "type": "number"
+        },
         "HumanReadableSeverity": {
             "type": "string",
             "enum": [
@@ -34,6 +44,16 @@
                 "hint",
                 "off"
             ]
+        },
+        "Severity": {
+            "oneOf": [
+                {
+                    "$ref": "#/definitions/DiagnosticSeverity"
+                },
+                {
+                    "$ref": "#/definitions/HumanReadableSeverity"
+                }
+            ]
         }
     },
     "oneOf": [
@@ -49,13 +69,7 @@
             "type": "string"
         },
         "severity": {
-            "enum": [
-                0,
-                1,
-                2,
-                3
-            ],
-            "type": "number"
+            "$ref": "#/definitions/Severity"
         },
         "message": {
             "type": "string"
@@ -119,7 +133,7 @@
             "minItems": 1,
             "items": [
                 {
-                    "$ref": "#/definitions/HumanReadableSeverity"
+                    "$ref": "#/definitions/Severity"
                 },
                 {
                     "type": "object"


Test patch:
diff --git a/src/rulesets/__tests__/validation.test.ts b/src/rulesets/__tests__/validation.test.ts
index 51b39c7e..7bb1403a 100644
--- a/src/rulesets/__tests__/validation.test.ts
+++ b/src/rulesets/__tests__/validation.test.ts
@@ -2,6 +2,10 @@ import { assertValidRuleset } from '../validation';
 const invalidRuleset = require('./__fixtures__/invalid-ruleset.json');
 const validRuleset = require('./__fixtures__/valid-flat-ruleset.json');
 
+// @oclif/test packages requires @types/mocha, therefore we have 2 packages coming up with similar typings
+// TS is confused and prefers the mocha ones, so we need to instrument it to pick up the Jest ones
+declare var it: jest.It;
+
 describe('Ruleset Validation', () => {
   it('given invalid ruleset should throw', () => {
     expect(assertValidRuleset.bind(null, invalidRuleset)).toThrow();
@@ -11,11 +15,25 @@ describe('Ruleset Validation', () => {
     expect(assertValidRuleset.bind(null, validRuleset)).not.toThrow();
   });
 
-  it('recognizes severity flags', () => {
+  it.each(['error', 'warn', 'info', 'hint', 'off'])('recognizes human-readable %s severity', severity => {
+    expect(
+      assertValidRuleset.bind(null, {
+        rules: {
+          rule: severity,
+        },
+      }),
+    ).not.toThrow();
+
     expect(
       assertValidRuleset.bind(null, {
         rules: {
-          rule: 'off',
+          rule: {
+            given: '$.info',
+            then: {
+              function: 'truthy',
+            },
+            severity,
+          },
         },
       }),
     ).not.toThrow();
@@ -29,6 +47,14 @@ describe('Ruleset Validation', () => {
         },
       }),
     ).not.toThrow();
+
+    expect(
+      assertValidRuleset.bind(null, {
+        rules: {
+          rule: [1],
+        },
+      }),
+    ).not.toThrow();
   });
 
   it('recognizes invalid array-ish syntax', () => {