-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-287247: IPA-109: Validate custom method must use colon (:) followed by the custom verb #328
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 3 commits
48b0633
a8ec29f
9a7fc40
0867e2d
297c81e
619c8c6
8c01429
edbcd5d
b57466a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-109-custom-method-must-use-camel-case', [ | ||
| { | ||
| name: 'valid methods', | ||
| document: { | ||
| paths: { | ||
| '/a/{exampleId}:methodName': {}, | ||
| '/a:methodName': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid methods with exception', | ||
| document: { | ||
| paths: { | ||
| '/b/{exampleId}:MethodName': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-109-custom-method-must-use-camel-case': {}, | ||
| }, | ||
| }, | ||
| '/b:MethodName': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-109-custom-method-must-use-camel-case': {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid methods', | ||
| document: { | ||
| paths: { | ||
| '/a/{exampleId}:MethodName': {}, | ||
| '/a:MethodName': {}, | ||
| '/a/{exampleId}:method_name': {}, | ||
| '/a:method_name': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-must-use-camel-case', | ||
| message: 'The custom method must use camelCase format. http://go/ipa/109', | ||
| path: ['paths', '/a/{exampleId}:MethodName'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-must-use-camel-case', | ||
| message: 'The custom method must use camelCase format. http://go/ipa/109', | ||
| path: ['paths', '/a:MethodName'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-must-use-camel-case', | ||
| message: 'The custom method must use camelCase format. http://go/ipa/109', | ||
| path: ['paths', '/a/{exampleId}:method_name'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-109-custom-method-must-use-camel-case', | ||
| message: 'The custom method must use camelCase format. http://go/ipa/109', | ||
| path: ['paths', '/a:method_name'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||||||||
| import { getCustomMethod, isCustomMethod } from './utils/resourceEvaluation.js'; | ||||||||||||
| import { hasException } from './utils/exceptions.js'; | ||||||||||||
| import { casing } from '@stoplight/spectral-functions'; | ||||||||||||
|
|
||||||||||||
| const RULE_NAME = 'xgen-IPA-109-custom-method-must-use-camel-case'; | ||||||||||||
| const ERROR_MESSAGE = 'The custom method must use camelCase format.'; | ||||||||||||
| const ERROR_RESULT = [{ message: ERROR_MESSAGE }]; | ||||||||||||
|
|
||||||||||||
| export default (input, opts, { path }) => { | ||||||||||||
| // Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath. | ||||||||||||
| let pathKey = path[1]; | ||||||||||||
|
|
||||||||||||
| if (!isCustomMethod(pathKey)) return; | ||||||||||||
|
|
||||||||||||
| if (hasException(input, RULE_NAME)) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| let methodName = getCustomMethod(pathKey); | ||||||||||||
yelizhenden-mdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| if (!methodName) { | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
yelizhenden-mdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
|
|
||||||||||||
| const isCamelCase = casing(methodName, { type: 'camel', disallowDigits: true }); | ||||||||||||
| if (isCamelCase !== undefined) { | ||||||||||||
|
||||||||||||
| return ERROR_RESULT; | ||||||||||||
| } | ||||||||||||
|
||||||||||||
| const isCamelCase = casing(methodName, { type: 'camel', disallowDigits: true }); | |
| if (isCamelCase !== undefined) { | |
| return ERROR_RESULT; | |
| } | |
| return casing(methodName, { type: 'camel', disallowDigits: true }); |
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.
If you want ot customize the error message, you can change the yaml here:
message: '{{error}} http://go/ipa/109' -> message: 'Custom error message here. http://go/ipa/109'
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.
It would return undefined if validated or something like {... use camel case} if not validated.
Also, we disregard returning ERROR_RESULT.
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.
If you want ot customize the error message, you can change the yaml here:
message: '{{error}} http://go/ipa/109' -> message: 'Custom error message here. http://go/ipa/109'
I can send a custom message which includes the problematic method name
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.
Ah I see, yeah it would be nice to have {methodName} must use camelCase, let's go with original approach and add the evaluated string to the message 👍
Uh oh!
There was an error while loading. Please reload this page.