DenamD
tRPC3y ago
Denam

How do you make use of custom input validation?

I have an input validation middleware, to parse my input based on some values in ctx.
export function contextInputValidation(
    callback: (options: { ctx: Context }) => zod.Schema,
) {
    return middleware(async ({ next, ctx, rawInput }) => {
        const schema = callback({ ctx });
        const input = schema.parse(rawInput);
        const stack = await next({
            rawInput: input,
        });

        return stack;
    });
}


However, how can I pass the value of this input down? rawInput and
input
are not available in the mutation method.

request
  .use(contextInputValidation(({ ctx }) => schema))
  .mutation(async ({ ctx, input, rawInput }) => {
    ...
  })
Was this page helpful?