How can I customize an errors with Fastify, Ajv and Scheme? I need to format by:
{
"message": "Bad request",
"path": "/account/register",
"status": 400,
"timestamp": 1697094987,
"errors": [
{ "key": "email", "value": "This value is not a valid email address."
]
}
When I try to:
const routes = async (app: Application) => {
app.post('/accounts/register', { schema }, async (req: FastifyRequest, reply: FastifyReply) => {
reply.code(200);
});
};
I get an error with the following format:
{
"statusCode": 400,
"code": "FST_ERR_VALIDATION",
"error": "Bad Request",
"message": "body must have required property 'email'"
}
If you need you can change the response error's
message
by setting theschemaErrorFormatter
.Nevertheless, you will not change the error output format.
Do change the output format you must:
allErrors
config. Note that it is considered insecureWill produce this output: