@@ -21,6 +21,7 @@ import {
21
21
ContentType ,
22
22
ajvErrorsToValidatorError ,
23
23
augmentAjvErrors ,
24
+ useAjvCache ,
24
25
} from './util' ;
25
26
26
27
type OperationObject = OpenAPIV3 . OperationObject ;
@@ -29,7 +30,6 @@ type ReferenceObject = OpenAPIV3.ReferenceObject;
29
30
type SecurityRequirementObject = OpenAPIV3 . SecurityRequirementObject ;
30
31
type SecuritySchemeObject = OpenAPIV3 . SecuritySchemeObject ;
31
32
type ApiKeySecurityScheme = OpenAPIV3 . ApiKeySecurityScheme ;
32
-
33
33
export class RequestValidator {
34
34
private middlewareCache : { [ key : string ] : RequestHandler } = { } ;
35
35
private apiDoc : OpenAPIV3 . DocumentV3 | OpenAPIV3 . DocumentV3_1 ;
@@ -80,7 +80,7 @@ export class RequestValidator {
80
80
const key = `${ req . method } -${ path } -${ contentTypeKey } ` ;
81
81
82
82
if ( ! this . middlewareCache [ key ] ) {
83
- const middleware = this . buildMiddleware ( path , reqSchema , contentType ) ;
83
+ const middleware = this . buildMiddleware ( path , reqSchema , contentType , key ) ;
84
84
this . middlewareCache [ key ] = middleware ;
85
85
}
86
86
return this . middlewareCache [ key ] ( req , res , next ) ;
@@ -104,6 +104,7 @@ export class RequestValidator {
104
104
path : string ,
105
105
reqSchema : OperationObject ,
106
106
contentType : ContentType ,
107
+ ajvCacheKey : string
107
108
) : RequestHandler {
108
109
const apiDoc = this . apiDoc ;
109
110
const schemaParser = new ParametersSchemaParser ( this . ajv , apiDoc ) ;
@@ -113,7 +114,7 @@ export class RequestValidator {
113
114
const validator = new Validator ( this . apiDoc , parameters , body , {
114
115
general : this . ajv ,
115
116
body : this . ajvBody ,
116
- } ) ;
117
+ } , ajvCacheKey ) ;
117
118
118
119
const allowUnknownQueryParameters = ! ! (
119
120
reqSchema [ 'x-eov-allow-unknown-query-parameters' ] ??
@@ -330,6 +331,7 @@ class Validator {
330
331
general : Ajv ;
331
332
body : Ajv ;
332
333
} ,
334
+ ajvCacheKey : string
333
335
) {
334
336
this . apiDoc = apiDoc ;
335
337
this . schemaGeneral = this . _schemaGeneral ( parametersSchema ) ;
@@ -338,8 +340,8 @@ class Validator {
338
340
...( < any > this . schemaGeneral ) . properties , // query, header, params props
339
341
body : ( < any > this . schemaBody ) . properties . body , // body props
340
342
} ;
341
- this . validatorGeneral = ajv . general . compile ( this . schemaGeneral ) ;
342
- this . validatorBody = ajv . body . compile ( this . schemaBody ) ;
343
+ this . validatorGeneral = useAjvCache ( ajv . general , this . schemaGeneral , ajvCacheKey ) ;
344
+ this . validatorBody = useAjvCache ( ajv . body , this . schemaBody , ajvCacheKey ) ;
343
345
}
344
346
345
347
private _schemaGeneral ( parameters : ParametersSchema ) : object {
0 commit comments