11import Ajv from 'ajv'
2- import * as fastify from 'fastify'
3- import { FastifyInstance , FastifyReply , FastifyRequest , RouteOptions } from 'fastify'
4- import { InternalServerError , INTERNAL_SERVER_ERROR } from 'http-errors-enhanced'
52import {
6- Configuration ,
3+ type FastifyInstance ,
4+ type FastifyReply ,
5+ type FastifyRequest ,
6+ type ValidationResult as FastifyValidationResult ,
7+ type RouteOptions
8+ } from 'fastify'
9+ import { INTERNAL_SERVER_ERROR , InternalServerError } from 'http-errors-enhanced'
10+ import {
711 kHttpErrorsEnhancedConfiguration ,
812 kHttpErrorsEnhancedResponseValidations ,
9- RequestSection ,
10- ResponseSchemas ,
11- ValidationFormatter ,
12- Validations
13+ type Configuration ,
14+ type RequestSection ,
15+ type ResponseSchemas ,
16+ type ValidationFormatter ,
17+ type Validations
1318} from './interfaces.js'
1419import { get } from './utils.js'
1520
16- export interface ValidationResult extends fastify . ValidationResult {
21+ export interface ValidationResult extends FastifyValidationResult {
1722 dataPath : any
1823 instancePath : string
1924}
@@ -31,7 +36,7 @@ export function niceJoin(array: string[], lastSeparator: string = ' and ', separ
3136 }
3237}
3338
34- export const validationMessagesFormatters : { [ key : string ] : ValidationFormatter } = {
39+ export const validationMessagesFormatters : Record < string , ValidationFormatter > = {
3540 contentType : ( ) =>
3641 'only JSON payloads are accepted. Please set the "Content-Type" header to start with "application/json"' ,
3742 json : ( ) => 'the body payload is not a valid JSON' ,
@@ -91,10 +96,10 @@ export const validationMessagesFormatters: { [key: string]: ValidationFormatter
9196
9297export function convertValidationErrors (
9398 section : RequestSection ,
94- data : { [ key : string ] : unknown } ,
99+ data : Record < string , unknown > ,
95100 validationErrors : ValidationResult [ ]
96101) : Validations {
97- const errors : { [ key : string ] : string } = { }
102+ const errors : Record < string , string > = { }
98103
99104 if ( section === 'querystring' ) {
100105 section = 'query'
@@ -217,7 +222,7 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
217222 this [ kHttpErrorsEnhancedResponseValidations ] . push ( [
218223 this ,
219224 validators ,
220- Object . entries ( route . schema . response as { [ key : string ] : object } )
225+ Object . entries ( route . schema . response as Record < string , object > )
221226 ] )
222227
223228 // Note that this hook is not called for non JSON payloads therefore validation is not possible in such cases
@@ -232,29 +237,33 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
232237
233238 // Never validate error 500
234239 if ( statusCode === INTERNAL_SERVER_ERROR ) {
235- return done ( null , payload )
240+ done ( null , payload )
241+ return
236242 }
237243
238244 // No validator, it means the HTTP status is not allowed
239245 const validator = validators [ statusCode ]
240246
241247 if ( ! validator ) {
242248 if ( request [ kHttpErrorsEnhancedConfiguration ] ! . allowUndeclaredResponses ) {
243- return done ( null , payload )
249+ done ( null , payload )
250+ return
244251 }
245252
246- return done ( new InternalServerError ( validationMessagesFormatters . invalidResponseCode ( statusCode ) ) )
253+ done ( new InternalServerError ( validationMessagesFormatters . invalidResponseCode ( statusCode ) ) )
254+ return
247255 }
248256
249257 // Now validate the payload
250258 const valid = validator ( payload )
251259
252260 if ( ! valid ) {
253- return done (
261+ done (
254262 new InternalServerError ( validationMessagesFormatters . invalidResponse ( statusCode ) , {
255263 failedValidations : convertValidationErrors ( 'response' , payload , validator . errors as ValidationResult [ ] )
256264 } )
257265 )
266+ return
258267 }
259268
260269 done ( null , payload )
@@ -263,7 +272,7 @@ export function addResponseValidation(this: FastifyInstance, route: RouteOptions
263272
264273export function compileResponseValidationSchema ( this : FastifyInstance , configuration : Configuration ) : void {
265274 // Fix CJS/ESM interoperability
266- // @ts -expect-error
275+ // @ts -expect-error Fix types
267276 let AjvConstructor = Ajv as Ajv & { default ?: Ajv }
268277
269278 if ( AjvConstructor . default ) {
@@ -273,7 +282,7 @@ export function compileResponseValidationSchema(this: FastifyInstance, configura
273282 const hasCustomizer = typeof configuration . responseValidatorCustomizer === 'function'
274283
275284 for ( const [ instance , validators , schemas ] of this [ kHttpErrorsEnhancedResponseValidations ] ) {
276- // @ts -expect-error
285+ // @ts -expect-error Fix types
277286 const compiler = new AjvConstructor ( {
278287 // The fastify defaults, with the exception of removeAdditional and coerceTypes, which have been reversed
279288 removeAdditional : false ,
0 commit comments