T
tRPC

How do you make use of custom input validation?

How do you make use of custom input validation?

DDenam9/27/2023
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;
});
}
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 }) => {
...
})
request
.use(contextInputValidation(({ ctx }) => schema))
.mutation(async ({ ctx, input, rawInput }) => {
...
})

Looking for more? Join the community!

T
tRPC

How do you make use of custom input validation?

Join Server