Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | 29x 29x 29x 29x 29x 29x 29x 103x 103x 103x 103x 199x 199x 199x 36x 38x 36x 36x 46x 36x 36x 8x 8x 8x 36x 37x 37x 1x 36x 36x 32x 42x 36x 36x 36x 36x 37x 7x 7x 36x 6x 6x 6x 7x 36x 6x 6x 6x 7x 2x 2x 6x 2x 2x 2x 2x 199x 36x 36x 46x 36x 7x 7x 14x 11x 7x 2x 2x 2x 4x 3x 3x 3x 199x 199x 12x 12x 52x 26x 26x 1x 187x 187x 187x 187x 187x 187x 449x 200x 249x 249x 12x 103x | // Assertation 1. If a path has a parameter, all operations must have a parameter of type // 'path' and name 'parameterName' ( parameterName matching what is contained in curly brackets -> {} ) // Assertation 2. All path parameters must be defined at either the path or operation level. // Assertation 3. All path segments are lower snake case // Assertation 4: // Identical path parameters should be defined at path level rather than in the operations const flatten = require('lodash/flatten'); const isEqual = require('lodash/isEqual'); const uniqWith = require('lodash/uniqWith'); const MessageCarrier = require('../../../utils/messageCarrier'); const { checkCase } = require('../../../utils'); const allowedOperations = [ 'get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace' ]; module.exports.validate = function({ resolvedSpec }, config) { const messages = new MessageCarrier(); config = config.paths; const pathNames = resolvedSpec.paths ? Object.keys(resolvedSpec.paths) : []; pathNames.forEach(pathName => { // get all path parameters contained in curly brackets const regex = /\{(.*?)\}/g; let parameters = pathName.match(regex); // there are path parameters, check each operation to make sure they are defined if (parameters) { // parameter strings will still have curly braces around them // from regex match - take them out parameters = parameters.map(param => { return param.slice(1, -1); }); const path = resolvedSpec.paths[pathName]; const operations = Object.keys(path).filter(pathItem => allowedOperations.includes(pathItem) ); // paths can have a global parameters object that applies to all operations let globalParameters = []; if (path.parameters) { globalParameters = path.parameters .filter(param => param.in && param.in.toLowerCase() === 'path') .map(param => param.name); } operations.forEach(opName => { const operation = path[opName]; // ignore validating excluded operations if (operation['x-sdk-exclude'] === true) { return; } // get array of 'names' for parameters of type 'path' in the operation let givenParameters = []; if (operation.parameters) { givenParameters = operation.parameters .filter(param => param.in && param.in.toLowerCase() === 'path') .map(param => param.name); } let accountsForAllParameters = true; const missingParameters = []; parameters.forEach(name => { if ( !givenParameters.includes(name) && !globalParameters.includes(name) ) { accountsForAllParameters = false; missingParameters.push(name); } }); if (!accountsForAllParameters) { const checkStatus = config.missing_path_parameter; Eif (checkStatus != 'off') { missingParameters.forEach(name => { messages.addMessage( ['paths', pathName, opName, 'parameters'], `Operation must include a path parameter with name: ${name}.`, checkStatus, 'missing_path_parameter' ); }); } } }); if (!operations.length) { let accountsForAllParameters = true; const missingParameters = []; parameters.forEach(name => { if (!globalParameters.includes(name)) { accountsForAllParameters = false; missingParameters.push(name); } }); if (!accountsForAllParameters) { const checkStatus = config.missing_path_parameter; Eif (checkStatus != 'off') { missingParameters.forEach(name => { messages.addMessage( ['paths', pathName], `Path parameter must be defined at the path or the operation level: ${name}.`, checkStatus, 'missing_path_parameter' ); }); } } } } // Assertation 4 // Check that parameters are not defined redundantly in the operations if (parameters) { const pathObj = resolvedSpec.paths[pathName]; const operationKeys = Object.keys(pathObj).filter( op => allowedOperations.indexOf(op) > -1 ); if (operationKeys.length > 1) { parameters.forEach(parameter => { const operationPathParams = uniqWith( flatten( operationKeys.map(op => pathObj[op].parameters).filter(Boolean) ).filter(p => p.name === parameter), isEqual ); if (operationPathParams.length === 1) { // All definitions of this path param are the same in the operations const checkStatus = config.duplicate_path_parameter || 'off'; Eif (checkStatus.match('error|warning')) { operationKeys.forEach(op => { if (!pathObj[op].parameters) return; const index = pathObj[op].parameters.findIndex( p => p.name === parameter ); messages.addMessage( ['paths', pathName, op, 'parameters', `${index}`], 'Common path parameters should be defined on path object', checkStatus, 'duplicate_path_parameter' ); }); } } }); } } // enforce path segments are lower snake case const checkStatus = config.snake_case_only; if (checkStatus != 'off') { const segments = pathName.split('/'); segments.forEach(segment => { // the first element will be "" since pathName starts with "/" // also, ignore validating the path parameters if (segment === '' || segment[0] === '{') { return; } if (!checkCase(segment, 'lower_snake_case')) { messages.addMessage( ['paths', pathName], `Path segments must be lower snake case.`, checkStatus, 'snake_case_only' ); } }); } else { // in the else block because usage of paths_case_convention is mutually // exclusive with usage of config.snake_case_only since it is overlapping // functionality Eif (config.paths_case_convention) { const checkStatusPath = config.paths_case_convention[0]; Eif (checkStatusPath !== 'off') { const caseConvention = config.paths_case_convention[1]; const segments = pathName.split('/'); segments.forEach(segment => { // the first element will be "" since pathName starts with "/" // also, ignore validating the path parameters if (segment === '' || segment[0] === '{') { return; } const isCorrectCase = checkCase(segment, caseConvention); if (!isCorrectCase) { messages.addMessage( ['paths', pathName], `Path segments must follow case convention: ${caseConvention}`, checkStatusPath, 'paths_case_convention' ); } }); } } } }); return messages; }; |