Michael SchaufelbergerM
tRPC3y ago
4 replies
Michael Schaufelberger

How can I access the trpc context in zod's refine function?

To do something like
create: myProcedure
    .input(myInputSchema.refine(async ({ slug }, ctx) => 
      slugDoesNotAlreadyExist(ctx.db, slug),
      {
        message: 'Slug already exists',
        path: ['slug'],
    }))
    .mutation(async ({ ctx, input }) => {
       return ctx.db.insert(...)
    }),


Edit:

It would allow for easier UX because I can use the existing validation code in the frontend to show the user the errors at the correct field.
Was this page helpful?