-
Notifications
You must be signed in to change notification settings - Fork 7
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
CLOUDP-283086: POC validator with custom function #287
Draft
lovisaberggren
wants to merge
7
commits into
main
Choose a base branch
from
CLOUDP-283086
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ccd8b22
CLOUDP-283086: POC validator with custom function
lovisaberggren 635851c
CLOUDP-283086: Comment
lovisaberggren 9540993
CLOUDP-283086: Restructure and identify resource
lovisaberggren 85da0b0
CLOUDP-283086: newline
lovisaberggren 9f422ae
Merge branch 'main' into CLOUDP-283086
lovisaberggren 24bf86c
CLOUDP-283086: Use package.json remove Makefile
lovisaberggren ed93697
CLOUDP-283086: Restructuring and experiment with exceptions
lovisaberggren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
extends: | ||
- ./rulesets/IPA-104.yaml |
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,15 @@ | ||
# IPA-104: Get | ||
# http://go/ipa/104 | ||
|
||
functions: | ||
- eachResourceHasGetMethod | ||
|
||
rules: | ||
xgen-IPA-104-resource-has-GET: | ||
description: "APIs must provide a get method for resources. http://go/ipa/104" | ||
message: "{{error}} http://go/ipa/117" | ||
severity: error | ||
given: "$.paths" | ||
then: | ||
field: "@key" | ||
function: "eachResourceHasGetMethod" |
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,47 @@ | ||
import { hasExemption } from './utils/exemptions.js'; | ||
import { | ||
hasGetMethod, | ||
isChild, | ||
isCustomMethod, | ||
isStandardResource, | ||
isSingletonResource, | ||
getResourcePaths, | ||
} from './utils/resourceEvaluation.js'; | ||
|
||
const RULE_NAME = 'xgen-IPA-104-resource-has-GET'; | ||
const ERROR_MESSAGE = 'APIs must provide a get method for resources.'; | ||
|
||
export default (input, _, context) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we document what version of ES can we use? |
||
if (isChild(input) || isCustomMethod(input)) { | ||
return; | ||
} | ||
|
||
const oas = context.documentInventory.resolved; | ||
const resourceObject = oas.paths[input]; | ||
|
||
if (hasExemption(RULE_NAME, resourceObject)) { | ||
return; | ||
} | ||
|
||
const resourcePaths = getResourcePaths(input, Object.keys(oas.paths)); | ||
|
||
if (isSingletonResource(resourcePaths)) { | ||
// Singleton resource, may have custom methods | ||
if (!hasGetMethod(oas.paths[resourcePaths[0]])) { | ||
return [ | ||
{ | ||
message: ERROR_MESSAGE, | ||
}, | ||
]; | ||
} | ||
} else if (isStandardResource(resourcePaths)) { | ||
// Normal resource, may have custom methods | ||
if (!hasGetMethod(oas.paths[resourcePaths[1]])) { | ||
return [ | ||
{ | ||
message: ERROR_MESSAGE, | ||
}, | ||
]; | ||
} | ||
} | ||
}; |
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,15 @@ | ||
const EXEMPTION_EXTENSION = 'x-xgen-IPA-exception'; | ||
|
||
/** | ||
* Checks if the object has an exemption set for the passed rule name by checking | ||
* if the object has a field "x-xgen-IPA-exception" containing the rule as a | ||
* field. | ||
* | ||
* @param ruleName the name of the exemption | ||
* @param object the object to evaluate | ||
* @returns {boolean} | ||
*/ | ||
export function hasExemption(ruleName, object) { | ||
const exemptions = object[EXEMPTION_EXTENSION]; | ||
return exemptions !== undefined && Object.keys(exemptions).includes(ruleName); | ||
} |
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,63 @@ | ||
export function isChild(path) { | ||
return path.endsWith('}'); | ||
} | ||
|
||
export function isCustomMethod(path) { | ||
return path.includes(':'); | ||
} | ||
|
||
/** | ||
* Checks if a resource is a singleton resource based on the paths for the | ||
* resource. The resource may have custom methods. | ||
* | ||
* @param resourcePaths all paths for the resource as an array of strings | ||
* @returns {boolean} | ||
*/ | ||
export function isSingletonResource(resourcePaths) { | ||
if (resourcePaths.length === 1) { | ||
return true; | ||
} | ||
const additionalPaths = resourcePaths.slice(1); | ||
return !additionalPaths.some((p) => !isCustomMethod(p)); | ||
} | ||
|
||
/** | ||
* Checks if a resource is a standard resource based on the paths for the | ||
* resource. The resource may have custom methods. | ||
* | ||
* @param resourcePaths all paths for the resource as an array of strings | ||
* @returns {boolean} | ||
*/ | ||
export function isStandardResource(resourcePaths) { | ||
if (resourcePaths.length === 2 && isChild(resourcePaths[1])) { | ||
return true; | ||
} | ||
if (resourcePaths.length < 3 || !isChild(resourcePaths[1])) { | ||
return false; | ||
} | ||
const additionalPaths = resourcePaths.slice(2); | ||
return !additionalPaths.some((p) => !isCustomMethod(p)); | ||
} | ||
|
||
/** | ||
* Checks if a path object has a GET method | ||
* | ||
* @param pathObject the path object to evaluate | ||
* @returns {boolean} | ||
*/ | ||
export function hasGetMethod(pathObject) { | ||
return Object.keys(pathObject).some((o) => o === 'get'); | ||
} | ||
|
||
/** | ||
* Get all paths for a resource based on the parent path | ||
* | ||
* @param parent the parent path string | ||
* @param allPaths all paths as an array of strings | ||
* @returns {*} a string array of all paths for a resource, including the parent | ||
*/ | ||
export function getResourcePaths(parent, allPaths) { | ||
const childPathPattern = new RegExp(`^${parent}/{[a-zA-Z]+}$`); | ||
const customMethodPattern = new RegExp(`^${parent}/{[a-zA-Z]+}:+[a-zA-Z]+$`); | ||
return allPaths.filter((p) => parent === p || childPathPattern.test(p) || customMethodPattern.test(p)); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning :P