|
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 }]; |
4 | 5 | const VALID_METHODS = ['get', 'post']; |
5 | 6 |
|
6 | | -export default (paths) => { |
7 | | - // Collect all errors |
8 | | - const errors = []; |
| 7 | +export default (input, opts, { path }) => { |
| 8 | + // Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath. |
| 9 | + let pathKey = path[1]; |
9 | 10 |
|
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; |
| 11 | + if (!isCustomMethod(pathKey)) return; |
14 | 12 |
|
15 | | - // Get HTTP methods for this path |
16 | | - const httpMethods = Object.keys(pathItem); |
| 13 | + const httpMethods = Object.keys(input); |
17 | 14 |
|
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 | | - } |
| 15 | + // Check for invalid methods |
| 16 | + if (httpMethods.some((method) => !VALID_METHODS.includes(method))) { |
| 17 | + return ERROR_RESULT; |
| 18 | + } |
23 | 19 |
|
24 | | - // Check for multiple valid methods |
25 | | - const validMethodCount = httpMethods.filter((method) => VALID_METHODS.includes(method)).length; |
| 20 | + // Check for multiple valid methods |
| 21 | + const validMethodCount = httpMethods.filter((method) => VALID_METHODS.includes(method)).length; |
26 | 22 |
|
27 | | - if (validMethodCount > 1) { |
28 | | - errors.push({ path: ['paths', pathKey], message: ERROR_MESSAGE }); |
29 | | - } |
| 23 | + if (validMethodCount > 1) { |
| 24 | + return ERROR_RESULT; |
30 | 25 | } |
31 | | - |
32 | | - return errors; |
33 | 26 | }; |
0 commit comments