Skip to content

Commit 1d0d868

Browse files
address the comments
1 parent 60c888c commit 1d0d868

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

tools/spectral/ipa/rulesets/IPA-109.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ rules:
1111
severity: warn
1212
given: '$.paths'
1313
then:
14-
field: '@key'
1514
function: 'eachCustomMethodMustBeGetOrPost'
Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
import { isCustomMethod } from './utils/resourceEvaluation.js';
22

33
const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.';
4-
const ERROR_RESULT = [{ message: ERROR_MESSAGE }];
5-
const validMethods = ['get', 'post'];
4+
const VALID_METHODS = ['get', 'post'];
65

7-
export default (input, _, { documentInventory }) => {
8-
if (!isCustomMethod(input)) {
9-
return;
10-
}
6+
export default (paths) => {
7+
// Collect all errors
8+
const errors = [];
119

12-
const oas = documentInventory.resolved;
13-
let httpMethods = Object.keys(oas.paths[input]);
10+
// Iterate through each path key and its corresponding path item
11+
for (const [pathKey, pathItem] of Object.entries(paths)) {
12+
// Skip if not a custom method
13+
if (!isCustomMethod(pathKey)) continue;
1414

15-
const invalidMethodsFound = httpMethods.some((key) => !validMethods.includes(key));
16-
if (invalidMethodsFound) {
17-
return ERROR_RESULT;
18-
}
15+
// Get HTTP methods for this path
16+
const httpMethods = Object.keys(pathItem);
17+
18+
// Check for invalid methods
19+
if (httpMethods.some(method => !VALID_METHODS.includes(method))) {
20+
errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE });
21+
continue;
22+
}
1923

20-
const validMethodsFound = httpMethods.filter((key) => validMethods.includes(key));
21-
if (validMethodsFound.length > 1) {
22-
return ERROR_RESULT;
24+
// Check for multiple valid methods
25+
const validMethodCount = httpMethods.filter(method =>
26+
VALID_METHODS.includes(method)
27+
).length;
28+
29+
if (validMethodCount > 1) {
30+
errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE });
31+
}
2332
}
33+
34+
return errors;
35+
2436
};

0 commit comments

Comments
 (0)