Skip to content

Commit b4ff918

Browse files
feat: new rule to restrict media types in paths (#31)
* feat: add rule for no media types in resources New rule to address: #24 * Added more media types to description * Added more tests * feat: add rule for no media types in resources New rule to address: #24 * Added more media types to description * Added more tests * Update src/ruleset.ts * Update src/ruleset.ts reduce code duplication on regex Co-authored-by: Phil Sturgeon <[email protected]> * tests-added-oas2-tests Added tests for OAS2 api definitions and removed old test Co-authored-by: Phil Sturgeon <[email protected]>
1 parent 3e729a4 commit b4ff918

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import testRule from './__helpers__/helper';
3+
4+
testRule('no-file-extensions-in-paths', [
5+
{
6+
name: 'valid case',
7+
document: {
8+
swagger: "2.0",
9+
info: { version: '1.0' },
10+
paths: { 'resources': {} }
11+
},
12+
errors: [],
13+
},
14+
15+
{
16+
name: 'an API definition that is returning a json file',
17+
document: {
18+
swagger: "2.0",
19+
info: { version: '1.0' },
20+
paths: { 'resources.json': {} }
21+
},
22+
errors: [
23+
{
24+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
25+
path: ["paths", "resources.json"],
26+
severity: DiagnosticSeverity.Error,
27+
},
28+
],
29+
},
30+
{
31+
name: 'an API definition that is returning a xml file',
32+
document: {
33+
swagger: "2.0",
34+
info: { version: '1.0' },
35+
paths: { 'resources.xml': {} }
36+
},
37+
errors: [
38+
{
39+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
40+
path: ["paths", "resources.xml"],
41+
severity: DiagnosticSeverity.Error,
42+
},
43+
],
44+
},
45+
{
46+
name: 'an API definition that is returning a html file',
47+
document: {
48+
swagger: "2.0",
49+
info: { version: '1.0' },
50+
paths: { 'resources.html': {} }
51+
},
52+
errors: [
53+
{
54+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
55+
path: ["paths", "resources.html"],
56+
severity: DiagnosticSeverity.Error,
57+
},
58+
],
59+
},
60+
{
61+
name: 'an API definition that is returning a txt file',
62+
document: {
63+
swagger: "2.0",
64+
info: { version: '1.0' },
65+
paths: { 'resources.txt': {} }
66+
},
67+
errors: [
68+
{
69+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
70+
path: ["paths", "resources.txt"],
71+
severity: DiagnosticSeverity.Error,
72+
},
73+
],
74+
}
75+
]);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { DiagnosticSeverity } from '@stoplight/types';
2+
import testRule from './__helpers__/helper';
3+
4+
testRule('no-file-extensions-in-paths', [
5+
{
6+
name: 'valid case',
7+
document: {
8+
openapi: '3.1.0',
9+
info: { version: '1.0' },
10+
paths: { 'resources': {} }
11+
},
12+
errors: [],
13+
},
14+
15+
{
16+
name: 'an API definition that is returning a json file',
17+
document: {
18+
openapi: '3.1.0',
19+
info: { version: '1.0' },
20+
paths: { 'resources.json': {} }
21+
},
22+
errors: [
23+
{
24+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
25+
path: ["paths", "resources.json"],
26+
severity: DiagnosticSeverity.Error,
27+
},
28+
],
29+
},
30+
{
31+
name: 'an API definition that is returning a xml file',
32+
document: {
33+
openapi: '3.1.0',
34+
info: { version: '1.0' },
35+
paths: { 'resources.xml': {} }
36+
},
37+
errors: [
38+
{
39+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
40+
path: ["paths", "resources.xml"],
41+
severity: DiagnosticSeverity.Error,
42+
},
43+
],
44+
},
45+
{
46+
name: 'an API definition that is returning a html file',
47+
document: {
48+
openapi: '3.1.0',
49+
info: { version: '1.0' },
50+
paths: { 'resources.html': {} }
51+
},
52+
errors: [
53+
{
54+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
55+
path: ["paths", "resources.html"],
56+
severity: DiagnosticSeverity.Error,
57+
},
58+
],
59+
},
60+
{
61+
name: 'an API definition that is returning a txt file',
62+
document: {
63+
openapi: '3.1.0',
64+
info: { version: '1.0' },
65+
paths: { 'resources.txt': {} }
66+
},
67+
errors: [
68+
{
69+
message: 'Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.',
70+
path: ["paths", "resources.txt"],
71+
severity: DiagnosticSeverity.Error,
72+
},
73+
],
74+
}
75+
]);

src/ruleset.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ export default {
279279
severity: DiagnosticSeverity.Warning,
280280
},
281281

282+
// Author: Advanced API & Integrations Team (https://www.oneadvanced.com/)
283+
"no-file-extensions-in-paths": {
284+
description: "Paths must not include file extensions such as .json, .xml, .html and .txt",
285+
message:
286+
"Paths must not include file extensions such as .json, .xml, .html and .txt. Use the OpenAPI `content` keyword to tell consumers which Media Types are available.",
287+
given: "$.paths[*]~",
288+
then: {
289+
function: pattern,
290+
functionOptions: {
291+
notMatch: "\.(json|xml|html|txt)$",
292+
},
293+
},
294+
severity: DiagnosticSeverity.Error,
295+
},
296+
282297
// Author: Advanced API & Integrations Team (https://www.oneadvanced.com/)
283298
"adv-security-schemes-defined": {
284299
description: "All APIs MUST have a security scheme defined.",

0 commit comments

Comments
 (0)