Closed moeenn closed 1 year ago
JSON.parse
will correctly reconstruct the object
const errors = JSON.parse(response.message)
errors.forEach(({
validation,
code,
message,
}) => {
// do whatever you want with each error's properties
})
Please guide me as to where i should put this code. A short example would be very helpful.
@moeenn fastify-type-provider-zod doesn't form your response, it only throws an error. Please take a look into fastify error handler customization in order to format your response properly: https://www.fastify.io/docs/latest/Reference/Server/#seterrorhandler
Ok. Let me take a look.
That said, by default fastify should be forming proper JSON response already, it might be that the client which is consuming the API that is not deserializing it into an object correctly. In that case you'd need to use the code that @rupert-mckay has provided on your client side, after receiving the response.
The response I posted above was received by Insomnia during my testing. I don't think this is a problem with Insomnia's JSON deserialization.
is response content-type set to application/json? if yes, then it's on client to deserialize
I am using
fastify-type-provider-zod
with Fastify (v 4.7.0).I have defined a Login route and the body schema is defined as follows
If the incoming request doesn't satisfy the zod schema, a validation error is thrown as expected. However the following response is received by the caller in case e.g. email is invalid.
Is there a way to fix the formatting of the response?