Skip to content

Commit 1c6cfa2

Browse files
address the comments
1 parent 3eceba0 commit 1c6cfa2

File tree

4 files changed

+41
-105
lines changed

4 files changed

+41
-105
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*.yaml
55
*.yml
66
*.html
7+
!/tools/spectral/ipa/**/*.yaml

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

Lines changed: 26 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,13 @@ testRule('xgen-IPA-102-path-alternate-resource-name-path-param', [
66
name: 'valid paths - api/atlas/v2',
77
document: {
88
paths: {
9-
'/api/atlas/v2/resourceName': {
10-
post: {},
11-
get: {},
12-
},
13-
'/api/atlas/v2/resourceName/{pathParam}': {
14-
get: {},
15-
patch: {},
16-
delete: {},
17-
},
18-
'/api/atlas/v2/resourceName1/{pathParam}/resourceName2': {
19-
post: {},
20-
get: {},
21-
},
22-
'/api/atlas/v2/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {
23-
get: {},
24-
patch: {},
25-
delete: {},
26-
},
27-
'/api/atlas/v2/resourceName/{pathParam}:method': {
28-
post: {},
29-
},
30-
'/api/atlas/v2/custom:method': {
31-
post: {},
32-
},
33-
'/api/atlas/v2': {
34-
post: {},
35-
},
9+
'/api/atlas/v2/resourceName': {},
10+
'/api/atlas/v2/resourceName/{pathParam}': {},
11+
'/api/atlas/v2/resourceName1/{pathParam}/resourceName2': {},
12+
'/api/atlas/v2/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {},
13+
'/api/atlas/v2/resourceName/{pathParam}:method': {},
14+
'/api/atlas/v2/custom:method': {},
15+
'/api/atlas/v2': {},
3616
},
3717
},
3818
errors: [],
@@ -41,33 +21,13 @@ testRule('xgen-IPA-102-path-alternate-resource-name-path-param', [
4121
name: 'valid paths - api/atlas/v2/unauth',
4222
document: {
4323
paths: {
44-
'/api/atlas/v2/unauth/resourceName': {
45-
post: {},
46-
get: {},
47-
},
48-
'/api/atlas/v2/unauth/resourceName/{pathParam}': {
49-
get: {},
50-
patch: {},
51-
delete: {},
52-
},
53-
'/api/atlas/v2/unauth/resourceName1/{pathParam}/resourceName2': {
54-
post: {},
55-
get: {},
56-
},
57-
'/api/atlas/v2/unauth/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {
58-
get: {},
59-
patch: {},
60-
delete: {},
61-
},
62-
'/api/atlas/v2/unauth/resourceName/{pathParam}:method': {
63-
post: {},
64-
},
65-
'/api/atlas/v2/unauth/custom:method': {
66-
post: {},
67-
},
68-
'/api/atlas/v2/unauth': {
69-
post: {},
70-
},
24+
'/api/atlas/v2/unauth/resourceName': {},
25+
'/api/atlas/v2/unauth/resourceName/{pathParam}': {},
26+
'/api/atlas/v2/unauth/resourceName1/{pathParam}/resourceName2': {},
27+
'/api/atlas/v2/unauth/resourceName1/{pathParam1p}/resourceName2/{pathParam2}': {},
28+
'/api/atlas/v2/unauth/resourceName/{pathParam}:method': {},
29+
'/api/atlas/v2/unauth/custom:method': {},
30+
'/api/atlas/v2/unauth': {},
7131
},
7232
},
7333
errors: [],
@@ -76,30 +36,12 @@ testRule('xgen-IPA-102-path-alternate-resource-name-path-param', [
7636
name: 'invalid paths - api/atlas/v2',
7737
document: {
7838
paths: {
79-
'/api/atlas/v2/resourceName1/resourceName2': {
80-
post: {},
81-
get: {},
82-
},
83-
'/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}': {
84-
patch: {},
85-
delete: {},
86-
},
87-
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3': {
88-
patch: {},
89-
delete: {},
90-
},
91-
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {
92-
patch: {},
93-
delete: {},
94-
},
95-
'/api/atlas/v2/{pathParam}': {
96-
post: {},
97-
get: {},
98-
},
99-
'/api/atlas/v2/{pathParam1}/{pathParam2}': {
100-
post: {},
101-
get: {},
102-
},
39+
'/api/atlas/v2/resourceName1/resourceName2': {},
40+
'/api/atlas/v2/resourceName/{pathParam1}/{pathParam2}': {},
41+
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/resourceName3': {},
42+
'/api/atlas/v2/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {},
43+
'/api/atlas/v2/{pathParam}': {},
44+
'/api/atlas/v2/{pathParam1}/{pathParam2}': {},
10345
},
10446
},
10547
errors: [
@@ -145,30 +87,12 @@ testRule('xgen-IPA-102-path-alternate-resource-name-path-param', [
14587
name: 'invalid paths - api/atlas/v2/unauth',
14688
document: {
14789
paths: {
148-
'/api/atlas/v2/unauth/resourceName1/resourceName2': {
149-
post: {},
150-
get: {},
151-
},
152-
'/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}': {
153-
patch: {},
154-
delete: {},
155-
},
156-
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3': {
157-
patch: {},
158-
delete: {},
159-
},
160-
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {
161-
patch: {},
162-
delete: {},
163-
},
164-
'/api/atlas/v2/unauth/{pathParam}': {
165-
post: {},
166-
get: {},
167-
},
168-
'/api/atlas/v2/unauth/{pathParam1}/{pathParam2}': {
169-
post: {},
170-
get: {},
171-
},
90+
'/api/atlas/v2/unauth/resourceName1/resourceName2': {},
91+
'/api/atlas/v2/unauth/resourceName/{pathParam1}/{pathParam2}': {},
92+
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/resourceName3': {},
93+
'/api/atlas/v2/unauth/resourceName1/{pathParam1}/resourceName2/{pathParam2}/{pathParam3}': {},
94+
'/api/atlas/v2/unauth/{pathParam}': {},
95+
'/api/atlas/v2/unauth/{pathParam1}/{pathParam2}': {},
17296
},
17397
},
17498
errors: [

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ const validatePathStructure = (elements) => {
2020

2121
export default (input) => {
2222
const prefix = getPrefix(input);
23-
if (!prefix) return [];
23+
if (!prefix) return;
2424

2525
let suffixWithLeadingSlash = input.slice(prefix.length);
2626
if (suffixWithLeadingSlash.length === 0) {
27-
return [];
27+
return;
2828
}
2929

3030
let suffix = suffixWithLeadingSlash.slice(1);
3131
let elements = suffix.split('/');
32-
return validatePathStructure(elements) ? [] : ERROR_RESULT;
32+
if (!validatePathStructure(elements)) {
33+
return ERROR_RESULT;
34+
}
3335
};

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* Checks if a string belongs to a path parameter or a path parameter with a custom method.
3+
*
4+
* A path parameter has the format: `{paramName}`
5+
* A path parameter with a custom method has the format: `{paramName}:customMethod`
6+
*
7+
* @param {string} str - A string extracted from a path split by slashes.
8+
* @returns {boolean} True if the string matches the expected formats, false otherwise.
9+
*/
110
export function isPathParam(str) {
211
const pathParamRegEx = new RegExp(`^{[a-z][a-zA-Z0-9]*}$`);
312
const pathParamWithCustomMethodRegEx = new RegExp(`^{[a-z][a-zA-Z0-9]*}:[a-z][a-zA-Z0-9]*$`);

0 commit comments

Comments
 (0)