|
1 | | -const operationKeys = ["get", "post", "put", "delete", "options", "head", "patch"] |
2 | | - |
3 | | -const PATH_TEMPLATES_REGEX = /\{(.*?)\}/g |
4 | | - |
5 | | -export const validatePathParameterDeclarationHasMatchingDefiniton = () => async system => { |
6 | | - const nodes = await system.validateSelectors.allPathItems() |
7 | | - |
8 | | - return nodes.reduce(async (prev, node) => { |
9 | | - const acc = await prev |
10 | | - const pathTemplates = (node.key.match(PATH_TEMPLATES_REGEX) || []) |
11 | | - .map(str => str.replace("{", "").replace("}", "")) |
12 | | - if(pathTemplates.length) { |
13 | | - for (let paramName of pathTemplates) { |
14 | | - if(paramName.length === 0) { |
15 | | - // don't validate empty param names... they're invalid anyway |
16 | | - continue |
17 | | - } |
18 | | - const resolverResult = await system.fn.memoizedResolveSubtree(system.specSelectors.specJson(), node.path) |
19 | | - const res = checkForDefinition(paramName, resolverResult.spec) |
20 | | - if(res.inOperation && res.missingFromOperations.length) { |
21 | | - const missingStr = res.missingFromOperations |
22 | | - .map(str => `"${str}"`) |
23 | | - .join(", ") |
24 | | - |
25 | | - acc.push({ |
26 | | - message: `Declared path parameter "${paramName}" needs to be defined within every operation in the path (missing in ${missingStr}), or moved to the path-level parameters object`, |
27 | | - path: [...node.path], |
28 | | - level: "error", |
29 | | - }) |
30 | | - } else if(!res.found) { |
31 | | - acc.push({ |
32 | | - message: `Declared path parameter "${paramName}" needs to be defined as a path parameter at either the path or operation level`, |
33 | | - path: [...node.path], |
34 | | - level: "error", |
35 | | - }) |
36 | | - } |
37 | | - } |
38 | | - } |
39 | | - return acc |
40 | | - }, Promise.resolve([])) |
41 | | -} |
| 1 | +import { |
| 2 | + PATH_TEMPLATES_REGEX |
| 3 | +} from "./helpers" |
42 | 4 |
|
43 | 5 | export const validatePathParameterDeclarationIsNotEmpty = () => system => { |
44 | 6 | return system.validateSelectors |
@@ -81,53 +43,3 @@ export const validatePathParameterKeysAreDifferent = () => system => { |
81 | 43 | }, []) |
82 | 44 | }) |
83 | 45 | } |
84 | | - |
85 | | -/// Helpers |
86 | | - |
87 | | -function checkForDefinition(paramName, pathItem) { |
88 | | - const pathItemParameters = pathItem.parameters |
89 | | - const operationsInPathItem = (Object.keys(pathItem) || []) |
90 | | - .filter(key => operationKeys.indexOf(key) > -1) |
91 | | - .map(key => { |
92 | | - const obj = pathItem[key] |
93 | | - obj.method = key |
94 | | - return obj |
95 | | - }) |
96 | | - |
97 | | - const res = { |
98 | | - found: false, |
99 | | - inPath: false, |
100 | | - inOperation: false, |
101 | | - missingFromOperations: [] |
102 | | - } |
103 | | - |
104 | | - // Look at the path parameters |
105 | | - if(Array.isArray(pathItemParameters)) { |
106 | | - pathItemParameters.forEach(param => { |
107 | | - if(param.name === paramName && param.in === "path") { |
108 | | - res.found = true |
109 | | - res.inPath = true |
110 | | - } |
111 | | - }) |
112 | | - } |
113 | | - |
114 | | - // Next, look at the operations... |
115 | | - if(!res.found && operationsInPathItem.length) { |
116 | | - operationsInPathItem |
117 | | - .forEach(op => { |
118 | | - const inThisOperation = (op.parameters || []) |
119 | | - .some(param => param.name === paramName && param.in === "path") |
120 | | - |
121 | | - if(inThisOperation) { |
122 | | - res.found = true |
123 | | - res.inOperation = true |
124 | | - } |
125 | | - |
126 | | - if(!inThisOperation) { |
127 | | - res.missingFromOperations.push(op.method) |
128 | | - } |
129 | | - }) |
130 | | - } |
131 | | - |
132 | | - return res |
133 | | -} |
0 commit comments