Skip to content

fix(void): type c.req.valid() after Hono validator middleware - #1069

Open
morinokami wants to merge 2 commits into
voidzero-dev:mainfrom
morinokami:fix/hono-validator-types
Open

morinokami wants to merge 2 commits into
voidzero-dev:mainfrom
morinokami:fix/hono-validator-types

Conversation

@morinokami

Copy link
Copy Markdown
Collaborator

The Hono guide says that any hono/* or @hono/* package works. Validator middleware runs in defineHandler(middleware, handler), but its types don't reach the handler:

// routes/api/items.ts
import { zValidator } from '@hono/zod-validator';
import { validator } from 'hono/validator';
import { defineHandler } from 'void';
import { z } from 'zod';

export const POST = defineHandler(zValidator('json', z.object({ name: z.string() })), (c) => {
  const { name } = c.req.valid('json');
  return { name };
});

export const PUT = defineHandler(
  validator('json', (value, c) => {
    if (typeof value.name !== 'string') return c.text('name is required', 400);
    return { name: value.name as string };
  }),
  (c) => c.req.valid('json'),
);
main This PR
c.req.valid('json') in POST TS2345: Argument of type '"json"' is not assignable to parameter of type 'never' { name: string }
The validator in PUT, which returns c.text() TS2769: No overload matches this call Accepted

This rarely comes up, since Void apps usually validate with defineHandler.withValidator(), which is typed. That is probably why it went unnoticed. An app that does use a Hono validator, though, gets type errors for code that works at runtime and type-checks in a plain Hono app.

CloudContext had no input type, so c.req.valid() accepted no target, and MiddlewareFn allowed only Promise<Response | void>, which a validator's typed response does not fit.

Fix

  • CloudContext takes Hono's input type as an optional type parameter. defineHandler() infers each middleware and passes what the validators return to the handler's c. The data adds up across middleware, as in Hono's app.post(path, ...middleware, handler).
  • Middleware may return a TypedResponse.
  • defineMiddleware() keeps the types of a validator it wraps, so defineMiddleware(zValidator(...)) types the handler too. This goes beyond the two routes above. For other middleware it returns MiddlewareHandler<CloudEnv> as before.

Calls without a validator keep their types. The handler's c is the same CloudContext, and defineHandler<Props>(middleware, handler), inline middleware, a factory called in place such as createMiddleware(), and a typed withValidator leaf after middleware behave as on main.

Two things to know:

  • Hovers and error messages now print CloudContext<BlankInput> where they printed CloudContext.
  • With an explicit type argument, as in defineHandler<Props>(validator, handler), TypeScript infers nothing from the middleware, so c.req.valid() stays as on main. An annotated return type, (c): Props => ..., keeps the validator types.

Docs

  • docs/integrations/hono.md: validator middleware types c.req.valid() in the route handler. The typed fetch client does not read these validators, so defineHandler.withValidator() remains the way to check requests at the call site.
  • docs/reference/api/types.md: the type argument of CloudContext.

Tests

  • packages/void/test/unit/define-handler-middleware.typecheck.ts (new, 16 cases): the types after validators, and the calls without one that must keep theirs. With main's handler/index.ts it reports 48 errors.
  • packages/void/test/unit/define-handler.test.ts (1 case): a validator() passes its data to the handler, and its 400 response is returned.
  • packages/void/test/integration/route-url-builder.test.ts: with typed routes, an unknown c.redirect() target is still rejected in inline middleware and in a handler after a validator.

Verification

  • vp run test: builds the packages and runs the platform, unit and integration groups; 202/202 tasks pass.
  • vp check and vp run check:types (37/37) pass.
  • The built declarations with @hono/zod-validator 0.9.1, on TypeScript 5.9.3, 6.0.3, 7.0.2 and tsrs 7.1.0-beta: the routes above type-check, and about 40 calls that main accepts (Hono's built-in middleware, basicAuth(), inline middleware, defineHandler<Props>, typed leaves, wrappers) get the same diagnostics as with main's declarations.
  • handler/index.ts compiles to the same JavaScript as on main.

🤖 Generated with Claude Code

morinokami and others added 2 commits December 7, 2026 01:35
In `defineHandler(middleware..., handler)`, a Hono validator such as
`zValidator()` or `validator()` ran, but its types did not reach the handler:
`c.req.valid()` accepted no target, and a validator that returns a typed
response was rejected.

- Infer each middleware as a whole and pass what the validators return to
  the handler's `c`. `CloudContext` takes an optional Hono input type.
- `defineMiddleware()` keeps the types of a validator it wraps.
- A middleware typed with `CloudContext<{ out: ... }>` reads the validated
  data after the validator.
- A middleware factory called in place, such as a rate limiter with a key
  function, still gets CloudEnv for its callbacks.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The overload that matched no call gave a call in the handler position no
expected type: in `defineHandler(auth, traced((c) => ...))`, `c` was `any` or
`unknown` instead of CloudContext.

- Take each middleware as `M | MiddlewareFn`. A middleware factory called in
  place keeps CloudEnv for its callbacks without the two overloads that
  matched no call, and a wrapper around the handler keeps CloudContext.
- Add up what the middleware validate, as Hono does. This replaces the rule
  that the last validator wins for a target, and the separate handling of a
  middleware that only reads validated data.
- Reject a middleware that needs more than CloudContext again.
- Keep the names and return types of main where they need no change.
- Describe the type argument of `CloudContext` in the API reference.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant