tRPC Procedure with Zod Validation

fullstack
TypeScript
scaffolding
minimalist
Remix

Type-safe tRPC procedure with input validation and proper error handling.

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 protectedProcedure middleware
  • 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 TRPCError with appropriate codes:
    • UNAUTHORIZED - Authentication failed
    • BAD_REQUEST - Invalid input
    • INTERNAL_SERVER_ERROR - Server errors
    • NOT_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

Comments (0)

Sign in to leave a comment

Sign In