@@ -21,6 +21,7 @@ import {
2121  ContentType , 
2222  ajvErrorsToValidatorError , 
2323  augmentAjvErrors , 
24+   useAjvCache , 
2425}  from  './util' ; 
2526
2627type  OperationObject  =  OpenAPIV3 . OperationObject ; 
@@ -29,7 +30,6 @@ type ReferenceObject = OpenAPIV3.ReferenceObject;
2930type  SecurityRequirementObject  =  OpenAPIV3 . SecurityRequirementObject ; 
3031type  SecuritySchemeObject  =  OpenAPIV3 . SecuritySchemeObject ; 
3132type  ApiKeySecurityScheme  =  OpenAPIV3 . ApiKeySecurityScheme ; 
32- 
3333export  class  RequestValidator  { 
3434  private  middlewareCache : {  [ key : string ] : RequestHandler  }  =  { } ; 
3535  private  apiDoc : OpenAPIV3 . DocumentV3  |  OpenAPIV3 . DocumentV3_1 ; 
@@ -80,7 +80,7 @@ export class RequestValidator {
8080    const  key  =  `${ req . method } ${ path } ${ contentTypeKey }  ; 
8181
8282    if  ( ! this . middlewareCache [ key ] )  { 
83-       const  middleware  =  this . buildMiddleware ( path ,  reqSchema ,  contentType ) ; 
83+       const  middleware  =  this . buildMiddleware ( path ,  reqSchema ,  contentType ,   key ) ; 
8484      this . middlewareCache [ key ]  =  middleware ; 
8585    } 
8686    return  this . middlewareCache [ key ] ( req ,  res ,  next ) ; 
@@ -104,6 +104,7 @@ export class RequestValidator {
104104    path : string , 
105105    reqSchema : OperationObject , 
106106    contentType : ContentType , 
107+     ajvCacheKey : string 
107108  ) : RequestHandler  { 
108109    const  apiDoc  =  this . apiDoc ; 
109110    const  schemaParser  =  new  ParametersSchemaParser ( this . ajv ,  apiDoc ) ; 
@@ -113,7 +114,7 @@ export class RequestValidator {
113114    const  validator  =  new  Validator ( this . apiDoc ,  parameters ,  body ,  { 
114115      general : this . ajv , 
115116      body : this . ajvBody , 
116-     } ) ; 
117+     } ,   ajvCacheKey ) ; 
117118
118119    const  allowUnknownQueryParameters  =  ! ! ( 
119120      reqSchema [ 'x-eov-allow-unknown-query-parameters' ]  ?? 
@@ -330,6 +331,7 @@ class Validator {
330331      general : Ajv ; 
331332      body : Ajv ; 
332333    } , 
334+     ajvCacheKey : string 
333335  )  { 
334336    this . apiDoc  =  apiDoc ; 
335337    this . schemaGeneral  =  this . _schemaGeneral ( parametersSchema ) ; 
@@ -338,8 +340,8 @@ class Validator {
338340      ...( < any > this . schemaGeneral ) . properties ,  // query, header, params props 
339341      body : ( < any > this . schemaBody ) . properties . body ,  // body props 
340342    } ; 
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 ) ; 
343345  } 
344346
345347  private  _schemaGeneral ( parameters : ParametersSchema ) : object  { 
0 commit comments