Repository navigation
fix(void): type c.req.valid() after Hono validator middleware - #1069
Open
morinokami wants to merge 2 commits into
Open
morinokami wants to merge 2 commits into
morinokami wants to merge 2 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Hono guide says that any
hono/*or@hono/*package works. Validator middleware runs indefineHandler(middleware, handler), but its types don't reach the handler:mainc.req.valid('json')inPOSTArgument of type '"json"' is not assignable to parameter of type 'never'{ name: string }PUT, which returnsc.text()No overload matches this callThis 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.CloudContexthad no input type, soc.req.valid()accepted no target, andMiddlewareFnallowed onlyPromise<Response | void>, which a validator's typed response does not fit.Fix
CloudContexttakes Hono's input type as an optional type parameter.defineHandler()infers each middleware and passes what the validators return to the handler'sc. The data adds up across middleware, as in Hono'sapp.post(path, ...middleware, handler).TypedResponse.defineMiddleware()keeps the types of a validator it wraps, sodefineMiddleware(zValidator(...))types the handler too. This goes beyond the two routes above. For other middleware it returnsMiddlewareHandler<CloudEnv>as before.Calls without a validator keep their types. The handler's
cis the sameCloudContext, anddefineHandler<Props>(middleware, handler), inline middleware, a factory called in place such ascreateMiddleware(), and a typedwithValidatorleaf after middleware behave as onmain.Two things to know:
CloudContext<BlankInput>where they printedCloudContext.defineHandler<Props>(validator, handler), TypeScript infers nothing from the middleware, soc.req.valid()stays as onmain. An annotated return type,(c): Props => ..., keeps the validator types.Docs
docs/integrations/hono.md: validator middleware typesc.req.valid()in the route handler. The typed fetch client does not read these validators, sodefineHandler.withValidator()remains the way to check requests at the call site.docs/reference/api/types.md: the type argument ofCloudContext.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. Withmain'shandler/index.tsit reports 48 errors.packages/void/test/unit/define-handler.test.ts(1 case): avalidator()passes its data to the handler, and its400response is returned.packages/void/test/integration/route-url-builder.test.ts: with typed routes, an unknownc.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 checkandvp run check:types(37/37) pass.@hono/zod-validator0.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 thatmainaccepts (Hono's built-in middleware,basicAuth(), inline middleware,defineHandler<Props>, typed leaves, wrappers) get the same diagnostics as withmain's declarations.handler/index.tscompiles to the same JavaScript as onmain.🤖 Generated with Claude Code