Comparing version 0.0.7 to 0.0.8
50
index.js
@@ -211,7 +211,12 @@ var express = require('express') | ||
function makeValidator(schema) { | ||
function makeValidator(originalSchema) { | ||
var schema = convertHeadersToLowerCase(originalSchema); | ||
return function paramValidator(req, res, next) { | ||
var errors = joi.validate(req.params, schema); | ||
var errors = joi.validate( | ||
onlySchemaFields(merge(req.params, req.query, req.headers), schema), | ||
schema | ||
); | ||
if (errors) { | ||
res.send(400, { error: errors.message }); | ||
res.send(400, { error: errors.message, details: "http://www.youtube.com/watch?v=WOdjCb4LwQY" }); | ||
} else { | ||
@@ -223,2 +228,37 @@ next(); | ||
function convertHeadersToLowerCase(schema) { | ||
var converted = copyProperties({}, schema); | ||
Object.keys(converted).forEach(function(key) { | ||
var joiType = schema[key]; | ||
if (joiType.notes === 'header') { | ||
if (key !== key.toLowerCase()) { | ||
converted[key.toLowerCase()] = joiType; | ||
delete converted[key]; | ||
} | ||
} | ||
}); | ||
return converted; | ||
} | ||
function onlySchemaFields(obj, schema) { | ||
Object.keys(obj).forEach(function(key) { | ||
if (!schema.hasOwnProperty(key)) { | ||
delete obj[key]; | ||
} | ||
}); | ||
return obj; | ||
} | ||
function copyProperties(dest, source) { | ||
Object.keys(source).forEach(function(property) { | ||
dest[property] = source[property]; | ||
}); | ||
return dest; | ||
} | ||
function merge() { | ||
var toMerge = Array.prototype.slice.call(arguments); | ||
return toMerge.reduce(copyProperties, {}); | ||
} | ||
function minOrMax(swagger, joiType, index, minOrMax) { | ||
@@ -248,3 +288,2 @@ swagger.allowableValues = swagger.allowableValues || {}; | ||
if (joiType.type === "String") { | ||
@@ -290,5 +329,4 @@ swaggerParam.dataType = "string"; | ||
function validate(schema) { | ||
var middleware = makeValidator(schema); | ||
var middleware = makeValidator(convertHeadersToLowerCase(schema)); | ||
middleware.params = convertJoiTypesToSwagger(schema); | ||
@@ -295,0 +333,0 @@ middleware.errorResponses = [ |
{ | ||
"name": "flair-doc", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Swagger-compliant REST API documentation generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -239,3 +239,6 @@ var should = require('should') | ||
.get('/pants/cheese') | ||
.expect(400, { error: "not valid" }, done); | ||
.expect(400, { | ||
error: "not valid", | ||
details: "http://www.youtube.com/watch?v=WOdjCb4LwQY" | ||
}, done); | ||
}); | ||
@@ -242,0 +245,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
45389
8
1304