express-openapi-validation
Advanced tools
Comparing version 0.6.0 to 0.6.1
62
index.js
@@ -20,4 +20,9 @@ var convert = require('openapi-jsonschema-parameters'); | ||
var schemas = convert(args.parameters); | ||
var errorTransformer = args.errorTransformer || toOpenapiValidationError; | ||
var errorTransformer = typeof args.errorTransformer === 'function' && | ||
args.errorTransformer; | ||
var errorMapper = errorTransformer ? | ||
extendedErrorMapper(errorTransformer) : | ||
toOpenapiValidationError; | ||
var bodySchema = schemas.body; | ||
var bodyValidationSchema; | ||
var headersSchema = lowercasedHeaders(schemas.headers); | ||
@@ -38,2 +43,10 @@ var pathSchema = schemas.path; | ||
if (bodySchema) { | ||
bodyValidationSchema = { | ||
properties: { | ||
body: bodySchema | ||
} | ||
}; | ||
} | ||
if (args.schemas) { | ||
@@ -45,16 +58,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; | ||
} | ||
@@ -68,3 +77,3 @@ | ||
} else if (bodySchema) { | ||
bodySchema.definitions = args.schemas; | ||
bodyValidationSchema.definitions = args.schemas; | ||
} | ||
@@ -81,3 +90,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)); | ||
@@ -115,3 +124,3 @@ } catch(e) { | ||
status: 400, | ||
errors: errors.map(errorTransformer) | ||
errors: errors.map(errorMapper) | ||
}; | ||
@@ -133,2 +142,8 @@ } else if (schemaError) { | ||
function extendedErrorMapper(mapper) { | ||
return function(jsonSchemaError) { | ||
return mapper(toOpenapiValidationError(jsonSchemaError), jsonSchemaError); | ||
}; | ||
} | ||
function lowercasedHeaders(headersSchema) { | ||
@@ -154,10 +169,23 @@ if (headersSchema) { | ||
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) { | ||
@@ -164,0 +192,0 @@ if (errors) { |
{ | ||
"name": "express-openapi-validation", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Express middleware for openapi parameter validation.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -77,8 +77,8 @@ # express-openapi-validation [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coveralls Status][coveralls-image]][coveralls-url] | ||
E.G. | ||
This function is passed 2 arguments. | ||
``` | ||
errorTransformer: function(error) { | ||
errorTransformer: function(openapiError, jsonschemaError) { | ||
return { | ||
message: error.message | ||
message: openapiError.message | ||
}; | ||
@@ -88,4 +88,11 @@ } | ||
See the error format in [jsonschema](https://www.npmjs.com/package/jsonschema). | ||
See the error format in [jsonschema](https://www.npmjs.com/package/jsonschema) for | ||
`jsonschemaError`. `openapiError`s have the following properties: | ||
* `errorCode` - A jsonschema error suffixed with `.openapi.validation`. | ||
* `location` - One of `body`, `headers`, `path`, or `query`. Signifies where validation | ||
failed. | ||
* `message` - A detailed message as to why validation failed. | ||
* `path` - The property of the location that failed validation. | ||
#### args.customFormats | ||
@@ -92,0 +99,0 @@ |
@@ -14,4 +14,4 @@ module.exports = { | ||
errorTransformer: function(error) { | ||
return 'asdf'; | ||
errorTransformer: function(openApiError, jsonSchemaError) { | ||
return arguments.length; | ||
} | ||
@@ -31,5 +31,5 @@ }, | ||
errors: [ | ||
'asdf' | ||
2 | ||
] | ||
}) | ||
}; |
35108
40
1323
136