forked from 1-Platform/api-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(1-Platform#12,1-Platform#3): changed to uniform plugin loading a…
…nd added more checks
- Loading branch information
Showing
11 changed files
with
288 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
rules: | ||
- name: "status_code_check" | ||
status_code_check: | ||
file: "status_code_check.js" | ||
- name: "body_in_get_req" | ||
body_in_get_req: | ||
file: "body_in_get_req.js" | ||
- name: "case_checker" | ||
file: "case_checker.js" | ||
url_case_checker: | ||
file: "url_case_checker.js" | ||
unsafe_url_character_check: | ||
file: "unsafe_url_character_check.js" | ||
url_length: | ||
file: "url_length.js" | ||
req_body_case_checker: | ||
file: "req_body_case_checker.js" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const snakeCaseRegex = /^[a-z0-9]+(?:_[a-z0-9]+)*$/; | ||
const camelCaseRegex = /^[a-z]+(?:[A-Z0-9]+[a-z0-9]+[A-Za-z0-9]*)*$/; | ||
const pascalCaseRegex = /^(?:[A-Z][a-z0-9]+)(?:[A-Z]+[a-z0-9]*)*$/; | ||
const kebabCaseRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; | ||
|
||
function isCamelCase(word) { | ||
return camelCaseRegex.test(word); | ||
} | ||
|
||
function isPascalCase(word) { | ||
return pascalCaseRegex.test(word); | ||
} | ||
|
||
function isSnakeCase(word) { | ||
return snakeCaseRegex.test(word); | ||
} | ||
|
||
function isKebabCase(word) { | ||
return kebabCaseRegex.test(word); | ||
} | ||
|
||
function getCaseCheckerFn(type) { | ||
switch (type) { | ||
case "camelcase": | ||
return isCamelCase; | ||
case "snakecase": | ||
return isSnakeCase; | ||
case "pascalcase": | ||
return isPascalCase; | ||
case "kebabcase": | ||
return isKebabCase; | ||
default: | ||
return isCamelCase; | ||
} | ||
} | ||
|
||
export default function (config, options = {}) { | ||
let numberOfResponses = 0; | ||
let numbnerOfFalseResponses = 0; | ||
const checkerFn = getCaseCheckerFn(options?.casing); | ||
|
||
Object.keys(config.schema.paths).forEach((path) => { | ||
Object.keys(config.schema.paths[path]).forEach((method) => { | ||
(config.schema.paths[path][method].parameters || []).forEach((param) => { | ||
numberOfResponses++; | ||
if (!checkerFn(param.name)) { | ||
numbnerOfFalseResponses++; | ||
config.report({ | ||
message: `Invalid casing for ${param.name} of ${param.in}`, | ||
path: path, | ||
method: method, | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
Object.keys(config.schema.components.schemas).forEach((schema) => { | ||
Object.keys(config.schema.components.schemas[schema].properties).forEach( | ||
(property) => { | ||
numberOfResponses++; | ||
if (!checkerFn(property)) { | ||
numbnerOfFalseResponses++; | ||
config.report({ | ||
message: `Invalid casing for ${property} of schema ${schema}`, | ||
path: "Nil", | ||
method: "Nil", | ||
}); | ||
} | ||
} | ||
); | ||
}); | ||
|
||
// if number goes to negative | ||
const score = | ||
(Math.max(numberOfResponses - numbnerOfFalseResponses, 0) / | ||
numberOfResponses) * | ||
100; | ||
config.setScore("quality", score); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// By this spec: https://perishablepress.com/stop-using-unsafe-characters-in-urls/ | ||
const unsafeURLRegex = /^[a-zA-Z0-9{}\/~_-]*$/; | ||
|
||
export default function (config) { | ||
let numberOfResponses = 0; | ||
let numbnerOfFalseResponses = 0; | ||
|
||
Object.keys(config.schema.paths).forEach((path) => { | ||
numberOfResponses++; | ||
if (!unsafeURLRegex.test(path)) { | ||
numbnerOfFalseResponses++; | ||
|
||
// get all methods | ||
const methods = Object.keys(config.schema.paths[path]) | ||
.join(", ") | ||
.toUpperCase(); | ||
config.report({ | ||
message: `URL contains unsafe character`, | ||
path: path, | ||
method: methods, | ||
}); | ||
} | ||
}); | ||
|
||
const score = | ||
(Math.max(numberOfResponses - numbnerOfFalseResponses, 0) / | ||
numberOfResponses) * | ||
100; | ||
|
||
config.setScore("quality", score); | ||
} |
Oops, something went wrong.