Wojciech
Wojciech16mo ago

how to handle error from Zod in trpc?

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'],
})
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
1 Reply
sachin
sachin16mo ago
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