T
tRPC

how to handle error from Zod in trpc?

how to handle error from Zod in trpc?

Mmlody_krol9/12/2023
I would like to return error instead throwing it away so i could show user nice feedback i found https://zod.dev/?id=safeparse but i dont know how to implement it
signup: authProcedure
.input(SignupInput)
.mutation(
async ({
ctx: { supabase },
input: { email, password, repeatPassword },
}) => {
const user = await supabase.auth.getSession()
console.log(user.data.session)
console.log(email, password, repeatPassword)
}
),


export const SignupInput = z
.object({
password: z.string().min(8),
repeatPassword: z.string().min(8),
})
.refine(data => data.password === data.repeatPassword, {
message: "Passwords don't match",
path: ['repeatPassword'],
})
signup: authProcedure
.input(SignupInput)
.mutation(
async ({
ctx: { supabase },
input: { email, password, repeatPassword },
}) => {
const user = await supabase.auth.getSession()
console.log(user.data.session)
console.log(email, password, repeatPassword)
}
),


export const SignupInput = z
.object({
password: z.string().min(8),
repeatPassword: z.string().min(8),
})
.refine(data => data.password === data.repeatPassword, {
message: "Passwords don't match",
path: ['repeatPassword'],
})
Ssachin9/12/2023
you can look at the error that it throws on the client side and display a status based on that react query has an error property

Looking for more? Join the community!

T
tRPC

how to handle error from Zod in trpc?

Join Server