Skip to content

Commit

Permalink
handle unsupported content type in body
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Jul 19, 2019
1 parent 7143645 commit 42dd032
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/middlewares/openapi.request.validator.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class RequestValidator {

const parameters = this.parametersToSchema(path, pathSchema.parameters);
const requestBody = pathSchema.requestBody;
let body = this.requestBodyToSchema(path, requestBody);
let body = this.requestBodyToSchema(req, requestBody);
let requiredAdds = requestBody && requestBody.required ? ['body'] : [];
const schema = {
// $schema: "http://json-schema.org/draft-04/schema#",
Expand Down Expand Up @@ -211,12 +211,19 @@ export class RequestValidator {
};
}

private requestBodyToSchema(path, requestBody) {
const content =
(requestBody && requestBody.content && requestBody.content[TYPE_JSON]) ||
{};
const schema = content.schema;
return schema || {};
private requestBodyToSchema(req, requestBody: any = {}) {
if (requestBody.content) {
const type = req.headers['content-type']; // || TYPE_JSON;
const content = requestBody.content[type];
if (!content)
{
const message = `Unsupported Content-Type ${type}`;
const err = validationError(415, req.path, message);
throw ono(err, message)
}
return content.schema || {}
}
return {};
}

private parametersToSchema(path, parameters = []) {
Expand Down

0 comments on commit 42dd032

Please sign in to comment.