express-openapi-validation
Advanced tools
Comparing version 0.5.3 to 0.5.4
48
index.js
@@ -26,2 +26,3 @@ var convert = require('openapi-jsonschema-parameters'); | ||
var bodySchema = schemas.body; | ||
var bodyValidationSchema; | ||
var headersSchema = lowercasedHeaders(schemas.headers); | ||
@@ -33,2 +34,10 @@ var pathSchema = schemas.path; | ||
if (bodySchema) { | ||
bodyValidationSchema = { | ||
properties: { | ||
body: bodySchema | ||
} | ||
}; | ||
} | ||
if (args.schemas) { | ||
@@ -40,16 +49,12 @@ if (Array.isArray(args.schemas)) { | ||
if (id) { | ||
var localSchemaPath; | ||
var localSchemaPath = LOCAL_DEFINITION_REGEX.exec(id); | ||
if (bodySchema) { | ||
localSchemaPath = LOCAL_DEFINITION_REGEX.exec(id); | ||
} | ||
if (localSchemaPath && bodyValidationSchema) { | ||
var definitions = bodyValidationSchema[localSchemaPath[1]]; | ||
if (localSchemaPath) { | ||
var localSchemas = bodySchema[localSchemaPath[1]]; | ||
if (!localSchemas) { | ||
localSchemas = bodySchema[localSchemaPath[1]] = {}; | ||
if (!definitions) { | ||
definitions = bodyValidationSchema[localSchemaPath[1]] = {}; | ||
} | ||
localSchemas[localSchemaPath[2]] = schema; | ||
definitions[localSchemaPath[2]] = schema; | ||
} | ||
@@ -63,3 +68,3 @@ | ||
} else if (bodySchema) { | ||
bodySchema.definitions = args.schemas; | ||
bodyValidationSchema.definitions = args.schemas; | ||
} | ||
@@ -76,3 +81,3 @@ } | ||
try { | ||
var validation = v.validate(req.body, bodySchema); | ||
var validation = v.validate({body: req.body}, bodyValidationSchema); | ||
errors.push.apply(errors, withAddedLocation('body', validation.errors)); | ||
@@ -153,10 +158,23 @@ } catch(e) { | ||
function toOpenapiValidationError(error) { | ||
return { | ||
path: error.property.replace(/^instance\.?/, '') || error.argument, | ||
return stripBodyInfo({ | ||
path: error.property.replace( | ||
error.location === 'body' ? | ||
/^instance\.body\.?/ : | ||
/^instance\.?/, '') || error.argument, | ||
errorCode: error.name + '.openapi.validation', | ||
message: error.stack, | ||
location: error.location | ||
}; | ||
}); | ||
} | ||
function stripBodyInfo(error) { | ||
if (error.location === 'body') { | ||
error.path = error.path.replace(/^body\./, ''); | ||
error.message = error.message.replace(/^instance\.body\./, 'instance.'); | ||
error.message = error.message.replace(/^instance\.body /, 'instance '); | ||
} | ||
return error; | ||
} | ||
function withAddedLocation(location, errors) { | ||
@@ -163,0 +181,0 @@ if (errors) { |
{ | ||
"name": "express-openapi-validation", | ||
"version": "0.5.3", | ||
"version": "0.5.4", | ||
"description": "Express middleware for openapi parameter validation.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
33502
38
1258