Skip to content

Commit 9093492

Browse files
CLOUDP-271999: Field names must use camelCase
1 parent 8411c91 commit 9093492

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-112-field-names-are-camel-case', [
5+
{
6+
name: 'valid schema - all field names are camelCase',
7+
document: {
8+
components: {
9+
schemas: {
10+
SchemaName: {
11+
properties: {
12+
userId: { type: 'string' },
13+
firstName: { type: 'string' },
14+
emailAddress: { type: 'string' },
15+
phoneNumber: { type: 'string' },
16+
},
17+
},
18+
},
19+
},
20+
},
21+
errors: [],
22+
},
23+
{
24+
name: 'invalid schema',
25+
document: {
26+
components: {
27+
schemas: {
28+
SchemaName: {
29+
properties: {
30+
UserId: { type: 'string' },
31+
user_id: { type: 'string' },
32+
'user-id': { type: 'string' },
33+
},
34+
},
35+
},
36+
},
37+
},
38+
errors: [
39+
{
40+
code: 'xgen-IPA-112-field-names-are-camel-case',
41+
message: 'Property "UserId" must use camelCase format.',
42+
path: ['components', 'schemas', 'SchemaName', 'properties', 'UserId'],
43+
severity: DiagnosticSeverity.Warning,
44+
},
45+
{
46+
code: 'xgen-IPA-112-field-names-are-camel-case',
47+
message: 'Property "user_id" must use camelCase format.',
48+
path: ['components', 'schemas', 'SchemaName', 'properties', 'user_id'],
49+
severity: DiagnosticSeverity.Warning,
50+
},
51+
{
52+
code: 'xgen-IPA-112-field-names-are-camel-case',
53+
message: 'Property "user-id" must use camelCase format.',
54+
path: ['components', 'schemas', 'SchemaName', 'properties', 'user-id'],
55+
severity: DiagnosticSeverity.Warning,
56+
},
57+
],
58+
},
59+
{
60+
name: 'schema with exception - non-camelCase field with exception',
61+
document: {
62+
components: {
63+
schemas: {
64+
SchemaName: {
65+
properties: {
66+
'NON-CAMEL-CASE': {
67+
type: 'string',
68+
'x-xgen-IPA-exception': {
69+
'xgen-IPA-112-field-names-are-camel-case': 'Reason',
70+
},
71+
},
72+
},
73+
},
74+
},
75+
},
76+
},
77+
errors: [],
78+
},
79+
{
80+
name: 'mixed valid, invalid, and exception fields',
81+
document: {
82+
components: {
83+
schemas: {
84+
SchemaName: {
85+
properties: {
86+
userId: { type: 'string' },
87+
'API-Key': {
88+
type: 'string',
89+
'x-xgen-IPA-exception': {
90+
'xgen-IPA-112-field-names-are-camel-case': 'Reason',
91+
},
92+
},
93+
User_Name: {
94+
type: 'string',
95+
'x-xgen-IPA-exception': {
96+
'xgen-IPA-112-field-names-are-camel-case': 'Reason',
97+
},
98+
},
99+
},
100+
},
101+
},
102+
},
103+
},
104+
errors: [],
105+
},
106+
]);

tools/spectral/ipa/rulesets/IPA-112.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
functions:
55
- IPA112AvoidProjectFieldNames
6+
- IPA112FieldNamesAreCamelCase
67

78
rules:
89
xgen-IPA-112-avoid-project-field-names:
@@ -26,3 +27,17 @@ rules:
2627
alternative: ['group']
2728
- name: 'projects'
2829
alternative: ['groups']
30+
xgen-IPA-112-field-names-are-camel-case:
31+
description: |
32+
Schema field names should be in camelCase format.
33+
34+
##### Implementation details
35+
Rule checks for the following conditions:
36+
- Searches through all schemas in the API definition
37+
- Identifies property names that are not in camelCase format
38+
- Reports any instances where these field names are not in camelCase format
39+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-field-names-are-camel-case'
40+
severity: warn
41+
given: '$.components.schemas..properties[*]~'
42+
then:
43+
function: 'IPA112FieldNamesAreCamelCase'

tools/spectral/ipa/rulesets/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,17 @@ Rule checks for the following conditions:
463463
- Reports any instances where these field names are used
464464
- Suggests using "group", "groups", or "groupId" as alternatives
465465

466+
#### xgen-IPA-112-field-names-are-camel-case
467+
468+
![warn](https://img.shields.io/badge/warning-yellow)
469+
Schema field names should be in camelCase format.
470+
471+
##### Implementation details
472+
Rule checks for the following conditions:
473+
- Searches through all schemas in the API definition
474+
- Identifies property names that are not in camelCase format
475+
- Reports any instances where these field names are not in camelCase format
476+
466477

467478

468479
### IPA-113
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { casing } from '@stoplight/spectral-functions';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import { hasException } from './utils/exceptions.js';
4+
import { resolveObject } from './utils/componentUtils.js';
5+
6+
const RULE_NAME = 'xgen-IPA-112-field-names-are-camel-case';
7+
8+
export default (input, options, { path, documentInventory }) => {
9+
const oas = documentInventory.resolved;
10+
const property = resolveObject(oas, path);
11+
12+
if (hasException(property, RULE_NAME)) {
13+
collectException(property, RULE_NAME, path);
14+
return;
15+
}
16+
17+
if (casing(input, { type: 'camel', disallowDigits: true })) {
18+
const errorMessage = `Property "${input}" must use camelCase format.`;
19+
return collectAndReturnViolation(path, RULE_NAME, errorMessage);
20+
}
21+
collectAdoption(path, RULE_NAME);
22+
};

0 commit comments

Comments
 (0)