跳至主内容
版本:9.x

输出验证

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

tRPC 默认提供输出结果的自动类型安全,无需额外添加验证器;但在某些场景下,严格定义输出类型可有效防止敏感数据泄露。

类似 input: 的用法,可通过 query()mutation() 路由方法添加 output: 验证器。该验证器将处理 resolve() 函数返回的载荷。

当定义 output 验证器后,其推断类型将作为 resolve() 函数的预期返回类型。

信息
  • 此功能完全可选,仅用于确保不会意外泄露敏感数据
  • 若输出验证失败,服务端将返回 INTERNAL_SERVER_ERROR 错误

示例

tRPC 开箱即支持 yup/superstruct/zod/myzod/自定义验证器等方案 - 查看测试用例

使用 Zod

tsx
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;
tsx
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;

使用 Yup

tsx
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;
tsx
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;

使用 Superstruct

tsx
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;
tsx
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;

使用自定义验证器

tsx
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;
tsx
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;