Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-no media types in paths #31

Merged
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