Skip to content

fix(validate): convert malformed JSON to 400 in validated-handler path#1465

Merged
pi0 merged 1 commit into
mainfrom
fix/validated-json-400
Jul 15, 2026
Merged

fix(validate): convert malformed JSON to 400 in validated-handler path#1465
pi0 merged 1 commit into
mainfrom
fix/validated-json-400

Conversation

@pi0x

@pi0x pi0x commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

A rejected req.json() in the validated-request proxy (_validatedJson) was not caught, so a malformed JSON body sent to a defineValidatedHandler surfaced as an unhandled 500 — inconsistent with readBody, which returns a 400 "Invalid JSON body".

This wraps the .json() call so a parse failure becomes a 400 HTTPError with message "Invalid JSON body". Adds a regression test covering malformed JSON to a validated handler.

🤖 Generated with Claude Code

A rejected `req.json()` in the validated-request proxy surfaced as an
unhandled 500. Wrap the parse so a malformed JSON body becomes a 400
"Invalid JSON body", consistent with `readBody`.

Co-Authored-By: Claude Fable 5 <[email protected]>
@pi0x
pi0x requested a review from pi0 as a code owner July 14, 2026 12:34
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@pi0x pi0x left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review — LGTM (posting as comment since GitHub blocks self-approval on this account).

Clean, minimal fix that closes a real gap: a malformed JSON body to a defineValidatedHandler previously surfaced as an unhandled 500, now a 400.

Verified locally on the branch:

  • Error shape is byte-for-byte consistent with readBody (src/utils/body.ts): status: 400, statusText: "Bad Request", message: "Invalid JSON body".
  • The .catch() sits only on req.json(), before the validation .then() chain, so schema/validation errors are untouched and keep their existing createValidationError shape/status.
  • The regression test genuinely fails without the fix (asserted 500 before, 400 after) and passes with it. It correctly satisfies header/query validation first so the JSON-parse path is actually exercised.
  • pnpm vitest run test/handler.test.ts and pnpm lint both green.

Two minor, non-blocking notes (both edge cases and strict improvements over the prior 500):

  1. req.json() bundles body-reading + parsing, so a genuine body/stream read error would also be reported as 400 "Invalid JSON body". readBody wraps only JSON.parse (not req.text()), so it would surface such a read error as 500. Rare, but a slight semantic divergence if you want the two paths to treat internal read failures identically.
  2. Empty body now returns 400 "Invalid JSON body" here, whereas readBody treats an empty body as undefined and hands it to the schema. Not wrong, just worth being aware the two paths diverge for empty bodies.

Neither blocks merge.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/validated-json-400

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pi0
pi0 merged commit aa46942 into main Jul 15, 2026
10 checks passed
@pi0
pi0 deleted the fix/validated-json-400 branch July 15, 2026 08:27
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