swagger-tools
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -49,3 +49,3 @@ /* | ||
*/ | ||
exports = module.exports = function swaggerMiddleware (resourceList, resources) { | ||
exports = module.exports = function swaggerMetadataMiddleware (resourceList, resources) { | ||
if (_.isUndefined(resourceList)) { | ||
@@ -113,51 +113,55 @@ throw new Error('resourceList is required'); | ||
if (!_.isUndefined(swaggerMetadata.operation)) { | ||
_.each(swaggerMetadata.operation.parameters, function (param) { | ||
var val; | ||
try { | ||
_.each(swaggerMetadata.operation.parameters, function (param) { | ||
var val; | ||
// Get the value to validate based on the operation parameter type | ||
switch (param.paramType) { | ||
case 'body': | ||
case 'form': | ||
if (!req.body) { | ||
return next('Server configuration error: req.body is not defined but is required'); | ||
} | ||
// Get the value to validate based on the operation parameter type | ||
switch (param.paramType) { | ||
case 'body': | ||
case 'form': | ||
if (!req.body) { | ||
throw new Error('Server configuration error: req.body is not defined but is required'); | ||
} | ||
val = req.body[param.name]; | ||
val = req.body[param.name]; | ||
break; | ||
case 'header': | ||
val = req.headers[param.name]; | ||
break; | ||
case 'header': | ||
val = req.headers[param.name]; | ||
break; | ||
case 'path': | ||
_.each(api.keys, function (key, index) { | ||
if (key.name === param.name) { | ||
val = match[index + 1]; | ||
break; | ||
case 'path': | ||
_.each(api.keys, function (key, index) { | ||
if (key.name === param.name) { | ||
val = match[index + 1]; | ||
} | ||
}); | ||
break; | ||
case 'query': | ||
if (!req.query) { | ||
throw new Error('Server configuration error: req.query is not defined but is required'); | ||
} | ||
}); | ||
break; | ||
case 'query': | ||
if (!req.query) { | ||
return next('Server configuration error: req.query is not defined but is required'); | ||
val = req.query[param.name]; | ||
break; | ||
} | ||
val = req.query[param.name]; | ||
// Use the default value when necessary | ||
if (_.isUndefined(val) && !_.isUndefined(param.defaultValue)) { | ||
val = param.defaultValue; | ||
} | ||
break; | ||
} | ||
swaggerMetadata.params[param.name] = { | ||
schema: param, | ||
value: val | ||
}; | ||
}); | ||
// Use the default value when necessary | ||
if (_.isUndefined(val) && !_.isUndefined(param.defaultValue)) { | ||
val = param.defaultValue; | ||
} | ||
swaggerMetadata.params[param.name] = { | ||
schema: param, | ||
value: val | ||
}; | ||
}); | ||
// Attach Swagger metadata to the request | ||
req.swagger = swaggerMetadata; | ||
// Attach Swagger metadata to the request | ||
req.swagger = swaggerMetadata; | ||
} catch (err) { | ||
return next(err.message); | ||
} | ||
} | ||
@@ -164,0 +168,0 @@ |
@@ -54,4 +54,6 @@ /* | ||
var stubHandler = function stubHandler (req, res) { | ||
res.end('OK'); | ||
var createStubHandler = function createStubHandler (req, res, msg) { | ||
return function stubHandler (req, res) { | ||
res.end(msg); | ||
}; | ||
}; | ||
@@ -102,3 +104,4 @@ | ||
if (_.isUndefined(handler) && options.useStubs === true) { | ||
handler = handlerCache[operation.nickname] = stubHandler; | ||
handler = handlerCache[operation.nickname] = createStubHandler(req, res, | ||
'Stubbed response for ' + operation.nickname); | ||
} | ||
@@ -105,0 +108,0 @@ |
@@ -130,7 +130,2 @@ /* | ||
var contentType = req.headers['content-type'] || 'application/octet-stream'; | ||
var returnError = function returnError (message, status) { | ||
res.status = _.isUndefined(status) ? 500 : status; | ||
return next(message); | ||
}; | ||
var operation = req.swagger ? req.swagger.operation : undefined; | ||
@@ -142,4 +137,3 @@ | ||
if (operation.consumes.indexOf(contentType) === -1) { | ||
return returnError('Invalid content type (' + contentType + '). These are valid: ' + | ||
operation.consumes.join(', ')); | ||
return next('Invalid content type (' + contentType + '). These are valid: ' + operation.consumes.join(', ')); | ||
} | ||
@@ -149,80 +143,83 @@ } | ||
// Validate the parameters | ||
_.each(operation.parameters || [], function (param) { | ||
var minimum = param.minimum; | ||
var maximum = param.maximum; | ||
var invalidParamPrefix = 'Parameter (' + param.name + ') '; | ||
var invalidTypePrefix = invalidParamPrefix + 'is not a valid '; | ||
var testVal; | ||
var val = req.swagger.params[param.name].value; | ||
try { | ||
_.each(operation.parameters || [], function (param) { | ||
var minimum = param.minimum; | ||
var maximum = param.maximum; | ||
var invalidParamPrefix = 'Parameter (' + param.name + ') '; | ||
var invalidTypePrefix = invalidParamPrefix + 'is not a valid '; | ||
var testVal; | ||
var val = req.swagger.params[param.name].value; | ||
// Validate requiredness | ||
if (!_.isUndefined(param.required)) { | ||
if (param.required === true && _.isUndefined(val)) { | ||
return returnError(invalidParamPrefix + 'is required', 400); | ||
// Validate requiredness | ||
if (!_.isUndefined(param.required)) { | ||
if (param.required === true && _.isUndefined(val)) { | ||
throw new Error(invalidParamPrefix + 'is required'); | ||
} | ||
} | ||
} | ||
// Validate the value type/format | ||
if (!isValid(val, param.type, param.format)) { | ||
return returnError(invalidTypePrefix + (_.isUndefined(param.format) ? '' : param.format + ' ') + param.type + | ||
': ' + val, 400); | ||
} | ||
// Validate the value type/format | ||
if (!isValid(val, param.type, param.format)) { | ||
throw new Error(invalidTypePrefix + (_.isUndefined(param.format) ? '' : param.format + ' ') + | ||
param.type + ': ' + val); | ||
} | ||
if (param.type === 'integer') { | ||
testVal = parseInt(val, 10); | ||
} else if (param.type === 'number') { | ||
testVal = parseFloat(val); | ||
} | ||
// Validate enum | ||
if (!_.isUndefined(param.enum) && param.enum.indexOf(val) === -1) { | ||
return returnError(invalidParamPrefix + 'is not an allowable value (' + param.enum.join(', ') + '): ' + val, | ||
400); | ||
} | ||
// Validate maximum | ||
if (!_.isUndefined(maximum)) { | ||
if (!_.isNumber(maximum)) { | ||
maximum = parseFloat(maximum); | ||
if (param.type === 'integer') { | ||
testVal = parseInt(val, 10); | ||
} else if (param.type === 'number') { | ||
testVal = parseFloat(val); | ||
} | ||
if (testVal > maximum) { | ||
return returnError(invalidParamPrefix + 'is greater than the configured maximum (' + param.maximum + '): ' + | ||
val, 400); | ||
// Validate enum | ||
if (!_.isUndefined(param.enum) && param.enum.indexOf(val) === -1) { | ||
throw new Error(invalidParamPrefix + 'is not an allowable value (' + param.enum.join(', ') + '): ' + val); | ||
} | ||
} | ||
// Validate minimum | ||
if (!_.isUndefined(minimum)) { | ||
if (!_.isNumber(minimum)) { | ||
minimum = parseFloat(minimum); | ||
// Validate maximum | ||
if (!_.isUndefined(maximum)) { | ||
if (!_.isNumber(maximum)) { | ||
maximum = parseFloat(maximum); | ||
} | ||
if (testVal > maximum) { | ||
throw new Error(invalidParamPrefix + 'is greater than the configured maximum (' + param.maximum + | ||
'): ' + val); | ||
} | ||
} | ||
if (testVal < minimum) { | ||
return returnError(invalidParamPrefix + 'is less than the configured minimum (' + param.minimum + '): ' + | ||
val, 400); | ||
// Validate minimum | ||
if (!_.isUndefined(minimum)) { | ||
if (!_.isNumber(minimum)) { | ||
minimum = parseFloat(minimum); | ||
} | ||
if (testVal < minimum) { | ||
throw new Error(invalidParamPrefix + 'is less than the configured minimum (' + param.minimum + '): ' + | ||
val); | ||
} | ||
} | ||
} | ||
// Validate array | ||
if (param.type === 'array') { | ||
try { | ||
val.forEach(function (aVal, index) { | ||
if (!isValid(aVal, param.items.type, param.format)) { | ||
throw Error(invalidParamPrefix + 'at index ' + index + ' is not a valid ' + param.items.type + ': ' + | ||
aVal); | ||
} | ||
}); | ||
} catch (err) { | ||
return returnError(err.message); | ||
// Validate array | ||
if (param.type === 'array') { | ||
try { | ||
val.forEach(function (aVal, index) { | ||
if (!isValid(aVal, param.items.type, param.format)) { | ||
throw Error(invalidParamPrefix + 'at index ' + index + ' is not a valid ' + param.items.type + ': ' + | ||
aVal); | ||
} | ||
}); | ||
} catch (err) { | ||
throw new Error(err.message); | ||
} | ||
} | ||
} | ||
// Validate uniqueItems | ||
if (!_.isUndefined(param.uniqueItems)) { | ||
if (_.uniq(val).length !== val.length) { | ||
return returnError(invalidParamPrefix + 'does not allow duplicate values: ' + val.join(', '), 400); | ||
// Validate uniqueItems | ||
if (!_.isUndefined(param.uniqueItems)) { | ||
if (_.uniq(val).length !== val.length) { | ||
throw new Error(invalidParamPrefix + 'does not allow duplicate values: ' + val.join(', ')); | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
} catch (err) { | ||
return next(err.message); | ||
} | ||
} | ||
@@ -229,0 +226,0 @@ |
{ | ||
"name": "swagger-tools", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"description": "Various tools for using and integrating with Swagger.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "gulp test" | ||
"test": "./node_modules/gulp/bin/gulp.js" | ||
}, | ||
@@ -40,8 +40,8 @@ "author": { | ||
"devDependencies": { | ||
"body-parser": "^1.4.3", | ||
"connect": "^3.0.2", | ||
"gulp": "^3.8.5", | ||
"gulp-jshint": "^1.6.2", | ||
"gulp-mocha": "^0.4.1", | ||
"jshint-stylish": "^0.2.0", | ||
"body-parser": "^1.5.2", | ||
"connect": "^3.1.0", | ||
"gulp": "^3.8.6", | ||
"gulp-jshint": "^1.8.0", | ||
"gulp-mocha": "^0.5.2", | ||
"jshint-stylish": "^0.4.0", | ||
"qs": "^0.6.6", | ||
@@ -53,7 +53,7 @@ "string": "^1.9.0", | ||
"jjv": "^1.0.0", | ||
"jjve": "^0.2.1", | ||
"jjve": "^0.4.0", | ||
"lodash": "^2.4.1", | ||
"parseurl": "^1.1.3", | ||
"parseurl": "^1.2.0", | ||
"path-to-regexp": "^0.2.3" | ||
} | ||
} |
@@ -5,2 +5,7 @@ The project provides various tools for integrating and interacting with Swagger. This project is in its infancy but | ||
## Project Badges | ||
* [Travis](https://travis-ci.org/) build status: [![Build Status](https://img.shields.io/travis/apigee-127/swagger-tools.svg)](https://travis-ci.org/apigee-127/swagger-tools) | ||
* [NPM](https://www.npmjs.org) dependencies: [![Dependencies](http://img.shields.io/david/apigee-127/swagger-tools.svg)](https://david-dm.org/apigee-127/swagger-tools) | ||
* [NPM](https://www.npmjs.org) developer dependencies: [![Dev Dependencies](http://img.shields.io/david/dev/apigee-127/swagger-tools.svg)](https://david-dm.org/apigee-127/swagger-tools#info=devDependencies&view=table) | ||
## Supported Swagger Versions | ||
@@ -7,0 +12,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82090
1776
90
+ Addedjjve@0.4.0(transitive)
- Removedjjve@0.2.1(transitive)
Updatedjjve@^0.4.0
Updatedparseurl@^1.2.0