Skip to content

Commit 4f349b6

Browse files
CLOUDP-306161: Improve single resource path evaluation (#536)
1 parent 0f6e848 commit 4f349b6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
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}',

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,33 @@ export function isResourceCollectionIdentifier(path) {
2020
/**
2121
* Checks if a path represents a single resource. For example:
2222
* '/resource/{id}' returns true
23+
* '/resource/{resourceId}/child/{childId}' returns true
2324
* '/resource/{id}/child' returns false
24-
* '/resource/{id}/{id}' returns false
25+
* '/resource' returns false
26+
* '/resource/child/{id}' returns false
2527
*
2628
* @param {string} path the path to evaluate
2729
* @returns {boolean} true if the path represents a single resource, false otherwise
2830
*/
2931
export function isSingleResourceIdentifier(path) {
30-
const pattern = new RegExp(`^.*/[a-zA-Z]+/{[a-zA-Z]+}$`);
31-
return pattern.test(path);
32+
const p = removePrefix(path);
33+
34+
// Check if the path ends with /{paramName} pattern
35+
const endsWithParamPattern = /\/\{[a-zA-Z][a-zA-Z0-9]*}$/;
36+
37+
if (!endsWithParamPattern.test(p)) {
38+
return false;
39+
}
40+
41+
// Extract the part before the final parameter
42+
const lastSlashBeforeParam = p.lastIndexOf('/');
43+
if (lastSlashBeforeParam === -1) {
44+
return false;
45+
}
46+
47+
// Check if the preceding part is a valid resource collection identifier
48+
const collectionPath = p.substring(0, lastSlashBeforeParam);
49+
return isResourceCollectionIdentifier(collectionPath);
3250
}
3351

3452
export function isCustomMethodIdentifier(path) {

0 commit comments

Comments
 (0)