Skip to content

Commit b99ef44

Browse files
CLOUDP-272001: Disallow usages of project in the api (fix) (#571)
1 parent 86096a7 commit b99ef44

File tree

5 files changed

+55
-34
lines changed

5 files changed

+55
-34
lines changed

tools/spectral/ipa/__tests__/IPA112AvoidProjectFieldNames.test.js

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
1111
properties: {
1212
group: { type: 'string' },
1313
groupId: { type: 'string' },
14-
otherField: { type: 'number' },
14+
projection: { type: 'number' },
1515
},
1616
},
1717
},
@@ -57,7 +57,7 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
5757
errors: [
5858
{
5959
code: 'xgen-IPA-112-avoid-project-field-names',
60-
message: 'Field name "projects" should be avoided. Consider using "group" instead.',
60+
message: 'Field name "projects" should be avoided. Consider using "groups" instead.',
6161
path: ['components', 'schemas', 'SchemaName', 'properties', 'projects'],
6262
severity: DiagnosticSeverity.Warning,
6363
},
@@ -85,35 +85,6 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
8585
},
8686
],
8787
},
88-
{
89-
name: 'invalid schema - with different case variants',
90-
document: {
91-
components: {
92-
schemas: {
93-
SchemaName: {
94-
properties: {
95-
Project: { type: 'string' },
96-
PROJECTID: { type: 'string' },
97-
},
98-
},
99-
},
100-
},
101-
},
102-
errors: [
103-
{
104-
code: 'xgen-IPA-112-avoid-project-field-names',
105-
message: 'Field name "Project" should be avoided. Consider using "group" instead.',
106-
path: ['components', 'schemas', 'SchemaName', 'properties', 'Project'],
107-
severity: DiagnosticSeverity.Warning,
108-
},
109-
{
110-
code: 'xgen-IPA-112-avoid-project-field-names',
111-
message: 'Field name "PROJECTID" should be avoided. Consider using "group" instead.',
112-
path: ['components', 'schemas', 'SchemaName', 'properties', 'PROJECTID'],
113-
severity: DiagnosticSeverity.Warning,
114-
},
115-
],
116-
},
11788
{
11889
name: 'invalid schema with exception - project field name with exception',
11990
document: {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { splitCamelCase } from '../../rulesets/functions/utils/schemaUtils.js';
2+
import { describe, expect, it } from '@jest/globals';
3+
4+
describe('splitCamelCase', () => {
5+
it('should split basic camelCase strings', () => {
6+
expect(splitCamelCase('camelCase')).toEqual(['camel', 'case']);
7+
expect(splitCamelCase('thisIsCamelCase')).toEqual(['this', 'is', 'camel', 'case']);
8+
});
9+
10+
it('should handle single-word strings', () => {
11+
expect(splitCamelCase('word')).toEqual(['word']);
12+
expect(splitCamelCase('Word')).toEqual(['word']);
13+
});
14+
15+
it('should handle strings with numbers', () => {
16+
expect(splitCamelCase('user123Name')).toEqual(['user123', 'name']);
17+
expect(splitCamelCase('user123name')).toEqual(['user123name']);
18+
});
19+
20+
it('should handle empty strings', () => {
21+
expect(splitCamelCase('')).toEqual(['']);
22+
});
23+
24+
it('should handle edge cases from the project', () => {
25+
expect(splitCamelCase('project')).toEqual(['project']);
26+
expect(splitCamelCase('projectId')).toEqual(['project', 'id']);
27+
expect(splitCamelCase('myProjectDetails')).toEqual(['my', 'project', 'details']);
28+
expect(splitCamelCase('projection')).toEqual(['projection']);
29+
});
30+
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ rules:
2424
prohibitedFieldNames:
2525
- name: 'project'
2626
alternative: ['group']
27+
- name: 'projects'
28+
alternative: ['groups']

tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
handleInternalError,
77
} from './utils/collectionUtils.js';
88
import { resolveObject } from './utils/componentUtils.js';
9+
import { splitCamelCase } from './utils/schemaUtils.js';
910

1011
const RULE_NAME = 'xgen-IPA-112-avoid-project-field-names';
1112

@@ -28,18 +29,20 @@ export default (input, options, { path, documentInventory }) => {
2829
function checkViolationsAndReturnErrors(input, options, path) {
2930
try {
3031
const prohibitedFieldNames = options?.prohibitedFieldNames || [];
31-
const lowerPropertyName = input.toLowerCase();
32+
33+
// Split the field name into words assuming camelCase
34+
const words = splitCamelCase(input);
3235

3336
// Check if the property name includes any of the prohibited terms
3437
for (const prohibitedItem of prohibitedFieldNames) {
3538
const prohibitedName = prohibitedItem.name || '';
3639
const alternative = prohibitedItem.alternative || '';
3740

38-
if (lowerPropertyName.includes(prohibitedName.toLowerCase())) {
41+
if (words.some((word) => word === prohibitedName)) {
3942
return [
4043
{
4144
path,
42-
message: `Field name "${input}" should be avoided. Consider using ${alternative.map((alt) => `"${alt}"`)} instead.`,
45+
message: `Field name "${input}" should be avoided. Consider using "${alternative}" instead.`,
4346
},
4447
];
4548
}

tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ export function getSchemaPathFromEnumPath(path) {
3030
}
3131
return path.slice(0, enumIndex);
3232
}
33+
34+
/**
35+
* Split camelCase string into words
36+
* Example: "myProjectId" becomes ["my", "Project", "Id"]
37+
* @param str {string} camelCase string
38+
* @returns {string[]}
39+
*/
40+
export function splitCamelCase(str) {
41+
if (!str) return [''];
42+
43+
// Special handling for single words
44+
if (!/[A-Z]/.test(str)) return [str];
45+
46+
return str.split(/(?=[A-Z])/).map((word) => word.toLowerCase());
47+
}

0 commit comments

Comments
 (0)