diff --git a/src/middlewares/openapi.request.validator.ts b/src/middlewares/openapi.request.validator.ts index dd3be52f..996a2279 100644 --- a/src/middlewares/openapi.request.validator.ts +++ b/src/middlewares/openapi.request.validator.ts @@ -96,7 +96,7 @@ export class RequestValidator { const requiredAdds = []; if (requestBody?.hasOwnProperty('content')) { const reqBodyObject = requestBody; - body = this.requestBodyToSchema(path, contentType, reqBodyObject); + body = RequestBody.toSchema(this.ajv, path, contentType, reqBodyObject); if (reqBodyObject.required) requiredAdds.push('body'); } @@ -152,8 +152,6 @@ export class RequestValidator { req.params = openapi.pathParams ?? req.params; } - // (req).schema = schema; - /** * support json in request params, query, headers and cookies * like this filter={"type":"t-shirt","color":"blue"} @@ -237,8 +235,11 @@ export class RequestValidator { } } } +} - private requestBodyToSchema( +class RequestBody { + static toSchema( + ajv: Ajv, path: string, contentType: ContentType, requestBody: OpenAPIV3.RequestBodyObject, @@ -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 { @@ -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 &&