@@ -51,7 +51,7 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
5151 hostname : ( ) => 'must be a valid hostname' ,
5252 ipv4 : ( ) => 'must be a valid IPv4' ,
5353 ipv6 : ( ) => 'must be a valid IPv6' ,
54- paramType : ( type : string ) => {
54+ paramType : type => {
5555 switch ( type ) {
5656 case 'integer' :
5757 return 'must be a valid integer number'
@@ -68,8 +68,8 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
6868 }
6969 } ,
7070 presentString : ( ) => 'must be a non empty string' ,
71- minimum : ( min : number ) => `must be a number greater than or equal to ${ min } ` ,
72- maximum : ( max : number ) => `must be a number less than or equal to ${ max } ` ,
71+ minimum : min => `must be a number greater than or equal to ${ min } ` ,
72+ maximum : max => `must be a number less than or equal to ${ max } ` ,
7373 minimumProperties ( min : number ) : string {
7474 return min === 1 ? 'cannot be a empty object' : `must be a object with at least ${ min } properties`
7575 } ,
@@ -82,16 +82,16 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
8282 maximumItems ( max : number ) : string {
8383 return max === 0 ? 'must be a empty array' : `must be an array with at most ${ max } items`
8484 } ,
85- enum : ( values : Array < string > ) =>
85+ enum : values =>
8686 `must be one of the following values: ${ niceJoin (
8787 values . map ( ( f : string ) => `"${ f } "` ) ,
8888 ' or '
8989 ) } `,
90- pattern : ( pattern : string ) => `must match pattern "${ pattern . replace ( / \( \? : / g, '(' ) } "` ,
91- invalidResponseCode : ( code : number ) => `This endpoint cannot respond with HTTP status ${ code } .` ,
92- invalidResponse : ( code : number ) =>
90+ pattern : pattern => `must match pattern "${ pattern . replace ( / \( \? : / g, '(' ) } "` ,
91+ invalidResponseCode : code => `This endpoint cannot respond with HTTP status ${ code } .` ,
92+ invalidResponse : code =>
9393 `The response returned from the endpoint violates its specification for the HTTP status ${ code } .` ,
94- invalidFormat : ( format : string ) => `must match format "${ format } " (format)`
94+ invalidFormat : format => `must match format "${ format } " (format)`
9595}
9696
9797export function convertValidationErrors (
@@ -227,41 +227,43 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
227227 ] )
228228
229229 // Note that this hook is not called for non JSON payloads therefore validation is not possible in such cases
230- route . preSerialization = async function (
230+ route . preSerialization = function (
231231 this : FastifyInstance ,
232232 request : FastifyRequest ,
233233 reply : FastifyReply ,
234- payload : any
235- ) : Promise < any > {
234+ payload : any ,
235+ done : ( err : Error | null , payload ?: any ) => void
236+ ) : void {
236237 const statusCode = reply . raw . statusCode
237238
238239 // Never validate error 500
239240 if ( statusCode === INTERNAL_SERVER_ERROR ) {
240- return payload
241+ return done ( null , payload )
241242 }
242243
243244 // No validator, it means the HTTP status is not allowed
244245 const validator = validators [ statusCode ]
245246
246247 if ( ! validator ) {
247248 if ( request [ kHttpErrorsEnhancedConfiguration ] ! . allowUndeclaredResponses ) {
248- return payload
249+ return done ( null , payload )
249250 }
250251
251- throw new InternalServerError ( validationMessagesFormatters . invalidResponseCode ( statusCode ) )
252+ return done ( new InternalServerError ( validationMessagesFormatters . invalidResponseCode ( statusCode ) ) )
252253 }
253254
254255 // Now validate the payload
255256 const valid = validator ( payload )
256257
257258 if ( ! valid ) {
258- console . log ( validator . errors )
259- throw new InternalServerError ( validationMessagesFormatters . invalidResponse ( statusCode ) , {
260- failedValidations : convertValidationErrors ( 'response' , payload , validator . errors as Array < ValidationResult > )
261- } )
259+ return done (
260+ new InternalServerError ( validationMessagesFormatters . invalidResponse ( statusCode ) , {
261+ failedValidations : convertValidationErrors ( 'response' , payload , validator . errors as Array < ValidationResult > )
262+ } )
263+ )
262264 }
263265
264- return payload
266+ done ( null , payload )
265267 }
266268}
267269
0 commit comments