Skip to content

Commit

Permalink
fix(severity): default severity should be warning (#74)
Browse files Browse the repository at this point in the history
* fix(severitylabel): deafult severity label based on rule severity

* fix(tests): update tests to use default warning severity

* fix(ruleset): add rule type to path-keys-no-trailing-slash
  • Loading branch information
casserni authored and lottamus committed Dec 21, 2018
1 parent 1c6cf8a commit 11fa690
Show file tree
Hide file tree
Showing 41 changed files with 176 additions and 144 deletions.
69 changes: 33 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ Think of a set of **rules** and **functions** as a flexible and customizable sty
Spectral has a built-in set of functions which you can reference in your rules. This example uses the `RuleFunction.PATTERN` to create a rule that checks that all property values are in snake case.

```javascript
const { RuleFunction, Spectral } = require("@stoplight/spectral");
const { RuleFunction, Spectral } = require('@stoplight/spectral');

const spectral = new Spectral();

spectral.addRules({
snake_case: {
summary: "Checks for snake case pattern",
summary: 'Checks for snake case pattern',

// evaluate every property
given: "$..*",
given: '$..*',

then: {
function: RuleFunction.PATTERN,
functionOptions: {
match: "^[a-z]+[a-z0-9_]*[a-z0-9]+$"
}
}
}
match: '^[a-z]+[a-z0-9_]*[a-z0-9]+$',
},
},
},
});

const results = spectral.run({
name: "helloWorld"
name: 'helloWorld',
});

console.log(JSON.stringify(results, null, 4));
Expand All @@ -66,8 +66,8 @@ console.log(JSON.stringify(results, null, 4));
// {
// "name": "snake_case",
// "message": "must match the pattern '^[a-z]+[a-z0-9_]*[a-z0-9]+$'",
// "severity": 50,
// "severityLabel": "error",
// "severity": 40,
// "severityLabel": "warn",
// "path": [
// "name"
// ]
Expand All @@ -81,7 +81,7 @@ console.log(JSON.stringify(results, null, 4));
Sometimes the built-in functions don't cover your use case. This example creates a custom function, `customNotThatFunction`, and then uses it within a rule, `openapi_not_swagger`. The custom function checks that you are not using a specific string (e.g., "Swagger") and suggests what to use instead (e.g., "OpenAPI").

```javascript
const { Spectral } = require("@stoplight/spectral");
const { Spectral } = require('@stoplight/spectral');

// custom function
const customNotThatFunction = (targetValue, options) => {
Expand All @@ -91,40 +91,40 @@ const customNotThatFunction = (targetValue, options) => {
// return the single error
return [
{
message: `Use ${suggestion} instead of ${match}!`
}
message: `Use ${suggestion} instead of ${match}!`,
},
];
}
};

const spectral = new Spectral();

spectral.addFunctions({
notThat: customNotThatFunction
notThat: customNotThatFunction,
});

spectral.addRules({
openapi_not_swagger: {
summary: "Checks for use of Swagger, and suggests OpenAPI.",
summary: 'Checks for use of Swagger, and suggests OpenAPI.',

// check every property
given: "$..*",
given: '$..*',

then: {
// reference the function we added!
function: "notThat",
function: 'notThat',

// pass it the options it needs
functionOptions: {
match: "Swagger",
suggestion: "OpenAPI"
}
}
}
match: 'Swagger',
suggestion: 'OpenAPI',
},
},
},
});

const results = spectral.run({
description: "Swagger is pretty cool!"
description: 'Swagger is pretty cool!',
});

console.log(JSON.stringify(results, null, 4));
Expand All @@ -135,8 +135,8 @@ console.log(JSON.stringify(results, null, 4));
// {
// "name": "openapi_not_swagger",
// "message": "Use OpenAPI instead of Swagger!",
// "severity": 50,
// "severityLabel": "error",
// "severity": 40,
// "severityLabel": "warn",
// "path": [
// "description"
// ]
Expand All @@ -152,23 +152,20 @@ Spectral also includes a number of ready made rules and functions for OpenAPI Sp
You can also add to these rules to create a customized linting style guide for your OAS documents.

```javascript
const { Spectral } = require("@stoplight/spectral");
const {
oas2Functions,
oas2Rules
} = require("@stoplight/spectral/rulesets/oas2");
const { Spectral } = require('@stoplight/spectral');
const { oas2Functions, oas2Rules } = require('@stoplight/spectral/rulesets/oas2');

// an OASv2 document
var myOAS = {
// ... properties in your document
responses: {
"200": {
description: "",
'200': {
description: '',
schema: {
$ref: "#/definitions/error-response"
}
}
}
$ref: '#/definitions/error-response',
},
},
},
// ... properties in your document
};

Expand Down
33 changes: 31 additions & 2 deletions src/__tests__/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ describe('linter', () => {
expect(result.results[0]).toEqual({
name: 'rule1',
message,
severity: ValidationSeverity.Error,
severityLabel: ValidationSeverityLabel.Error,
severity: ValidationSeverity.Warn,
severityLabel: ValidationSeverityLabel.Warn,
path: ['responses', '404', 'description'],
});
});
Expand Down Expand Up @@ -124,6 +124,35 @@ describe('linter', () => {
expect(result.results[0].severityLabel).toEqual(ValidationSeverityLabel.Info);
});

test('should default severityLabel based on rule severity', () => {
spectral.addFunctions({
func1: () => {
return [
{
message: 'foo',
},
];
},
});

spectral.addRules({
rule1: {
given: '$.x',
severity: ValidationSeverity.Info,
then: {
function: 'func1',
},
},
});

const result = spectral.run({
x: true,
});

expect(result.results[0].severity).toEqual(ValidationSeverity.Info);
expect(result.results[0].severityLabel).toEqual(ValidationSeverityLabel.Info);
});

describe('functional tests for the given property', () => {
let fakeLintingFunction: any;

Expand Down
9 changes: 7 additions & 2 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ObjPath, ValidationSeverity, ValidationSeverityLabel } from '@stoplight
import * as jp from 'jsonpath';
const get = require('lodash/get');
const has = require('lodash/has');
const invert = require('lodash/invert');

import { IFunction, IGivenNode, IRuleResult, IRunOpts, IRunRule, IThen } from './types';

Expand Down Expand Up @@ -83,15 +84,19 @@ export const lintNode = (
}
) || [];

const severity = rule.severity || ValidationSeverity.Warn;
const severityLabel =
rule.severityLabel || (ValidationSeverityLabel[invert(ValidationSeverity)[severity]] as ValidationSeverityLabel);

results = results.concat(
targetResults.map(result => {
return {
name: rule.name,
summary: rule.summary,
message: result.message,
severity: rule.severity || ValidationSeverity.Error,
severityLabel: rule.severityLabel || ValidationSeverityLabel.Error,
path: result.path || targetPath,
severity,
severityLabel,
};
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Array [
"contact",
"name",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Contact object should have \`name\`, \`url\` and \`email\`.",
},
Object {
Expand All @@ -22,8 +22,8 @@ Array [
"contact",
"url",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Contact object should have \`name\`, \`url\` and \`email\`.",
},
Object {
Expand All @@ -34,8 +34,8 @@ Array [
"contact",
"email",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Contact object should have \`name\`, \`url\` and \`email\`.",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Array [
"path": Array [
"example",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Example should have either a \`value\` or \`externalValue\` field.",
},
]
Expand All @@ -23,8 +23,8 @@ Array [
"path": Array [
"example",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Example should have either a \`value\` or \`externalValue\` field.",
},
]
Expand Down
4 changes: 2 additions & 2 deletions src/rulesets/oas/__tests__/__snapshots__/info-contact.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Array [
"info",
"contact",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Info object should contain \`contact\` object.",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Array [
"info",
"description",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "OpenAPI object info \`description\` must be present and non-empty string.",
},
]
Expand Down
4 changes: 2 additions & 2 deletions src/rulesets/oas/__tests__/__snapshots__/info-license.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Array [
"info",
"license",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "OpenAPI object info \`license\` must be present and non-empty string.",
},
]
Expand Down
4 changes: 2 additions & 2 deletions src/rulesets/oas/__tests__/__snapshots__/license-url.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Array [
"license",
"url",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "License object should include \`url\`.",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Array [
"info",
"description",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Markdown descriptions should not contain \`eval(\`.",
},
Object {
Expand All @@ -20,8 +20,8 @@ Array [
"info",
"title",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Markdown descriptions should not contain \`eval(\`.",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Array [
"info",
"description",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "Markdown descriptions should not contain \`<script>\` tags.",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Array [
0,
"$ref",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "References should start with \`#/\`.",
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Array [
"path": Array [
"tags",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "OpenAPI object should have alphabetical \`tags\`.",
},
]
Expand Down
4 changes: 2 additions & 2 deletions src/rulesets/oas/__tests__/__snapshots__/openapi-tags.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Array [
"path": Array [
"tags",
],
"severity": 50,
"severityLabel": "error",
"severity": 40,
"severityLabel": "warn",
"summary": "OpenAPI object should have non-empty \`tags\` array.",
},
]
Expand Down
Loading

0 comments on commit 11fa690

Please sign in to comment.