Bug - Part of the Request Body is not being passed down to input() method.
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:
[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' }
I also have created a project from scratch and it is also not working:
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.
This works but it is a hack:
Hello marioteik,
This is not a bug. Basically the client is setup to transform the request and then send to the server. This is required as SuperJson requires a specific format.
But when you use tools like Postman or bruno that tranformaton does not happens. Instead you have to manually do it.
For testing from API Platform just use like thiis {phone: '{"json":"+55 1234567890"}', code: '{"json":"629123"}'}
Let me know if this solves the issue :)
I thing docs should mention it.