|
1 | 1 | import { isCustomMethod } from './utils/resourceEvaluation.js'; |
2 | 2 |
|
3 | 3 | 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']; |
6 | 5 |
|
7 | | -export default (input, _, { documentInventory }) => { |
8 | | - if (!isCustomMethod(input)) { |
9 | | - return; |
10 | | - } |
| 6 | +export default (paths) => { |
| 7 | + // Collect all errors |
| 8 | + const errors = []; |
11 | 9 |
|
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; |
14 | 14 |
|
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 | + } |
19 | 23 |
|
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 | + } |
23 | 32 | } |
| 33 | + |
| 34 | + return errors; |
| 35 | + |
24 | 36 | }; |
0 commit comments