tRPC Procedure with Zod Validation
fullstack
TypeScript
scaffolding
minimalist
Type-safe tRPC procedure with input validation and proper error handling.
By ethan_w
12/8/2025
Prompt
tRPC Procedure with Zod Validation
Create a tRPC procedure for [Operation] with end-to-end type safety.
Requirements
1. Input Validation
- Use Zod schema with detailed validation rules
- Type-safe input parsing
- Custom validation logic if needed
2. Authentication
- Use
protectedProceduremiddleware - Verify user authentication
- Access user context via
ctx.session
3. Database Interaction
- Proper error handling for queries
- Use transactions when needed
- Type-safe database operations
4. Typed Response
- Return properly typed data
- Inferred types on client side
5. Error Handling
- Use
TRPCErrorwith appropriate codes:UNAUTHORIZED- Authentication failedBAD_REQUEST- Invalid inputINTERNAL_SERVER_ERROR- Server errorsNOT_FOUND- Resource not found
6. Documentation
- Add JSDoc comments
- Explain procedure purpose
Example Structure
Server (procedure definition)
export const [procedureName] = protectedProcedure
.input(z.object({ ... }))
.mutation(async ({ ctx, input }) => {
// implementation
});
Client Usage
const result = await trpc.[procedureName].mutate({ ... });
Show both server-side definition and client-side usage.
Tags
trpc
typescript
zod
type-safety
Tested Models
gpt-4-turbo
claude-3-5-sonnet