-
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
Changes from 4 commits
4ef072b
9a1e37c
81a2d23
dafe953
3eceba0
1c6cfa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
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': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/resourceName/{pathParam}': { | ||
get: {}, | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/resourceName1/{pathParam}/resourceName2': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': { | ||
get: {}, | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/resourceName/{pathParam}:method': { | ||
post: {}, | ||
}, | ||
'/api/atlas/v2/custom:method': { | ||
post: {}, | ||
}, | ||
'/api/atlas/v2': { | ||
post: {}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'valid paths - api/atlas/v2/unauth', | ||
document: { | ||
paths: { | ||
'/api/atlas/v2/unauth/resourceName': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName/{pathParam}': { | ||
get: {}, | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam}/resourceName2': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': { | ||
get: {}, | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName/{pathParam}:method': { | ||
post: {}, | ||
}, | ||
'/api/atlas/v2/unauth/custom:method': { | ||
post: {}, | ||
}, | ||
'/api/atlas/v2/unauth': { | ||
post: {}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid paths - api/atlas/v2', | ||
document: { | ||
paths: { | ||
'/api/atlas/v2/resourceName1/resourceName2': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}': { | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3': { | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': { | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/{pathParam}': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/{pathParam1}/{pathParam2}': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
}, | ||
}, | ||
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': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}': { | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3': { | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': { | ||
patch: {}, | ||
delete: {}, | ||
}, | ||
'/api/atlas/v2/unauth/{pathParam}': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
'/api/atlas/v2/unauth/{pathParam1}/{pathParam2}': { | ||
post: {}, | ||
get: {}, | ||
}, | ||
}, | ||
}, | ||
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, | ||
}, | ||
], | ||
}, | ||
]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
extends: | ||
- ./rulesets/IPA-102.yaml | ||
- ./rulesets/IPA-104.yaml |
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' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,10 +6,10 @@ functions: | |
|
||
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" | ||
description: 'APIs must provide a get method for resources. http://go/ipa/104' | ||
message: '{{error}} http://go/ipa/104' | ||
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. ❤️ |
||
severity: warn | ||
given: "$.paths" | ||
given: '$.paths' | ||
then: | ||
field: "@key" | ||
function: "eachResourceHasGetMethod" | ||
field: '@key' | ||
function: 'eachResourceHasGetMethod' | ||
yelizhenden-mdb marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
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? |
||||||
}); | ||||||
}; | ||||||
|
||||||
// eslint-disable-next-line no-unused-vars | ||||||
export default (input, _0, _1) => { | ||||||
yelizhenden-mdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
const prefix = getPrefix(input); | ||||||
if (!prefix) return []; | ||||||
|
||||||
let suffixWithLeadingSlash = input.slice(prefix.length); | ||||||
if (suffixWithLeadingSlash.length === 0) { | ||||||
return []; | ||||||
} | ||||||
yelizhenden-mdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
let suffix = suffixWithLeadingSlash.slice(1); | ||||||
let elements = suffix.split('/'); | ||||||
return validatePathStructure(elements) ? [] : ERROR_RESULT; | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
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); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.