I'm using joi to validate my apis request. All things is good except the error message return the field name with slash '\' character.
{
"message": "validation error",
"error": {
"status": 400,
"statusText": "Bad Request",
"errors": [
{
"field": "email",
"location": "body",
"messages": [
"\"email\" must be a valid email"
],
"types": [
"string.email"
]
}
]
}
}
Anyone got this problem ?
Your variable names are put in quotes by default and those quotes are escaped with the backslash character. I couldn't find much on the reason for this so maybe someone else can weigh in there.
But to override this behavior, you can override the language
option when you call joi.validate()
and pass in an optional options
parameter. This example just overrides the escaping for strings.
var joi = require('joi');
var schema = joi.object().keys({
name: joi.string().required()
});
var x = {
name: 123
};
var options = {
language: {
string: {
base: '{{key}} must be a string'
}
}
};
var result = joi.validate(x, schema, options);
console.log(JSON.stringify(result, null, 2));
To override escape characters for all types, specify a key
parameter in your options
. Note the space at the end.
var options = {
language: {
key: '{{key}} '
}
};
More detailed examples on how to override language
can be found here.