Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@productdevbook
Copy link
Member

@productdevbook productdevbook commented Nov 3, 2025

Summary

cc: #1088

This PR enhances H3's validation capabilities with comprehensive type-safe validation for routes and handlers:

  • Route params validation: Added support for validating route parameters (e.g., /users/:id) with full type inference
  • Response validation: Server-side response validation to ensure handlers return correctly shaped data
  • Enhanced defineValidatedHandler: Now supports params and response schemas in addition to existing body, headers, and query validation
  • Enhanced defineRoute: Full type-safe validation with proper TypeScript overloads for better DX
  • New utilities: Exported syncValidate for synchronous validation and validateResponse for response validation
  • Comprehensive tests: Added full test coverage including integration tests demonstrating complete validation workflows

Key Features

1. Params Validation

const handler = defineValidatedHandler({
  validate: {
    params: z.object({ id: z.string().uuid() })
  },
  handler: (event) => {
    // event.context.params is typed as { id: string }
    const id = event.context.params.id;
    return { userId: id };
  }
});

2. Response Validation

const route = defineRoute({
  method: 'POST',
  route: '/api/users',
  validate: {
    response: z.object({ id: z.string(), name: z.string() })
  },
  handler: async (event) => {
    // Return type is validated and type-checked
    return { id: '123', name: 'John' };
  }
});

3. Complete Validation

defineRoute({
  method: 'POST',
  route: '/api/users/:id',
  validate: {
    params: z.object({ id: z.string().uuid() }),
    query: z.object({ include: z.string().optional() }),
    body: z.object({ name: z.string(), email: z.string().email() }),
    response: z.object({ id: z.string(), name: z.string(), email: z.string() })
  },
  handler: async (event) => {
    // All types are inferred: params, query, body, and return type
    const { id } = event.context.params;
    const body = await event.req.json();
    return { id, name: body.name, email: body.email };
  }
});

Test Plan

  • Unit tests for params validation in defineValidatedHandler
  • Unit tests for response validation in defineValidatedHandler
  • Integration tests for defineRoute with all validation schemas
  • Error handling tests for invalid params, query, body, headers, and response
  • Type inference tests demonstrating full type safety
  • Existing tests continue to pass

🤖 Generated with Claude Code

- Added support for response validation in defineValidatedHandler
- Introduced syncValidate function for synchronous validation
- Updated defineRoute to include comprehensive validation schemas
- Expanded tests for full validation type inference and error handling
@productdevbook productdevbook requested a review from pi0 as a code owner November 3, 2025 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants