feat: add response body validation via validateResponse option#317
Merged
Conversation
Contributor
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||
commit: |
Add a validateResponse router option that parses response bodies through their Zod schema. When enabled, unknown fields are stripped from JSON responses and missing required fields trigger validation errors. Works with both plain object returns and Response objects.
c9bfec3 to
101ce6d
Compare
…andling, and docs - Return 500 (not 400) for response validation failures via new ResponseValidationException (code 7013, isVisible: false) - console.error full ZodError details for server-side debugging - Isolate response validation in its own try/catch (no longer flows through handleError hook as a raw ZodError) - Delete Content-Length/Transfer-Encoding headers when reconstructing Response objects after field stripping - Clone Response before consuming body for defensive recovery - Add 7 new tests: non-JSON passthrough, null responses, default schema fallback, unmatched status codes, Hono+Response, passthroughErrors interaction, Zod defaults on responses - Update existing invalid-data tests to expect 500/7013 - Document validateResponse option in router options docs - Document ResponseValidationException in error handling docs - Expand changeset with usage examples and behavior details
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.
Summary
Closes #159
Adds a
validateResponserouter option that, when enabled, parses response bodies through their Zod response schema. This provides two key benefits:Usage
How it works
handle()returns, the response is parsed through the Zod schema for the matching status codeChanges
src/types.ts: AddedvalidateResponse?: booleantoRouterOptionsandRouteOptionssrc/openapi.ts: Forward the option to route instancessrc/route.ts: AddedgetResponseSchema()andvalidateResponse()methods, called inexecute()when the option is enabled