Skip to content

Commit a8d9fc6

Browse files
CLOUDP-306161: Improve single resource path evaluation
1 parent 936804f commit a8d9fc6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

tools/spectral/ipa/__tests__/utils/resourceEvaluation.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ describe('tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js', ()
227227
path: '/resourceOne/{id}/{id}',
228228
isSingleResourceIdentifier: false,
229229
},
230+
{
231+
description: 'invalid single identifier',
232+
path: '/resource/resource/{id}',
233+
isSingleResourceIdentifier: false,
234+
},
230235
{
231236
description: 'single identifier',
232237
path: '/resource/{id}',
@@ -236,7 +241,7 @@ describe('tools/spectral/ipa/rulesets/functions/utils/resourceEvaluation.js', ()
236241
description: 'single identifier child',
237242
path: '/resource/{id}/child/{id}',
238243
isSingleResourceIdentifier: true,
239-
},
244+
}
240245
];
241246

242247
testCases.forEach((testCase) => {

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,24 @@ export function isResourceCollectionIdentifier(path) {
2727
* @returns {boolean} true if the path represents a single resource, false otherwise
2828
*/
2929
export function isSingleResourceIdentifier(path) {
30-
const pattern = new RegExp(`^.*/[a-zA-Z]+/{[a-zA-Z]+}$`);
31-
return pattern.test(path);
30+
const p = removePrefix(path);
31+
32+
// Check if the path ends with /{paramName} pattern
33+
const endsWithParamPattern = /\/\{[a-zA-Z][a-zA-Z0-9]*}$/;
34+
35+
if (!endsWithParamPattern.test(p)) {
36+
return false;
37+
}
38+
39+
// Extract the part before the final parameter
40+
const lastSlashBeforeParam = p.lastIndexOf('/');
41+
if (lastSlashBeforeParam === -1) {
42+
return false;
43+
}
44+
45+
// Check if the preceding part is a valid resource collection identifier
46+
const collectionPath = p.substring(0, lastSlashBeforeParam);
47+
return isResourceCollectionIdentifier(collectionPath);
3248
}
3349

3450
export function isCustomMethodIdentifier(path) {

0 commit comments

Comments
 (0)