@@ -74,36 +74,36 @@ export class AjvOpenApiValidator implements OpenApiValidator {
7474 // always disable removeAdditional, because it has unexpected results with allOf
7575 this . ajv = new AjvDraft4 ( { ...ajvOpts , removeAdditional : false } )
7676 this . validatorOpts = validatorOpts ? { ...DEFAULT_VALIDATOR_OPTS , ...validatorOpts } : DEFAULT_VALIDATOR_OPTS
77+ if ( this . validatorOpts . log == undefined ) {
78+ this . validatorOpts . log = ( ) => { }
79+ }
80+
7781 this . initialize ( spec )
7882 }
7983
8084 validateResponseBody (
8185 path : string ,
8286 method : ValidatorHttpMethod ,
8387 status : string ,
84- data : unknown ,
85- strict : boolean ) : ErrorObj [ ] | undefined {
86- const validator = this . responseBodyValidators . find ( ( v ) => v . path === path && v . method === method && v . status === status ) ?. validator
88+ data : unknown ) : ErrorObj [ ] | undefined {
89+ const validator = this . responseBodyValidators . find ( ( v ) => v . path === path ?. toLowerCase ( ) && v . method === method ?. toLowerCase ( ) && v . status === status + '' ) ?. validator
8790 if ( validator ) {
8891 return this . validateBody ( validator , data )
89- } else if ( strict ) {
90- throw new Error ( `No validator found for '${ method } ${ path } '` )
92+ } else {
93+ throw new Error ( `No response body validator found for '${ method } ', ' ${ path } ', ${ status } ` )
9194 }
92- return undefined
9395 }
9496
9597 validateRequestBody (
9698 path : string ,
9799 method : ValidatorHttpMethod ,
98- data : unknown ,
99- strict : boolean ) : ErrorObj [ ] | undefined {
100- const validator = this . requestBodyValidators . find ( ( v ) => v . path === path && v . method === method ) ?. validator
100+ data : unknown ) : ErrorObj [ ] | undefined {
101+ const validator = this . requestBodyValidators . find ( ( v ) => v . path === path ?. toLowerCase ( ) && v . method === method ?. toLowerCase ( ) ) ?. validator
101102 if ( validator ) {
102103 return this . validateBody ( validator , data )
103- } else if ( strict ) {
104- throw new Error ( `No validator found for '${ method } ${ path } '` )
104+ } else {
105+ throw new Error ( `No request body validator found for '${ method } ${ path } '` )
105106 }
106- return undefined
107107 }
108108
109109
@@ -124,11 +124,11 @@ export class AjvOpenApiValidator implements OpenApiValidator {
124124 params : URLSearchParams ,
125125 strict = true
126126 ) : ErrorObj [ ] | undefined {
127- const parameterDefinitions = this . paramsValidators . filter ( ( p ) => p . path === path && p . method === method )
127+ const parameterDefinitions = this . paramsValidators . filter ( ( p ) => p . path === path ?. toLowerCase ( ) && p . method === method ?. toLowerCase ( ) )
128128
129129 let errResponse : ErrorObj [ ] = [ ]
130130 params . forEach ( ( value , key ) => {
131- const paramDefinitionIndex = parameterDefinitions . findIndex ( ( p ) => p . param . name === key )
131+ const paramDefinitionIndex = parameterDefinitions . findIndex ( ( p ) => p . param . name === key ?. toLowerCase ( ) )
132132 if ( paramDefinitionIndex < 0 ) {
133133 if ( strict ) {
134134 errResponse . push ( {
@@ -204,6 +204,7 @@ export class AjvOpenApiValidator implements OpenApiValidator {
204204 }
205205 }
206206
207+ this . validatorOpts . log ( `Adding schema '#/components/schemas/${ key } '` )
207208 this . ajv . addSchema ( schema , `#/components/schemas/${ key } ` , ) ;
208209 } ) ;
209210 }
@@ -255,9 +256,10 @@ export class AjvOpenApiValidator implements OpenApiValidator {
255256
256257 if ( schema ) {
257258 const schemaName = `#/paths${ path . replace ( / [ { } ] / g, '' ) } /${ method } /requestBody`
259+ this . validatorOpts . log ( `Adding request body validator '${ path } ', '${ method } ' with schema '${ schemaName } '` )
258260 this . ajv . addSchema ( schema , schemaName ) ;
259261 const validator = this . ajv . compile ( { $ref : schemaName } )
260- this . requestBodyValidators . push ( { path, method, validator} )
262+ this . requestBodyValidators . push ( { path : path . toLowerCase ( ) , method : method . toLowerCase ( ) as ValidatorHttpMethod , validator} )
261263 }
262264 }
263265
@@ -294,9 +296,10 @@ export class AjvOpenApiValidator implements OpenApiValidator {
294296
295297 if ( schema ) {
296298 const schemaName = `#/paths${ path . replace ( / [ { } ] / g, '' ) } /${ method } /response/${ key } `
299+ this . validatorOpts . log ( `Adding response body validator '${ path } ', '${ method } ', '${ key } ' with schema '${ schemaName } '` )
297300 this . ajv . addSchema ( schema , schemaName ) ;
298301 const validator = this . ajv . compile ( { $ref : schemaName } )
299- this . responseBodyValidators . push ( { path, method, status : key , validator} )
302+ this . responseBodyValidators . push ( { path : path . toLowerCase ( ) , method : method . toLowerCase ( ) as ValidatorHttpMethod , status : key , validator} )
300303 }
301304 } )
302305 }
@@ -334,9 +337,10 @@ export class AjvOpenApiValidator implements OpenApiValidator {
334337 // TODO could also add support for other parameters such as headers here
335338 if ( resolvedParam ?. in === 'query' && resolvedParam . schema ) {
336339 const schemaName = `#/paths${ path . replace ( / [ { } ] / g, '' ) } /${ method } /parameters/${ resolvedParam . name } `
340+ this . validatorOpts . log ( `Adding parameter validator '${ path } ', '${ method } ', '${ resolvedParam . name } '` )
337341 this . ajv . addSchema ( resolvedParam . schema , schemaName ) ;
338342 const validator = this . ajv . compile ( { $ref : schemaName } )
339- this . paramsValidators . push ( { path, method, param : { name : resolvedParam . name , required : resolvedParam . required , allowEmptyValue : resolvedParam . allowEmptyValue } , validator} )
343+ this . paramsValidators . push ( { path : path . toLowerCase ( ) , method : method . toLowerCase ( ) as ValidatorHttpMethod , param : { name : resolvedParam . name ?. toLowerCase ( ) , required : resolvedParam . required , allowEmptyValue : resolvedParam . allowEmptyValue } , validator} )
340344 }
341345 } )
342346 }
0 commit comments