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

include validation errors in the response body #200

Open
TheYuriG opened this issue Aug 26, 2023 · 2 comments
Open

include validation errors in the response body #200

TheYuriG opened this issue Aug 26, 2023 · 2 comments
Labels
enhancement Improves an existing feature. functionality A issue or pull request regarding the functionality.
Milestone

Comments

@TheYuriG
Copy link

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.

@TheYuriG TheYuriG changed the title Request: there is no way to retrieve Zod's validation errors Request: the Zod validation middleware doesn't pass the errors to the response function Aug 26, 2023
@boywithkeyboard boywithkeyboard added enhancement Improves an existing feature. functionality A issue or pull request regarding the functionality. labels Aug 26, 2023
@boywithkeyboard boywithkeyboard added this to the v2.0 milestone Aug 26, 2023
@TheYuriG
Copy link
Author

I ended up getting it to work by moving the validation to the response function as follows:

app.put(
  // request path
  "/register",
  // response
  async (c) => {
    // zod validation
    const zodValidation = 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",
      }),
    });

    const reqBody = await c.req.json();
    console.log(reqBody);

    try {
      console.log("validated body:", zodValidation.parse(reqBody));
    } catch ({ issues }) {
      console.log("issues:", issues);
    }
  },
);

Regardless, I would prefer if this was passed to the function by default instead so I can focus on the core logic rather than also doing the parsing myself.

@boywithkeyboard
Copy link
Collaborator

This will definitely be supported starting with version v2.0!

@boywithkeyboard boywithkeyboard changed the title Request: the Zod validation middleware doesn't pass the errors to the response function include validation errors in the response body Aug 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Improves an existing feature. functionality A issue or pull request regarding the functionality.
Projects
None yet
Development

No branches or pull requests

2 participants