HonoH
Hono9mo ago
Kalpak

zValidator c.req.valid("form") type error

Hey everyone, I am trying to use the zValidator middleware which I have setup in an auth.validator.ts file which looks like:
const signUpSchema = z.object({
  email: z.string().email("Invalid email address"),
  password: z
    .string()
    .regex(
      passwordRegex,
      "Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, and one number"
    ),
  firstName: z.string().min(1, "First name is required"),
  lastName: z.string().min(1, "Last name is required"),
  // TODO: Add more specific validation for dob (date format) and gender (enum) if needed
  dob: z
    .string()
    .regex(
      dateRegex,
      "Date must be in YYYY-MM-DD format with valid month and day"
    ),
  gender: z.enum(["male", "female", "other"]),
});

// zValidator middleware instance for the sign-up form
const signUpValidator = zValidator("form", signUpSchema);


I have my route setup like:
app.post(
  "/signup",
  authValidator.signUpValidator,
  signUpController.signUpWithEmail
);


and signUpController like:
async function signUpWithEmail(c: Context) {
  // const { email, password, firstName, lastName, dob, gender } =
  //   await c.req.parseBody();

  // const userMetadata = {
  //   first_name: firstName,
  //   last_name: lastName,
  //   dob,
  //   gender,
  // };

  const body = c.req.valid('form');
  console.log(body);

  // const { error } = await authService.signUpWithEmail(
  //   c,
  //   email as string,
  //   password as string,
  //   userMetadata as UserMetadata
  // );

  // if (error == null) {
  //   return c.render(<EmailVerificationCTA email={email as string} />);
  // }

  c.status(400);
  return c.redirect("/auth/error");
}

export default { signUpWithEmail };


When I am trying to use c.req.valid("form") i get the error:
Argument of type '"form"' is not assignable to parameter of type 'never'.

Im assuming its because the validator middleware is defined in and the route is setup in index.ts.

how do I fix this? Do I have to type the Env being passed into the hono router created in index.ts? If so, how?
Was this page helpful?