-
Notifications
You must be signed in to change notification settings - Fork 14
feat(ipa): new rule xgen-IPA-117-operation-summary-format #897
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
2 commits
Select commit
Hold shift + click to select a range
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
103 changes: 103 additions & 0 deletions
103
tools/spectral/ipa/__tests__/IPA117OperationSummaryFormat.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,103 @@ | ||
import testRule from './__helpers__/testRule'; | ||
import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
||
testRule('xgen-IPA-117-operation-summary-format', [ | ||
{ | ||
name: 'valid description', | ||
document: { | ||
paths: { | ||
'/resource/{id}': { | ||
get: { | ||
summary: 'Return One Resource by ID', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
{ | ||
name: 'invalid descriptions', | ||
document: { | ||
paths: { | ||
'/resource/{id}': { | ||
get: { | ||
summary: 'Return One Resource.', | ||
}, | ||
}, | ||
'/resource': { | ||
get: { | ||
summary: 'Return all resources', | ||
}, | ||
}, | ||
'/resource/{id}/child': { | ||
get: { | ||
summary: 'return all child resources.', | ||
}, | ||
}, | ||
'/resource/{id}/child/{id}': { | ||
get: { | ||
summary: 'Return **One** Child Resource', | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [ | ||
{ | ||
code: 'xgen-IPA-117-operation-summary-format', | ||
message: 'Operation summaries must be in Title Case, must not end with a period and must not use CommonMark.', | ||
path: ['paths', '/resource/{id}', 'get'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-117-operation-summary-format', | ||
message: 'Operation summaries must be in Title Case, must not end with a period and must not use CommonMark.', | ||
path: ['paths', '/resource', 'get'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-117-operation-summary-format', | ||
message: 'Operation summaries must be in Title Case, must not end with a period and must not use CommonMark.', | ||
path: ['paths', '/resource/{id}/child', 'get'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
{ | ||
code: 'xgen-IPA-117-operation-summary-format', | ||
message: 'Operation summaries must be in Title Case, must not end with a period and must not use CommonMark.', | ||
path: ['paths', '/resource/{id}/child/{id}', 'get'], | ||
severity: DiagnosticSeverity.Warning, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'invalid description with exceptions', | ||
document: { | ||
paths: { | ||
'/resource/{id}': { | ||
get: { | ||
summary: 'Return One Resource.', | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-117-operation-summary-format': 'reason', | ||
}, | ||
}, | ||
}, | ||
'/resource': { | ||
get: { | ||
summary: 'Return all resources', | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-117-operation-summary-format': 'reason', | ||
}, | ||
}, | ||
}, | ||
'/resource/{id}/child': { | ||
get: { | ||
summary: 'return all child resources.', | ||
'x-xgen-IPA-exception': { | ||
'xgen-IPA-117-operation-summary-format': 'reason', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
errors: [], | ||
}, | ||
]); |
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
26 changes: 26 additions & 0 deletions
26
tools/spectral/ipa/rulesets/functions/IPA117OperationSummaryFormat.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,26 @@ | ||
import { evaluateAndCollectAdoptionStatus, handleInternalError } from './utils/collectionUtils.js'; | ||
import { isTitleCase } from './utils/casing.js'; | ||
import { resolveObject } from './utils/componentUtils.js'; | ||
|
||
export default (input, { ignoreList, grammaticalWords }, { path, rule, documentInventory }) => { | ||
const operationObjectPath = path.slice(0, -1); | ||
const operationObject = resolveObject(documentInventory.resolved, operationObjectPath); | ||
const errors = checkViolationsAndReturnErrors(input, ignoreList, grammaticalWords, operationObjectPath, rule.name); | ||
return evaluateAndCollectAdoptionStatus(errors, rule.name, operationObject, operationObjectPath); | ||
}; | ||
|
||
function checkViolationsAndReturnErrors(summary, ignoreList, grammaticalWords, path, ruleName) { | ||
try { | ||
if (!isTitleCase(summary, ignoreList, grammaticalWords)) { | ||
return [ | ||
{ | ||
path, | ||
message: `Operation summaries must be in Title Case, must not end with a period and must not use CommonMark.`, | ||
}, | ||
]; | ||
} | ||
return []; | ||
} catch (e) { | ||
handleInternalError(ruleName, path, e); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Check if a string is in title case. | ||
* @param {string} str the string to check | ||
* @param {Array<string>} ignoreList list of words to ignore (e.g. abbreviations, acronyms) | ||
* @param {Array<string>} grammaticalWords list of grammatical words that must be lowercase (e.g., "and", "or", "the") | ||
* @returns {boolean} true if the string is in title case, false otherwise | ||
*/ | ||
export function isTitleCase(str, ignoreList, grammaticalWords) { | ||
// Split by spaces to check each word/word-group | ||
// First character should be uppercase, rest lowercase, all alphabetical | ||
const words = str.split(' '); | ||
|
||
return words.every((wordGroup, index) => { | ||
// For hyphenated words, check each part | ||
if (wordGroup.includes('-')) { | ||
const hyphenatedParts = wordGroup.split('-'); | ||
return hyphenatedParts.every((part) => { | ||
if (ignoreList.includes(part)) return true; | ||
return /^[A-Z][a-z]*$/.test(part); | ||
}); | ||
} | ||
|
||
// For regular words | ||
if (ignoreList.includes(wordGroup)) return true; | ||
if (index !== 0 && grammaticalWords.includes(wordGroup)) return true; | ||
return /^[A-Z][a-z]*$/.test(wordGroup); | ||
}); | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.