-
Couldn't load subscription status.
- Fork 14
CLOUDP-304930: IPA-102 pattern test #498
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 1 commit
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
92 changes: 92 additions & 0 deletions
92
tools/spectral/ipa/__tests__/collectionIdentifierPattern.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,92 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-102-collection-identifier-pattern', [ | ||
| { | ||
| name: 'valid collection identifiers', | ||
| document: { | ||
| paths: { | ||
| '/resources': {}, | ||
| '/users': {}, | ||
| '/resourceGroups': {}, | ||
| '/api/v2/customers/payments': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'valid with path parameters', | ||
| document: { | ||
| paths: { | ||
| '/resources/{id}': {}, | ||
| '/users/{userId}/profiles': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'valid with custom methods', | ||
| document: { | ||
| paths: { | ||
| '/resources:create': {}, | ||
| '/users/{userId}:activate': {}, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid starts with uppercase', | ||
| document: { | ||
| paths: { | ||
| '/Resources': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-pattern', | ||
| message: | ||
| "Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers (/[a-z][a-zA-Z0-9]*/). Path segment 'Resources' in path '/Resources' doesn't match the required pattern. http://go/ipa/102", | ||
| path: ['paths', '/Resources'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'invalid with special characters', | ||
| document: { | ||
| paths: { | ||
| '/resource-groups': {}, | ||
| '/user_profiles': {}, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-pattern', | ||
| message: | ||
| "Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers (/[a-z][a-zA-Z0-9]*/). Path segment 'resource-groups' in path '/resource-groups' doesn't match the required pattern. http://go/ipa/102", | ||
| path: ['paths', '/resource-groups'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-102-collection-identifier-pattern', | ||
| message: | ||
| "Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers (/[a-z][a-zA-Z0-9]*/). Path segment 'user_profiles' in path '/user_profiles' doesn't match the required pattern. http://go/ipa/102", | ||
| path: ['paths', '/user_profiles'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'valid with path-level exception', | ||
| document: { | ||
| paths: { | ||
| '/resource-groups': { | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-102-collection-identifier-pattern': 'Legacy API path that cannot be changed', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| 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
55 changes: 55 additions & 0 deletions
55
tools/spectral/ipa/rulesets/functions/collectionIdentifierPattern.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,55 @@ | ||
| import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js'; | ||
| import { hasException } from './utils/exceptions.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-102-collection-identifier-pattern'; | ||
| const ERROR_MESSAGE = | ||
| 'Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers (/[a-z][a-zA-Z0-9]*/).'; | ||
| const VALID_IDENTIFIER_PATTERN = /^[a-z][a-zA-Z0-9]*$/; | ||
|
|
||
| /** | ||
| * Checks if collection identifiers in paths begin with a lowercase letter and contain only ASCII letters and numbers | ||
| * | ||
| * @param {object} input - The paths object from the OpenAPI spec | ||
| * @param {object} _ - Unused | ||
| * @param {object} context - The context object containing the path | ||
| */ | ||
| export default (input, _, { path }) => { | ||
| const violations = []; | ||
|
|
||
| Object.keys(input).forEach((pathKey) => { | ||
| // Check for exception at the path level | ||
| if (input[pathKey] && hasException(input[pathKey], RULE_NAME)) { | ||
| collectException(input[pathKey], RULE_NAME, [...path, pathKey]); | ||
| return; | ||
| } | ||
|
|
||
| // Skip path parameters and custom methods | ||
| const pathSegments = pathKey.split('/').filter((segment) => segment.length > 0); | ||
|
|
||
| pathSegments.forEach((segment) => { | ||
| // Skip path parameters (those inside curly braces) | ||
| if (segment.startsWith('{') && segment.endsWith('}')) { | ||
| return; | ||
| } | ||
|
|
||
| // Skip segments with custom methods (containing :) | ||
| if (segment.includes(':')) { | ||
| return; | ||
| } | ||
|
|
||
| // Check the pattern | ||
| if (!VALID_IDENTIFIER_PATTERN.test(segment)) { | ||
| violations.push({ | ||
| message: `${ERROR_MESSAGE} Path segment '${segment}' in path '${pathKey}' doesn't match the required pattern.`, | ||
| path: [...path, pathKey], | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| if (violations.length > 0) { | ||
| return collectAndReturnViolation(path, RULE_NAME, violations); | ||
| } | ||
|
|
||
| return collectAdoption(path, RULE_NAME); | ||
| }; |
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.