-
Notifications
You must be signed in to change notification settings - Fork 14
CLOUDP-304967: Add IPA rule for description format #592
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
3 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
83 changes: 83 additions & 0 deletions
83
tools/spectral/ipa/__tests__/IPA117DescriptionEndsWithPeriod.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,83 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-117-description-ends-with-period', [ | ||
| { | ||
| name: 'valid description', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| id: { | ||
| description: 'Description.', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid descriptions', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| noPeriod: { | ||
| description: 'Description', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-117-description-ends-with-period', | ||
| message: 'Descriptions must end with a full stop(.).', | ||
| path: ['components', 'schemas', 'Schema', 'properties', 'noPeriod'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'ignores descriptions ending with table', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| noPeriod: { | ||
| description: 'Description\n| Table |', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid components with exceptions', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| noPeriod: { | ||
| description: 'Description', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-117-description-ends-with-period': 'reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| ]); |
66 changes: 66 additions & 0 deletions
66
tools/spectral/ipa/__tests__/IPA117DescriptionStartsWithUpperCase.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,66 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-117-description-starts-with-uppercase', [ | ||
| { | ||
| name: 'valid description', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| id: { | ||
| description: 'Description', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid descriptions', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| noUpperCase: { | ||
| description: 'description', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-117-description-starts-with-uppercase', | ||
| message: 'Descriptions must start with Uppercase.', | ||
| path: ['components', 'schemas', 'Schema', 'properties', 'noUpperCase'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid components with exceptions', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| Schema: { | ||
| properties: { | ||
| noUpperCase: { | ||
| description: 'description', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-117-description-starts-with-uppercase': '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
41 changes: 41 additions & 0 deletions
41
tools/spectral/ipa/rulesets/functions/IPA117DescriptionEndsWithPeriod.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,41 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-117-description-ends-with-period'; | ||
| const ERROR_MESSAGE_PERIOD = 'Descriptions must end with a full stop(.).'; | ||
|
|
||
| export default (input, opts, { path }) => { | ||
| // Ignore missing descriptions or descriptions that ends with an inline table | ||
| if (!input['description'] || input['description'].endsWith('|') || input['description'].endsWith('|\n')) { | ||
| return; | ||
| } | ||
|
|
||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const errors = checkViolationsAndReturnErrors(input['description'], path); | ||
| if (errors.length !== 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| function checkViolationsAndReturnErrors(description, path) { | ||
| const periodEnd = new RegExp(`[.]$`); | ||
|
|
||
| try { | ||
| if (!periodEnd.test(description)) { | ||
| return [{ path, message: ERROR_MESSAGE_PERIOD }]; | ||
| } | ||
| return []; | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, path, e); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
tools/spectral/ipa/rulesets/functions/IPA117DescriptionStartsWithUpperCase.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,40 @@ | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { | ||
| collectAdoption, | ||
| collectAndReturnViolation, | ||
| collectException, | ||
| handleInternalError, | ||
| } from './utils/collectionUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-117-description-starts-with-uppercase'; | ||
| const ERROR_MESSAGE_UPPER_CASE = 'Descriptions must start with Uppercase.'; | ||
|
|
||
| export default (input, opts, { path }) => { | ||
| if (!input['description']) { | ||
| return; | ||
| } | ||
|
|
||
| if (hasException(input, RULE_NAME)) { | ||
| collectException(input, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| const errors = checkViolationsAndReturnErrors(input['description'], path); | ||
| if (errors.length !== 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, errors); | ||
| } | ||
| collectAdoption(path, RULE_NAME); | ||
| }; | ||
|
|
||
| function checkViolationsAndReturnErrors(description, path) { | ||
| const upperCaseStart = new RegExp(`^[A-Z]`); | ||
|
|
||
| try { | ||
| if (!upperCaseStart.test(description)) { | ||
| return [{ path, message: ERROR_MESSAGE_UPPER_CASE }]; | ||
| } | ||
| return []; | ||
| } catch (e) { | ||
| handleInternalError(RULE_NAME, path, e); | ||
| } | ||
| } |
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.
Q: Can't we merge them into same rule as you did before, and giving inline table marks as functionOptions
Uh oh!
There was an error while loading. Please reload this page.
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.
+1.
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.
Was thinking about it initially though I didn't want to skip validating that the first letter is uppercase because it ends with
|, so I split them into two to have clearer logic in the validator. Though we could add this as a function option for thedescription-ends-with-period, wdyt?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.
Good context. Based on your explanation current state in PR SGTM