Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

include validation errors in the response body #200

@TheYuriG

Description

@TheYuriG

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImproves an existing feature.functionalityA issue or pull request regarding the functionality.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions