-
Notifications
You must be signed in to change notification settings - Fork 14
IPA-112: Field names must use camelCase #589
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
9093492
4521d8e
754003a
a306ef9
7dcc21d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import testRule from './__helpers__/testRule'; | ||
| import { DiagnosticSeverity } from '@stoplight/types'; | ||
|
|
||
| testRule('xgen-IPA-112-field-names-are-camel-case', [ | ||
| { | ||
| name: 'valid schema - all field names are camelCase', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| userId: { type: 'string' }, | ||
| firstName: { type: 'string' }, | ||
| emailAddress: { type: 'string' }, | ||
| phoneNumber: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'invalid schema', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| UserId: { type: 'string' }, | ||
| user_id: { type: 'string' }, | ||
| 'user-id': { type: 'string' }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [ | ||
| { | ||
| code: 'xgen-IPA-112-field-names-are-camel-case', | ||
| message: 'Property "UserId" must use camelCase format.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'UserId'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-112-field-names-are-camel-case', | ||
| message: 'Property "user_id" must use camelCase format.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'user_id'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| { | ||
| code: 'xgen-IPA-112-field-names-are-camel-case', | ||
| message: 'Property "user-id" must use camelCase format.', | ||
| path: ['components', 'schemas', 'SchemaName', 'properties', 'user-id'], | ||
| severity: DiagnosticSeverity.Warning, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'schema with exception - non-camelCase field with exception', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| 'NON-CAMEL-CASE': { | ||
| type: 'string', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-field-names-are-camel-case': 'Reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| { | ||
| name: 'mixed valid, invalid, and exception fields', | ||
| document: { | ||
| components: { | ||
| schemas: { | ||
| SchemaName: { | ||
| properties: { | ||
| userId: { type: 'string' }, | ||
| 'API-Key': { | ||
| type: 'string', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-field-names-are-camel-case': 'Reason', | ||
| }, | ||
| }, | ||
| User_Name: { | ||
| type: 'string', | ||
| 'x-xgen-IPA-exception': { | ||
| 'xgen-IPA-112-field-names-are-camel-case': 'Reason', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| errors: [], | ||
| }, | ||
| ]); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
|
|
||
| functions: | ||
| - IPA112AvoidProjectFieldNames | ||
| - IPA112FieldNamesAreCamelCase | ||
|
|
||
| rules: | ||
| xgen-IPA-112-avoid-project-field-names: | ||
|
|
@@ -29,3 +30,17 @@ rules: | |
| alternative: ['groups'] | ||
| ignore: | ||
| - 'gcp' | ||
| xgen-IPA-112-field-names-are-camel-case: | ||
| description: | | ||
| Schema field names should be in camelCase format. | ||
|
|
||
| ##### Implementation details | ||
| Rule checks for the following conditions: | ||
| - Searches through all schemas in the API definition | ||
| - Identifies property names that are not in camelCase format | ||
| - Reports any instances where these field names are not in camelCase format | ||
| message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-field-names-are-camel-case' | ||
| severity: warn | ||
| given: '$.components.schemas..properties[*]~' | ||
|
||
| then: | ||
| function: 'IPA112FieldNamesAreCamelCase' | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { casing } from '@stoplight/spectral-functions'; | ||
| import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js'; | ||
| import { hasException } from './utils/exceptions.js'; | ||
| import { resolveObject } from './utils/componentUtils.js'; | ||
|
|
||
| const RULE_NAME = 'xgen-IPA-112-field-names-are-camel-case'; | ||
|
|
||
| export default (input, options, { path, documentInventory }) => { | ||
| const oas = documentInventory.resolved; | ||
| const property = resolveObject(oas, path); | ||
|
|
||
| if (hasException(property, RULE_NAME)) { | ||
| collectException(property, RULE_NAME, path); | ||
| return; | ||
| } | ||
|
|
||
| if (casing(input, { type: 'camel', disallowDigits: true })) { | ||
| const errorMessage = `Property "${input}" must use camelCase format.`; | ||
| return collectAndReturnViolation(path, RULE_NAME, errorMessage); | ||
| } | ||
| collectAdoption(path, RULE_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.
Q: Does it also catch nested schemas, and arrays etc.?
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.
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.
Thanks!
For arrays:
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.
You mean the
propertiesinitems. Sure, let me add unit tests covering both and see how it works