From 6f1fc6a9965bc8aa1fdc4e34078b948ed923238a Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Wed, 18 May 2022 09:25:19 -0700 Subject: [PATCH] update validation readme --- recipes/validation.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/recipes/validation.md b/recipes/validation.md index 14a3fb9..7162360 100644 --- a/recipes/validation.md +++ b/recipes/validation.md @@ -2,6 +2,8 @@ tRPC works out-of-the-box with yup/superstruct/zod/myzod/custom validators. Learn more about input validation [here](https://trpc.io/docs/router#input-validation). +## Input Validation + ```ts // ~/server/trpc/index.ts import { z } from 'zod' @@ -21,3 +23,25 @@ export const router = trpc }, }) ``` + +## Output Validation + +```ts +// ~/server/trpc/index.ts +import { z } from 'zod' + +export const router = trpc + .router() + .query('hello', { + // validate output with Zod + output: z.object({ + greeting: z.string() + }), + // expects return type of { greeting: string } + resolve() { + return { + greeting: 'hello!', + } + }, + }) +```