import { Lifecycle , Request , ResponseToolkit , Util } from '@hapi/hapi' ; import { ValidationError } from 'joi' ; import Hoek from '@hapi/hoek' ; /**
 * Hapi extends the ValidationError interface to add this output key with more data.
 */ export interface HapiValidationError extends ValidationError { output : { statusCode : number ; headers : Util . Dictionary < string | string [ ] > ; payload : { statusCode : number ; error : string ; message ? : string ; validation : { source : string ; keys : string [ ] ; } ; } ; } ; } /**
 * Used to replicate Hapi v16 and below's validation responses. Should be used in the routes.validate.failAction key.
 */ export function defaultValidationErrorHandler ( request , h , err ? ) { if ( err && err . name === 'ValidationError' && err . hasOwnProperty ( 'output' ) ) { const validationError = err as HapiValidationError ; const validationKeys = [ ] ; validationError . details . forEach ( ( detail ) => { if ( detail . path . length > 0 ) { validationKeys . push ( Hoek . escapeHtml ( detail . path . join ( '.' ) ) ) ; } else { validationKeys . push ( 'value' ) ; } } ) ; validationError . output . payload . validation . keys = validationKeys ; } throw err ; }