feat(zod-openapi): optional response validation (strict flags, hooks)#1819
Open
mikan3rd wants to merge 8 commits intohonojs:mainfrom
Open
feat(zod-openapi): optional response validation (strict flags, hooks)#1819mikan3rd wants to merge 8 commits intohonojs:mainfrom
mikan3rd wants to merge 8 commits intohonojs:mainfrom
Conversation
…oks) Made-with: Cursor
🦋 Changeset detectedLatest commit: ad3c514 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Added tests to validate response behavior for various status codes and response schemas, ensuring strict adherence to defined responses. This includes handling cases where status is set but not declared, and validating response bodies against multiple schemas.
…e validation Updated comments in the response validation code to enhance clarity, including more descriptive error messages and streamlined documentation for the `ResponseHook` type. This change aims to improve code readability and maintainability.
Introduced a new type, BuiltinStrictValidationJson, to streamline response body type definitions in tests. This change enhances code clarity and reduces redundancy in type assertions for response validation scenarios.
…provements Enhanced the README to clarify the behavior of body-validation failures and the scope of strict response validation. Added a new test case to verify that strict response validation does not apply when middleware returns a response without calling `next()`. This improves documentation and ensures better understanding of response handling in the OpenAPIHono framework.
Refined existing tests to cover additional edge cases in response validation, ensuring comprehensive coverage for various response scenarios. This update enhances the robustness of the validation logic and improves overall test reliability.
Made-with: Cursor
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1819 +/- ##
==========================================
+ Coverage 91.71% 91.76% +0.05%
==========================================
Files 113 113
Lines 3803 3900 +97
Branches 964 990 +26
==========================================
+ Hits 3488 3579 +91
- Misses 283 289 +6
Partials 32 32
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Approach
Opt-in response checks aligned with PR #184 (
strictStatusCode/strictResponse), with maintainer direction in mind: validate JSON before aResponseexists by temporarily wrappingc.jsonandc.statusfor theopenapiroute handler, instead of re-reading the body from a finished response.Failure handling is configurable via
defaultResponseHookand per-routeresponseHook(thirdopenapiargument may be a requestHookor{ hook?, responseHook? }). If neither hook returns aResponse, a small JSON 500 body is sent.Caveats / assumptions
c.json(andc.statuswhen inferring status).c.text/c.html/c.bodyandreturn new Response(...)are unchanged.middlewarethat returns withoutnext()bypasses strict checks;c.jsononly after the handler returns is also outside the wrapped window.contententries,strictResponseuses the first (object key order).1XX–5XX, uppercaseX).issues; production apps should use hooks to redact or simplify payloads.strictResponseis skipped when the matched response has no JSON Zod schema (e.g. 204 with no body schema).See README Response validation and Scope and limitations for the full contract;
openapiJSDoc summarizes the hooks object and points there.