Skip to content

Commit 674c44f

Browse files
CLOUDP-272001: Disallow usages of project in the api (fix)
1 parent 475abee commit 674c44f

File tree

4 files changed

+20
-34
lines changed

4 files changed

+20
-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: {

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.toLowerCase() === 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ 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 {*}
39+
*/
40+
export function splitCamelCase(str) {
41+
return str.split(/(?=[A-Z])/);
42+
}

0 commit comments

Comments
 (0)