Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
properties: {
group: { type: 'string' },
groupId: { type: 'string' },
otherField: { type: 'number' },
projection: { type: 'number' },
},
},
},
Expand Down Expand Up @@ -57,7 +57,7 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
errors: [
{
code: 'xgen-IPA-112-avoid-project-field-names',
message: 'Field name "projects" should be avoided. Consider using "group" instead.',
message: 'Field name "projects" should be avoided. Consider using "groups" instead.',
path: ['components', 'schemas', 'SchemaName', 'properties', 'projects'],
severity: DiagnosticSeverity.Warning,
},
Expand Down Expand Up @@ -85,35 +85,6 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
},
],
},
{
name: 'invalid schema - with different case variants',
document: {
components: {
schemas: {
SchemaName: {
properties: {
Project: { type: 'string' },
PROJECTID: { type: 'string' },
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-112-avoid-project-field-names',
message: 'Field name "Project" should be avoided. Consider using "group" instead.',
path: ['components', 'schemas', 'SchemaName', 'properties', 'Project'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-112-avoid-project-field-names',
message: 'Field name "PROJECTID" should be avoided. Consider using "group" instead.',
path: ['components', 'schemas', 'SchemaName', 'properties', 'PROJECTID'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid schema with exception - project field name with exception',
document: {
Expand Down
30 changes: 30 additions & 0 deletions tools/spectral/ipa/__tests__/utils/schemaUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { splitCamelCase } from '../../rulesets/functions/utils/schemaUtils.js';
import { describe, expect, it } from '@jest/globals';

describe('splitCamelCase', () => {
it('should split basic camelCase strings', () => {
expect(splitCamelCase('camelCase')).toEqual(['camel', 'case']);
expect(splitCamelCase('thisIsCamelCase')).toEqual(['this', 'is', 'camel', 'case']);
});

it('should handle single-word strings', () => {
expect(splitCamelCase('word')).toEqual(['word']);
expect(splitCamelCase('Word')).toEqual(['word']);
});

it('should handle strings with numbers', () => {
expect(splitCamelCase('user123Name')).toEqual(['user123', 'name']);
expect(splitCamelCase('user123name')).toEqual(['user123name']);
});

it('should handle empty strings', () => {
expect(splitCamelCase('')).toEqual(['']);
});

it('should handle edge cases from the project', () => {
expect(splitCamelCase('project')).toEqual(['project']);
expect(splitCamelCase('projectId')).toEqual(['project', 'id']);
expect(splitCamelCase('myProjectDetails')).toEqual(['my', 'project', 'details']);
expect(splitCamelCase('projection')).toEqual(['projection']);
});
});
2 changes: 2 additions & 0 deletions tools/spectral/ipa/rulesets/IPA-112.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ rules:
prohibitedFieldNames:
- name: 'project'
alternative: ['group']
- name: 'projects'
alternative: ['groups']
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
handleInternalError,
} from './utils/collectionUtils.js';
import { resolveObject } from './utils/componentUtils.js';
import { splitCamelCase } from './utils/schemaUtils.js';

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

Expand All @@ -28,18 +29,20 @@ export default (input, options, { path, documentInventory }) => {
function checkViolationsAndReturnErrors(input, options, path) {
try {
const prohibitedFieldNames = options?.prohibitedFieldNames || [];
const lowerPropertyName = input.toLowerCase();

// Split the field name into words assuming camelCase
const words = splitCamelCase(input);

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

if (lowerPropertyName.includes(prohibitedName.toLowerCase())) {
if (words.some((word) => word === prohibitedName)) {
return [
{
path,
message: `Field name "${input}" should be avoided. Consider using ${alternative.map((alt) => `"${alt}"`)} instead.`,
message: `Field name "${input}" should be avoided. Consider using "${alternative}" instead.`,
},
];
}
Expand Down
15 changes: 15 additions & 0 deletions tools/spectral/ipa/rulesets/functions/utils/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ export function getSchemaPathFromEnumPath(path) {
}
return path.slice(0, enumIndex);
}

/**
* Split camelCase string into words
* Example: "myProjectId" becomes ["my", "Project", "Id"]
* @param str {string} camelCase string
* @returns {*}
*/
export function splitCamelCase(str) {
if (!str) return [''];

// Special handling for single words
if (!/[A-Z]/.test(str)) return [str];

return str.split(/(?=[A-Z])/).map((word) => word.toLowerCase());
}
Loading