-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-271988: IPA-102: Validate paths follow expected format #312
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ef072b
CLOUDP-271988: IPA-102: Validate paths follow expected format
yelizhenden-mdb 9a1e37c
fix
yelizhenden-mdb 81a2d23
fix
yelizhenden-mdb dafe953
prettier fix
yelizhenden-mdb 3eceba0
address the comments
yelizhenden-mdb 1c6cfa2
address the comments
yelizhenden-mdb 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 hidden or 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
*.yaml | ||
*.yml | ||
*.html | ||
!/tools/spectral/ipa/**/*.yaml |
137 changes: 137 additions & 0 deletions
137
tools/spectral/ipa/__tests__/eachPathAlternatesBetweenResourceNameAndPathParam.test.js
This file contains hidden or 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,137 @@ | ||
import testRule from './__helpers__/testRule'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
||
testRule('xgen-IPA-102-path-alternate-resource-name-path-param', [ | ||
{ | ||
name: 'valid paths - api/atlas/v2', | ||
document: { | ||
paths: { | ||
'/api/atlas/v2/resourceName': {}, | ||
'/api/atlas/v2/resourceName/{pathParam}': {}, | ||
'/api/atlas/v2/resourceName1/{pathParam}/resourceName2': {}, | ||
'/api/atlas/v2/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {}, | ||
'/api/atlas/v2/resourceName/{pathParam}:method': {}, | ||
'/api/atlas/v2/custom:method': {}, | ||
'/api/atlas/v2': {}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid paths - api/atlas/v2/unauth', | ||
document: { | ||
paths: { | ||
'/api/atlas/v2/unauth/resourceName': {}, | ||
'/api/atlas/v2/unauth/resourceName/{pathParam}': {}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam}/resourceName2': {}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {}, | ||
'/api/atlas/v2/unauth/resourceName/{pathParam}:method': {}, | ||
'/api/atlas/v2/unauth/custom:method': {}, | ||
'/api/atlas/v2/unauth': {}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid paths - api/atlas/v2', | ||
document: { | ||
paths: { | ||
'/api/atlas/v2/resourceName1/resourceName2': {}, | ||
'/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}': {}, | ||
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3': {}, | ||
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {}, | ||
'/api/atlas/v2/{pathParam}': {}, | ||
'/api/atlas/v2/{pathParam1}/{pathParam2}': {}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/resourceName1/resourceName2'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/{pathParam}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/{pathParam1}/{pathParam2}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'invalid paths - api/atlas/v2/unauth', | ||
document: { | ||
paths: { | ||
'/api/atlas/v2/unauth/resourceName1/resourceName2': {}, | ||
'/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}': {}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3': {}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {}, | ||
'/api/atlas/v2/unauth/{pathParam}': {}, | ||
'/api/atlas/v2/unauth/{pathParam1}/{pathParam2}': {}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/unauth/resourceName1/resourceName2'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/unauth/{pathParam}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-102-path-alternate-resource-name-path-param', | ||
message: 'API paths must alternate between resource name and path params. http://go/ipa/102', | ||
path: ['paths', '/api/atlas/v2/unauth/{pathParam1}/{pathParam2}'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
]); |
This file contains hidden or 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 hidden or 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,2 +1,3 @@ | ||
extends: | ||
- ./rulesets/IPA-102.yaml | ||
- ./rulesets/IPA-104.yaml |
This file contains hidden or 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-102: Resource Identifiers | ||
# http://go/ipa/102 | ||
|
||
functions: | ||
- eachPathAlternatesBetweenResourceNameAndPathParam | ||
|
||
rules: | ||
xgen-IPA-102-path-alternate-resource-name-path-param: | ||
description: 'Paths should alternate between resource names and path params. http://go/ipa/102' | ||
message: '{{error}} http://go/ipa/102' | ||
severity: warn | ||
given: '$.paths' | ||
then: | ||
field: '@key' | ||
function: 'eachPathAlternatesBetweenResourceNameAndPathParam' |
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
tools/spectral/ipa/rulesets/functions/eachPathAlternatesBetweenResourceNameAndPathParam.js
This file contains hidden or 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,35 @@ | ||||||
import { isPathParam } from './utils/pathUtils'; | ||||||
|
||||||
const ERROR_MESSAGE = 'API paths must alternate between resource name and path params.'; | ||||||
const ERROR_RESULT = [{ message: ERROR_MESSAGE }]; | ||||||
const AUTH_PREFIX = '/api/atlas/v2'; | ||||||
const UNAUTH_PREFIX = '/api/atlas/v2/unauth'; | ||||||
|
||||||
const getPrefix = (path) => { | ||||||
if (path.includes(UNAUTH_PREFIX)) return UNAUTH_PREFIX; | ||||||
if (path.includes(AUTH_PREFIX)) return AUTH_PREFIX; | ||||||
return null; | ||||||
}; | ||||||
|
||||||
const validatePathStructure = (elements) => { | ||||||
return elements.every((element, index) => { | ||||||
const isEvenIndex = index % 2 === 0; | ||||||
return isEvenIndex ? !isPathParam(element) : isPathParam(element); | ||||||
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.
Suggested change
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. When the index is odd, it will return false this way. Am I missing anything? |
||||||
}); | ||||||
}; | ||||||
|
||||||
export default (input) => { | ||||||
const prefix = getPrefix(input); | ||||||
if (!prefix) return; | ||||||
|
||||||
let suffixWithLeadingSlash = input.slice(prefix.length); | ||||||
if (suffixWithLeadingSlash.length === 0) { | ||||||
return; | ||||||
} | ||||||
|
||||||
let suffix = suffixWithLeadingSlash.slice(1); | ||||||
let elements = suffix.split('/'); | ||||||
if (!validatePathStructure(elements)) { | ||||||
return ERROR_RESULT; | ||||||
} | ||||||
}; |
This file contains hidden or 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,14 @@ | ||
/** | ||
* Checks if a string belongs to a path parameter or a path parameter with a custom method. | ||
* | ||
* A path parameter has the format: `{paramName}` | ||
* A path parameter with a custom method has the format: `{paramName}:customMethod` | ||
* | ||
* @param {string} str - A string extracted from a path split by slashes. | ||
* @returns {boolean} True if the string matches the expected formats, false otherwise. | ||
*/ | ||
export function isPathParam(str) { | ||
yelizhenden-mdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const pathParamRegEx = new RegExp(`^{[a-z][a-zA-Z0-9]*}$`); | ||
const pathParamWithCustomMethodRegEx = new RegExp(`^{[a-z][a-zA-Z0-9]*}:[a-z][a-zA-Z0-9]*$`); | ||
return pathParamRegEx.test(str) || pathParamWithCustomMethodRegEx.test(str); | ||
} |
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.
❤️