jstack icon indicating copy to clipboard operation
jstack copied to clipboard

Bug - Part of the Request Body is not being passed down to input() method.

Open marioteik opened this issue 10 months ago • 3 comments

I'm using the publicProcedure as it is in the scaffold:

export const publicProcedure = j.procedure.use(databaseMiddleware);

And I have a route with some methods, between them I have this:

export const authRouter = j.router({
  phoneOtp: publicProcedure
      .input(
        z.object({
          phone: z.string(),
          code: z.string().trim().min(1),
        }),
      )
      .post(async ({ c, ctx, input }) => {
        const { phone, code } = input;
        const { supabase, db } = ctx;
  
        console.log("Input:", input);
        console.log("Destructured values:", { phone, code });
  
        if (!phone) {
          throw new HTTPException(401, {
            message: "Número de telefone inválido.",
          });
        }
  
        if (!code) {
          throw new HTTPException(401, {
            message: "Código de verificação inválido.",
          });
        }

        // rest of the code
      }),
})

The issue is that my "code" property is returning as undefined:

Image

[Error [ZodError]: [
  {
    "code": "invalid_type",
    "expected": "string",
    "received": "undefined",
    "path": [
      "code"
    ],
    "message": "Required"
  }
]] {
  issues: [Array],
  addIssue: [Function (anonymous)],
  addIssues: [Function (anonymous)]
}
 POST /api/auth/phoneOtp 500 in 276ms

Even though if I get the body from the request with c.req.json() the "code" property is there:

Raw body: { phone: '+55XXXXXXXXXXXX', code: '629123' }

Image

I also have created a project from scratch and it is also not working:

Image

I've tried z.number() also, thinking it could be a parser issue but nothing helped.

Edit: the foo in the image was a forth test that I did thinking code and token could be a reservated property in the parser.

marioteik avatar Feb 15 '25 20:02 marioteik