Hapi.js && Joi Validator — Custom Messages

server.route({
config: {
validate: {
payload: {
email: Joi.string().required().email(),
password: Joi.string().required()
}
}
},
handler: SignIn,
method: 'POST',
path: '/account/sign-in'
})

1. The first method

email: Joi.string().required().email().error(new Error('E-mail is not correct.'))

2. The second method — more detailed

The main file containing default language object.

server.route({
config: {
validate: {
options: {
language: {
string: {
email: '!!E-mail "{{value}}" is not correct.'
}
}

},
payload: {
email: Joi.string().required().email(),
password: Joi.string().required()
}
}
},
handler: SignIn,
method: 'POST',
path: '/account/sign-in'
})

3. Useful information

{{value}} — {{!value}} — ! for HTML escape

!! — This will remove the {{key}} at the start of the string.

language: {
messages: {
wrapArrays: false
}
}

This will remove the array brackets.