-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use parameter-determining method in all parameters validations (#97
) * before, it was only used in the shared validations * also, refactor validator utils into single module * also, remove checkSnakeCase module - it was replaced by caseConventionCheck
- Loading branch information
Showing
15 changed files
with
80 additions
and
114 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Expose validator utils as a module with each sub-module as a property | ||
|
||
module.exports.checkCase = require('./caseConventionCheck'); | ||
module.exports.walk = require('./walk'); | ||
module.exports.isParameterObject = require('./isParameter'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = (path, isOAS3) => { | ||
const pathsForParameters = [ | ||
'get', | ||
'put', | ||
'post', | ||
'delete', | ||
'options', | ||
'head', | ||
'patch', | ||
'trace', | ||
'components' | ||
]; | ||
|
||
const inParametersSection = path[path.length - 2] === 'parameters'; | ||
|
||
// the above check is a necessary but not sufficient check for a parameter object | ||
// use the following checks to verify the object is where a parameter is supposed to be. | ||
// without these, a schema property named "parameters" would get validated as a parameter | ||
const isParameterByPath = pathsForParameters.includes(path[path.length - 3]); | ||
const isPathItemParameter = | ||
path[path.length - 4] === 'paths' && path.length === 4; | ||
const isTopLevelParameter = | ||
!isOAS3 && path[0] === 'parameters' && path.length === 2; | ||
|
||
return ( | ||
inParametersSection && | ||
(isParameterByPath || isPathItemParameter || isTopLevelParameter) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 6 additions & 14 deletions
20
src/plugins/validation/swagger2/semantic-validators/parameters.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
// Assertation 1: | ||
// The items property for a parameter is required when its type is set to array | ||
|
||
const { isParameterObject, walk } = require('../../../utils'); | ||
|
||
module.exports.validate = function({ resolvedSpec }) { | ||
const errors = []; | ||
const warnings = []; | ||
|
||
function walk(obj, path) { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return; | ||
} | ||
walk(resolvedSpec, [], (obj, path) => { | ||
const isContentsOfParameterObject = isParameterObject(path, false); // 2nd arg is isOAS3 | ||
|
||
// 1 | ||
if (path[path.length - 2] === 'parameters') { | ||
if (isContentsOfParameterObject) { | ||
if (obj.type === 'array' && typeof obj.items !== 'object') { | ||
errors.push({ | ||
path, | ||
message: "Parameters with 'array' type require an 'items' property." | ||
}); | ||
} | ||
} | ||
|
||
if (Object.keys(obj).length) { | ||
return Object.keys(obj).map(k => walk(obj[k], [...path, k])); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
walk(resolvedSpec, []); | ||
}); | ||
|
||
return { errors, warnings }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters