import Joi from 'joi' ; import { Request , ResponseToolkit } from '@hapi/hapi' ; import { defaultValidationErrorHandler , HapiValidationError , } from './default_validation_error_handler' ; const emptyOutput = { statusCode : 400 , headers : { } , payload : { statusCode : 400 , error : '' , validation : { source : '' , keys : [ ] , } , } , } ; describe ( 'defaultValidationErrorHandler' , ( ) => { it ( 'formats value validation errors correctly' , ( ) => { expect . assertions ( 1 ) ; const schema = Joi . array ( ) . items ( Joi . object ( { type : Joi . string ( ) . required ( ) , } ) . required ( ) ) ; const error = schema . validate ( [ { } ] , { abortEarly : false } ) . error as HapiValidationError ; error . output = { ... emptyOutput } ; error . output . payload . validation . keys = [ '0.type' , '' ] ; try { defaultValidationErrorHandler ( { } as Request , { } as ResponseToolkit , error ) ; } catch ( err ) { expect ( err . output . payload . validation . keys ) . toEqual ( [ '0.type' , 'value' ] ) ; } } ) ; } ) ;