Skip to content

Commit

Permalink
feat: new rule to restrict media types in paths (#31)
Browse files Browse the repository at this point in the history
* 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 <67381+philsturgeon@users.noreply.github.com>

* tests-added-oas2-tests

Added tests for OAS2 api definitions and removed old test

Co-authored-by: Phil Sturgeon <67381+philsturgeon@users.noreply.github.com>
  • Loading branch information
savage-alex and philsturgeon committed Sep 23, 2022
1 parent 3e729a4 commit b4ff918
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
75 changes: 75 additions & 0 deletions __tests__/no-file-extensions-in-paths-oas2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/helper';

testRule('no-file-extensions-in-paths', [
{
name: 'valid case',
document: {
swagger: "2.0",
info: { version: '1.0' },
paths: { 'resources': {} }
},
errors: [],
},

{
name: 'an API definition that is returning a json file',
document: {
swagger: "2.0",
info: { version: '1.0' },
paths: { 'resources.json': {} }
},
errors: [
{
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.',
path: ["paths", "resources.json"],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'an API definition that is returning a xml file',
document: {
swagger: "2.0",
info: { version: '1.0' },
paths: { 'resources.xml': {} }
},
errors: [
{
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.',
path: ["paths", "resources.xml"],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'an API definition that is returning a html file',
document: {
swagger: "2.0",
info: { version: '1.0' },
paths: { 'resources.html': {} }
},
errors: [
{
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.',
path: ["paths", "resources.html"],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'an API definition that is returning a txt file',
document: {
swagger: "2.0",
info: { version: '1.0' },
paths: { 'resources.txt': {} }
},
errors: [
{
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.',
path: ["paths", "resources.txt"],
severity: DiagnosticSeverity.Error,
},
],
}
]);
75 changes: 75 additions & 0 deletions __tests__/no-file-extensions-in-paths-oas3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DiagnosticSeverity } from '@stoplight/types';
import testRule from './__helpers__/helper';

testRule('no-file-extensions-in-paths', [
{
name: 'valid case',
document: {
openapi: '3.1.0',
info: { version: '1.0' },
paths: { 'resources': {} }
},
errors: [],
},

{
name: 'an API definition that is returning a json file',
document: {
openapi: '3.1.0',
info: { version: '1.0' },
paths: { 'resources.json': {} }
},
errors: [
{
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.',
path: ["paths", "resources.json"],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'an API definition that is returning a xml file',
document: {
openapi: '3.1.0',
info: { version: '1.0' },
paths: { 'resources.xml': {} }
},
errors: [
{
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.',
path: ["paths", "resources.xml"],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'an API definition that is returning a html file',
document: {
openapi: '3.1.0',
info: { version: '1.0' },
paths: { 'resources.html': {} }
},
errors: [
{
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.',
path: ["paths", "resources.html"],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'an API definition that is returning a txt file',
document: {
openapi: '3.1.0',
info: { version: '1.0' },
paths: { 'resources.txt': {} }
},
errors: [
{
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.',
path: ["paths", "resources.txt"],
severity: DiagnosticSeverity.Error,
},
],
}
]);
15 changes: 15 additions & 0 deletions src/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,21 @@ export default {
severity: DiagnosticSeverity.Warning,
},

// Author: Advanced API & Integrations Team (https://www.oneadvanced.com/)
"no-file-extensions-in-paths": {
description: "Paths must not include file extensions such as .json, .xml, .html and .txt",
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.",
given: "$.paths[*]~",
then: {
function: pattern,
functionOptions: {
notMatch: "\.(json|xml|html|txt)$",
},
},
severity: DiagnosticSeverity.Error,
},

// Author: Advanced API & Integrations Team (https://www.oneadvanced.com/)
"adv-security-schemes-defined": {
description: "All APIs MUST have a security scheme defined.",
Expand Down

0 comments on commit b4ff918

Please sign in to comment.