Skip to content

Commit

Permalink
extract request body to schema logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 23, 2019
1 parent b4c901a commit c835988
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class RequestValidator {
const requiredAdds = [];
if (requestBody?.hasOwnProperty('content')) {
const reqBodyObject = <OpenAPIV3.RequestBodyObject>requestBody;
body = this.requestBodyToSchema(path, contentType, reqBodyObject);
body = RequestBody.toSchema(this.ajv, path, contentType, reqBodyObject);
if (reqBodyObject.required) requiredAdds.push('body');
}

Expand Down Expand Up @@ -152,8 +152,6 @@ export class RequestValidator {
req.params = openapi.pathParams ?? req.params;
}

// (<any>req).schema = schema;

/**
* support json in request params, query, headers and cookies
* like this filter={"type":"t-shirt","color":"blue"}
Expand Down Expand Up @@ -237,8 +235,11 @@ export class RequestValidator {
}
}
}
}

private requestBodyToSchema(
class RequestBody {
static toSchema(
ajv: Ajv,
path: string,
contentType: ContentType,
requestBody: OpenAPIV3.RequestBodyObject,
Expand All @@ -258,13 +259,18 @@ export class RequestValidator {
throw validationError(415, path, msg);
}

const schema = this.cleanseContentSchema(contentType, requestBody);
const schema = RequestBody.cleanseContentSchema(
ajv,
contentType,
requestBody,
);
return schema ?? content.schema ?? {};
}
return {};
}

private cleanseContentSchema(
private static cleanseContentSchema(
ajv: Ajv,
contentType: ContentType,
requestBody: OpenAPIV3.RequestBodyObject,
): object {
Expand All @@ -274,7 +280,7 @@ export class RequestValidator {

let bodyContentRefSchema = null;
if (bodyContentSchema && '$ref' in bodyContentSchema) {
const objectSchema = this.ajv.getSchema(bodyContentSchema.$ref);
const objectSchema = ajv.getSchema(bodyContentSchema.$ref);
bodyContentRefSchema =
objectSchema &&
objectSchema.schema &&
Expand Down

0 comments on commit c835988

Please sign in to comment.