@@ -3,6 +3,7 @@ import { OpenAPIV3 } from 'openapi-types'
33import { buildRef } from '../resolve'
44import { BodyDiscriminatorFunction } from './decorators'
55import { options } from '../option'
6+ import { CustomLogger } from '../logger'
67
78const { getCustomLogger, features } = options
89
@@ -27,6 +28,8 @@ export async function validateAndParse(
2728 bodyDiscriminatorFn ?: BodyDiscriminatorFunction
2829 }
2930) : Promise < any [ ] > {
31+ const logger = getCustomLogger ( )
32+
3033 const args : any [ ] = [ ]
3134 for ( const param of rules . params || [ ] ) {
3235 // Handling @Request ()
@@ -37,7 +40,13 @@ export async function validateAndParse(
3740 // Handling body
3841 if ( param . in === 'body' ) {
3942 args . push (
40- await validateBody ( req , rules . body ! , rules . bodyDiscriminatorFn , schemas )
43+ await validateBody (
44+ req ,
45+ rules . body ! ,
46+ rules . bodyDiscriminatorFn ,
47+ schemas ,
48+ logger
49+ )
4150 )
4251 continue
4352 }
@@ -92,7 +101,8 @@ export async function validateAndParse(
92101 value ,
93102 param . schema ! ,
94103 schemas ,
95- 'unknown'
104+ 'unknown' ,
105+ logger
96106 )
97107 if ( ! ValidationResponse . succeed ) {
98108 throw new ValidateError (
@@ -114,6 +124,7 @@ export function validateAndParseResponse(
114124 statusCode : string ,
115125 contentType : string
116126) : unknown {
127+ const logger = getCustomLogger ( )
117128 try {
118129 const rule = rules [ statusCode ] ?? rules . default
119130 if ( ! rule )
@@ -130,17 +141,16 @@ export function validateAndParseResponse(
130141 if ( typeof data === 'undefined' || data === null ) {
131142 return data
132143 }
133- getCustomLogger ( ) . error (
134- `Schema is not found for '${ contentType } ', throwing error`
135- )
144+ logger . error ( `Schema is not found for '${ contentType } ', throwing error` )
136145 throw new ValidateError ( { } , 'This content-type is not allowed' )
137146 }
138147 const ValidationResponse = validateAndParseValueAgainstSchema (
139148 'response' ,
140149 data ,
141150 expectedSchema ,
142151 schemas ,
143- 'unknown'
152+ 'unknown' ,
153+ logger
144154 )
145155 if ( ! ValidationResponse . succeed ) {
146156 throw new ValidateError (
@@ -162,23 +172,20 @@ async function validateBody(
162172 req : express . Request ,
163173 rule : OpenAPIV3 . RequestBodyObject ,
164174 discriminatorFn : BodyDiscriminatorFunction | undefined ,
165- schemas : OpenAPIV3 . ComponentsObject [ 'schemas' ]
175+ schemas : OpenAPIV3 . ComponentsObject [ 'schemas' ] ,
176+ logger : CustomLogger
166177) : Promise < unknown > {
167178 const body = req . body
168179 const contentType = ( req . headers [ 'content-type' ] ?? 'application/json' ) . split (
169180 ';'
170181 ) [ 0 ]
171182 const expectedSchema = rule . content [ contentType ] ?. schema
172183 if ( typeof expectedSchema === 'undefined' ) {
173- getCustomLogger ( ) . error (
174- `Schema is not found for '${ contentType } ', throwing error`
175- )
184+ logger . error ( `Schema is not found for '${ contentType } ', throwing error` )
176185 throw new ValidateError ( { } , 'This content-type is not allowed' )
177186 }
178187 if ( req . readableEnded === false ) {
179- getCustomLogger ( ) . warn (
180- `! Warning: Body has not be parsed, body validation skipped !`
181- )
188+ logger . warn ( `! Warning: Body has not be parsed, body validation skipped !` )
182189 return body
183190 }
184191 if ( discriminatorFn ) {
@@ -188,7 +195,8 @@ async function validateBody(
188195 body ,
189196 { $ref : buildRef ( schemaName ) } ,
190197 schemas ,
191- 'unknown'
198+ 'unknown' ,
199+ logger
192200 )
193201 if ( validationResult . succeed ) {
194202 return validationResult . value
@@ -217,7 +225,8 @@ async function validateBody(
217225 body ,
218226 expectedSchema ,
219227 schemas ,
220- 'unknown'
228+ 'unknown' ,
229+ logger
221230 )
222231 if ( validationResult . succeed ) {
223232 return validationResult . value
@@ -293,7 +302,8 @@ function validateAndParseValueAgainstSchema(
293302 | OpenAPIV3 . ArraySchemaObject
294303 | OpenAPIV3 . NonArraySchemaObject ,
295304 schemas : OpenAPIV3 . ComponentsObject [ 'schemas' ] ,
296- parentType : 'allOf' | 'oneOf' | 'array' | 'object' | 'unknown'
305+ parentType : 'allOf' | 'oneOf' | 'array' | 'object' | 'unknown' ,
306+ logger : CustomLogger
297307) : SafeValidatedValue {
298308 const currentSchema = getFromRef ( schema , schemas )
299309 // Nullable
@@ -370,7 +380,8 @@ function validateAndParseValueAgainstSchema(
370380 const formatResult = validateAndParseFormat (
371381 name ,
372382 value ,
373- currentSchema . format
383+ currentSchema . format ,
384+ logger
374385 )
375386 if ( ! formatResult . succeed ) {
376387 return {
@@ -477,7 +488,8 @@ function validateAndParseValueAgainstSchema(
477488 item ,
478489 currentSchema . items ,
479490 schemas ,
480- 'array'
491+ 'array' ,
492+ logger
481493 )
482494 )
483495 const everyItemIsGood = values . every ( value => value . succeed )
@@ -532,7 +544,8 @@ function validateAndParseValueAgainstSchema(
532544 propValue ,
533545 currentSchema . properties ! [ propName ] ,
534546 schemas ,
535- 'unknown'
547+ 'unknown' ,
548+ logger
536549 )
537550 if ( ! validationResult . succeed ) {
538551 return validationResult
@@ -562,7 +575,7 @@ function validateAndParseValueAgainstSchema(
562575 ) {
563576 if ( additionalKeys . length > 0 ) {
564577 if ( features . enableLogUnexpectedAdditionalData ) {
565- getCustomLogger ( ) . warn (
578+ logger . warn (
566579 `Additional properties are not allowed. Found: ${ additionalKeys . join ( ', ' ) } `
567580 )
568581 } else {
@@ -596,7 +609,8 @@ function validateAndParseValueAgainstSchema(
596609 propValue ,
597610 currentSchema . additionalProperties as any ,
598611 schemas ,
599- 'unknown'
612+ 'unknown' ,
613+ logger
600614 )
601615 if ( ! validationResult . succeed ) {
602616 return validationResult
@@ -612,7 +626,7 @@ function validateAndParseValueAgainstSchema(
612626 additionalKeys . length > 0
613627 ) {
614628 if ( features . enableLogUnexpectedAdditionalData ) {
615- getCustomLogger ( ) . warn (
629+ logger . warn (
616630 `Additional properties are not allowed. Found: ${ additionalKeys . join ( ', ' ) } `
617631 )
618632 } else {
@@ -635,7 +649,8 @@ function validateAndParseValueAgainstSchema(
635649 value ,
636650 schema ,
637651 schemas ,
638- 'allOf'
652+ 'allOf' ,
653+ logger
639654 )
640655 )
641656
@@ -664,7 +679,8 @@ function validateAndParseValueAgainstSchema(
664679 value ,
665680 schema ,
666681 schemas ,
667- 'oneOf'
682+ 'oneOf' ,
683+ logger
668684 )
669685 if ( succeed ) {
670686 // set as matching value if we haven't found one
@@ -693,7 +709,7 @@ function validateAndParseValueAgainstSchema(
693709 }
694710 return { succeed : true , value : matchingValue }
695711 }
696- getCustomLogger ( ) . warn (
712+ logger . warn (
697713 `Schema of ${ name } is not yet supported, skipping value validation`
698714 )
699715 return { succeed : true , value }
@@ -702,7 +718,8 @@ function validateAndParseValueAgainstSchema(
702718function validateAndParseFormat (
703719 name : string ,
704720 value : string ,
705- format : string
721+ format : string ,
722+ logger : CustomLogger
706723) : SafeValidatedValue {
707724 if ( format === 'date' || format === 'date-time' ) {
708725 const date = new Date ( value )
@@ -715,7 +732,7 @@ function validateAndParseFormat(
715732 }
716733 return { succeed : true , value : date }
717734 }
718- getCustomLogger ( ) . warn (
735+ logger . warn (
719736 `Format '${ format } ' is not yet supported, value is returned without additionnal parsing`
720737 )
721738 return { succeed : true , value }
0 commit comments