Customized error messages for Hapi.js Joi validation
It provides the scenario based custom messages for the joi validation. 95% of the code taken from the relish package and included some features. By default it supports the internalization using either hapi-i18n or hapi-i18n-mongo, but its an optional.
Default Joi Response
{
"statusCode": 400,
"error": "Bad Request",
"message": "\"name\" fails because [\"name\" is not allowed to be empty], \"email\" must be a valid email",
"validation": {
"source": "payload",
"keys": [
"name",
"email"
]
}
}
Example Relish_message Response
{
"statusCode": 400,
"message": "Validation failed",
"errors": {
"name": "name is not allowed to be empty",
"email": "email must be a valid email"
}
}
npm install relish_message --save
First load and initialize the module
// load the package and set custom message options
const Relish = require('relish_message')({
stripQuotes : true,
statusCode : 400,
message : {
value: "Validation failed",
key : 'message'
},
error_key : 'error',
output_format: {
message: '',
data : {},
error : {}
},
messages : {
'signup': {
'name' : {
'required': 'First name required',
'empty' : 'First name required',
'min' : ['First name must have atleast {key} characters', {'key': 2}],
'max' : ['First name have maximum {key} characters', {'key': 35}]
},
'email' : {
'required': 'Email required',
'empty' : 'Email required',
'min' : ['Email must have atleast {key} characters', {'key': 3}],
'max' : ['Email have maximum {key} characters', {'key': 256}]
},
}
}
});
Once relish message was initialized, it will package exposes a custom failAction
handler that can be used in your Hapi.js [Route Options][hapi-route-options].
// call the failAction handler in your route options
server.route({
method: 'POST',
path: '/signup',
config: {
app: {
scenario: 'signup'
},
validate: {
// set a custom failAction handler
failAction: Relish.failAction,
payload: {
name: Joi.string().required(),
email: Joi.string().email()
}
}
},
handler: (request, h) => return h.response('OK')
});
Used to remove quotes from error messages if it is true. (Default: true)
Http status code of the error. (Default: 400)
Tells the response format of the error. Default format is
output_format: {
message: '',
error: {}
data: {}
}
Here data property will be optional one. But message and error were mandatory. This output_format property used to customize the response data format
Holds the response format message key name and value
Example if the message property is defined as follows
message: {
key: 'messages',
value: 'Validation failed'
}
output_format: {
message: '',
error: {}
data: {}
}
then response will be like this
{
messages: 'Validation failed',
error: {},
data: {}
}
Holds error key name Example
error_key: "errors",
output_format: {
message: '',
error: {}
data: {}
}
Holds the error message for each field
Output:
{
message: '',
errors: {
"name": "name is not allowed to be empty",
"email": "email must be a valid email"
}
data: {}
}
- Minor issues fixed
- Minor issues fixed
- Hapi 17.x.x support included. For those who are using 16.x.x try to use version 1.3.10
- Output format included
- Fixed the bugs
- Query and parameters validation error message was fixed
- Changed the response format
Old version:
{
"statusCode": 400,
"message": "Validation failed",
"errors": [
{
"key": "name",
"message": "name is not allowed to be empty",
},
{
"name": "email",
"message": "email must be a valid email"
}
]
]
}
New version:
{
"statusCode": 400,
"message": "Validation failed",
"errors": {
"name": "name is not allowed to be empty",
"email": "email must be a valid email"
}
}