Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 257 additions & 0 deletions tools/spectral/ipa/__tests__/collectionIdentifierCamelCase.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-102-collection-identifier-camelCase', [
{
name: 'valid camelCase identifiers',
document: {
paths: {
'/api/v2/atlas/test': {},
'/users': {},
'/resourceGroups': {},
'/userProfiles': {},
'/api/v1/test': {},
},
},
errors: [],
},
{
name: 'valid camelCase with path parameters',
document: {
paths: {
'/resourceGroups/{groupId}': {},
'/users/{userId}/userProfiles': {},
},
},
errors: [],
},
{
name: 'valid paths with custom methods (only checking identifier part)',
document: {
paths: {
'/resources:any_Custom_Method': {},
},
},
errors: [],
},
{
name: 'invalid PascalCase instead of camelCase',
document: {
paths: {
'/Resources': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'Resources' in path '/Resources' is not in camelCase. http://go/ipa/102",
path: ['paths', '/Resources'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid with snake_case instead of camelCase',
document: {
paths: {
'/resource_groups': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'resource_groups' in path '/resource_groups' is not in camelCase. http://go/ipa/102",
path: ['paths', '/resource_groups'],
severity: DiagnosticSeverity.Warning,
},
Comment on lines +57 to +68
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: If the path is for example /resource_groups/{id}/resource_keys, will there be two errors, one for resource_groups and one for resource_keys?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single violation with 2 errors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, could you add a test case for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extended multiple test cases.

],
},
{
name: 'invalid with kebab-case instead of camelCase',
document: {
paths: {
'/resource-groups': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'resource-groups' in path '/resource-groups' is not in camelCase. http://go/ipa/102",
path: ['paths', '/resource-groups'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid resource path with invalid casing but valid custom method',
document: {
paths: {
'/Resources:createResource': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'Resources' in path '/Resources:createResource' is not in camelCase. http://go/ipa/102",
path: ['paths', '/Resources:createResource'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid with consecutive uppercase letters',
document: {
paths: {
'/resourcesAPI': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'resourcesAPI' in path '/resourcesAPI' is not in camelCase. http://go/ipa/102",
path: ['paths', '/resourcesAPI'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'valid with path-level exception',
document: {
paths: {
'/resource_groups': {
'x-xgen-IPA-exception': {
'xgen-IPA-102-collection-identifier-camelCase': 'Legacy API path that cannot be changed',
},
},
},
},
errors: [],
},
{
name: 'reports violations for paths with double slashes',
document: {
paths: {
'/api//users': {},
'/resources///{resourceId}': {},
'//doubleSlashAtStart': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path '/api//users' contains double slashes (//) which is not allowed. http://go/ipa/102",
path: ['paths', '/api//users'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path '/resources///{resourceId}' contains double slashes (//) which is not allowed. http://go/ipa/102",
path: ['paths', '/resources///{resourceId}'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path '//doubleSlashAtStart' contains double slashes (//) which is not allowed. http://go/ipa/102",
path: ['paths', '//doubleSlashAtStart'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'handles paths with trailing slashes',
document: {
paths: {
'/api/users/': {},
'/resources/{resourceId}/': {},
},
},
errors: [],
},
{
name: 'detects multiple failures across a single path',
document: {
paths: {
'/API/Resource_groups/{userId}/User-profiles': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'API' in path '/API/Resource_groups/{userId}/User-profiles' is not in camelCase. http://go/ipa/102",
path: ['paths', '/API/Resource_groups/{userId}/User-profiles'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'Resource_groups' in path '/API/Resource_groups/{userId}/User-profiles' is not in camelCase. http://go/ipa/102",
path: ['paths', '/API/Resource_groups/{userId}/User-profiles'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'User-profiles' in path '/API/Resource_groups/{userId}/User-profiles' is not in camelCase. http://go/ipa/102",
path: ['paths', '/API/Resource_groups/{userId}/User-profiles'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'handles mixed valid and invalid segments with custom methods',
document: {
paths: {
'/api/Valid/Invalid_resource/{id}:validCustomMethod': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'Valid' in path '/api/Valid/Invalid_resource/{id}:validCustomMethod' is not in camelCase. http://go/ipa/102",
path: ['paths', '/api/Valid/Invalid_resource/{id}:validCustomMethod'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'Invalid_resource' in path '/api/Valid/Invalid_resource/{id}:validCustomMethod' is not in camelCase. http://go/ipa/102",
path: ['paths', '/api/Valid/Invalid_resource/{id}:validCustomMethod'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'handles double slashes with invalid segments - both issues reported',
document: {
paths: {
'/api//Invalid_segment//resources': {},
},
},
errors: [
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path '/api//Invalid_segment//resources' contains double slashes (//) which is not allowed. http://go/ipa/102",
path: ['paths', '/api//Invalid_segment//resources'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-102-collection-identifier-camelCase',
message:
"Collection identifiers must be in camelCase. Path segment 'Invalid_segment' in path '/api//Invalid_segment//resources' is not in camelCase. http://go/ipa/102",
path: ['paths', '/api//Invalid_segment//resources'],
severity: DiagnosticSeverity.Warning,
},
],
},
]);
22 changes: 22 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-102.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@
# http://go/ipa/102

rules:
xgen-IPA-102-collection-identifier-camelCase:
description: >-
Collection identifiers must be in camelCase. Logic includes:<br/>
- All path segments that are not path parameters<br/>
- Only the resource identifier part before any colon in custom method paths (e.g., `resource` in `/resource:customMethod`)<br/>
- Path parameters should also follow camelCase naming<br/>
- Certain values can be exempted via the ignoredValues configuration (e.g., 'v1', 'v2') that can be supplied as `ignoredValues`
argument to the rule<br/>
- Paths with `x-xgen-IPA-exception` for this rule are excluded from validation<br/>
- Double slashes (//) are not allowed in paths<br/>
http://go/ipa/102
message: '{{error}} http://go/ipa/102'
severity: warn
given: $.paths
then:
field: '@key'
function: collectionIdentifierCamelCase
functionOptions:
# Contains list of ignored path params
ignoredValues: ['v2', 'v1']

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'
Expand All @@ -23,3 +44,4 @@ rules:
functions:
- collectionIdentifierPattern
- eachPathAlternatesBetweenResourceNameAndPathParam
- collectionIdentifierCamelCase
9 changes: 5 additions & 4 deletions tools/spectral/ipa/rulesets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ For rule definitions, see [IPA-005.yaml](https://github.com/mongodb/openapi/blob

For rule definitions, see [IPA-102.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/rulesets/IPA-102.yaml).

| Rule Name | Description | Severity |
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | -------- |
| xgen-IPA-102-path-alternate-resource-name-path-param | Paths should alternate between resource names and path params. http://go/ipa/102 | error |
| xgen-IPA-102-collection-identifier-pattern | Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers. http://go/ipa/102 | warn |
| Rule Name | Description | Severity |
| ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| xgen-IPA-102-path-alternate-resource-name-path-param | Paths should alternate between resource names and path params. http://go/ipa/102 | error |
| xgen-IPA-102-collection-identifier-camelCase | Collection identifiers must be in camelCase. Logic includes:<br/> - All path segments that are not path parameters<br/> - Only the resource identifier part before any colon in custom method paths (e.g., `resource` in `/resource:customMethod`)<br/> - Path parameters should also follow camelCase naming<br/> - Certain values can be exempted via the ignoredValues configuration (e.g., 'v1', 'v2') that can be supplied as `ignoredValues` argument to the rule<br/> - Paths with `x-xgen-IPA-exception` for this rule are excluded from validation<br/> - Double slashes (//) are not allowed in paths<br/> http://go/ipa/102 | warn |
| xgen-IPA-102-collection-identifier-pattern | Collection identifiers must begin with a lowercase letter and contain only ASCII letters and numbers. http://go/ipa/102 | warn |

### IPA-104

Expand Down
Loading
Loading