This repository was archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
include validation errors in the response body #200
Copy link
Copy link
Open
Labels
enhancementImproves an existing feature.Improves an existing feature.functionalityA issue or pull request regarding the functionality.A issue or pull request regarding the functionality.
Milestone
Description
I need to be able to retrieve failed network requests that fail to pass through Zod's validation.
Right now, it seems to return default 400 BAD REQUEST responses by default, but I prefer to know why those requests failed, so I can return that data to the browser and display alerts for all their errors, in case they manage to get through the frontend validation somehow.
I'm using zod as a validation middleware before the response is sent. This is my code:
app.put(
//path
"/register",
//validation
{
body: z.object({
username: z.string({
required_error: "Name is required",
invalid_type_error: "Name must be a string",
}).min(6, { message: "Must be 6 or more characters long" }),
email: z.string().email({ message: "Invalid email address" }),
password: z.string({
required_error: "Password is required",
invalid_type_error: "Password must be a string",
}).min(8, { message: "Password must be 8 or more characters long" }),
confirmPassword: z.string({
required_error: "Password confirmation is required",
invalid_type_error: "Password confirmation must be a string",
}).min(8, {
message: "Password confirmation must be 8 or more characters long",
}),
}),
},
// response
async (c) => {
const reqBody = await c.req.body();
console.log("body:", reqBody);
},
);When an error happens, the response is sent before reaching the response block.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementImproves an existing feature.Improves an existing feature.functionalityA issue or pull request regarding the functionality.A issue or pull request regarding the functionality.