www/versioned_docs/version-9.x/server/output-validation.md
tRPC gives you automatic type-safety of outputs without the need of adding a validator; however, it can be useful at times to strictly define the output type in order to prevent sensitive data of being leaked.
Similar to input:, an output: validation to the query() and mutation() router methods. The output validator is invoked with the payload returned by the resolve() function.
When an output validator is defined, its inferred type is expected as the return type of the resolve() function.
:::info
INTERNAL_SERVER_ERROR.:::
tRPC works out-of-the-box with yup/superstruct/zod/myzod/custom validators/[..] - see test suite
import * as trpc from '@trpc/server';
import { z } from 'zod';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
output: z.object({
greeting: z.string(),
}),
// expects return type of { greeting: string }
resolve() {
return {
greeting: 'hello!',
};
},
});
export type AppRouter = typeof appRouter;
import * as trpc from '@trpc/server';
import * as yup from 'yup';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
output: yup.object({
greeting: yup.string().required(),
}),
resolve() {
return { greeting: 'hello!' };
},
});
export type AppRouter = typeof appRouter;
import * as trpc from '@trpc/server';
import * as t from 'superstruct';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: t.string(),
output: t.object({
greeting: t.string(),
}),
resolve({ input }) {
return { greeting: input };
},
});
export type AppRouter = typeof appRouter;
import * as trpc from '@trpc/server';
import * as t from 'superstruct';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
output: (value: any) => {
if (value && typeof value.greeting === 'string') {
return { greeting: value.greeting };
}
throw new Error('Greeting not found');
},
// expects return type of { greeting: string }
resolve() {
return { greeting: 'hello!' };
},
});
export type AppRouter = typeof appRouter;