Skip to content

refactor(validate): drop always-true if (validate.body) guard in body proxy#1392

Open
francisjohnjohnston-web wants to merge 1 commit into
h3js:mainfrom
francisjohnjohnston-web:pil-h3-validate-redundant-guard
Open

refactor(validate): drop always-true if (validate.body) guard in body proxy#1392
francisjohnjohnston-web wants to merge 1 commit into
h3js:mainfrom
francisjohnjohnston-web:pil-h3-validate-redundant-guard

Conversation

@francisjohnjohnston-web

@francisjohnjohnston-web francisjohnjohnston-web commented May 24, 2026

Copy link
Copy Markdown

What

Removes an always-true if (validate.body) guard inside the lazy body-validation
Proxy created by validatedRequest, flattening one level of nesting and a dead
branch.

Why it's safe / why the guard is redundant

validatedRequest returns the unwrapped request when validate.body is unset:

if (!validate.body) {
  return req;
}
// Proxy created here — reached only when validate.body is truthy
return new Proxy(req, { get(_t, prop) { if (validate.body) {  } } });

So by the time the Proxy's get handler runs, validate.body is always truthy
— the inner if (validate.body) can never be false. The handler already relies on
this (it uses the non-null assertion validate.body! inside). Removing the guard
is behaviour-preserving and lets the body-prop checks sit one level shallower.

Why a reviewer might not have caught it

The redundant guard reads as ordinary defensiveness; it's only visible as dead once
you trace that the enclosing function early-returns for the !validate.body case,
so the proxy is conditionally constructed.

Scope

Internal helper only — no public API, signature, or behaviour change. All 38
validate tests pass; tsc --noEmit, oxlint, and oxfmt are clean.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved request body validation error handling with more consistent enforcement of the .json() method for accessing request bodies.
  • Performance

    • Optimized request validation proxy handler by removing unnecessary runtime checks, reducing overhead for request processing.

Review Change Stack

…dy proxy

`validatedRequest` returns the unwrapped request when `validate.body` is unset,
so the lazy body-validation Proxy is only ever created when `validate.body` is
truthy. The Proxy's `get` handler then re-checked `if (validate.body)` — a guard
that is always true at that point — adding a dead branch and a nesting level.
Remove it (behaviour-preserving; the existing `validate.body!` assertion already
assumed non-null) and note the invariant in a comment. All 38 validate tests
pass; tsc, oxlint, and oxfmt clean.
@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e526bda1-2e9a-443a-9615-ab9d4f5db8e7

📥 Commits

Reviewing files that changed from the base of the PR and between 84244b4 and 181d593.

📒 Files selected for processing (1)
  • src/utils/internal/validate.ts

📝 Walkthrough

Walkthrough

The proxy get handler in validatedRequest is simplified by removing a redundant if (validate.body) guard. Since the proxy is created only when validate.body is set, the handler now unconditionally intercepts .json() to validate the parsed JSON, blocks direct body-reading method access, and delegates other properties to Reflect.get.

Changes

Request body validation proxy

Layer / File(s) Summary
Proxy handler simplification
src/utils/internal/validate.ts
The get trap removes the redundant if (validate.body) check. The .json() accessor now unconditionally validates parsed JSON against the schema and throws createValidationError with onError when validation fails; blocked reqBodyKeys throw TypeError with usage guidance; other properties delegate to Reflect.get.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A proxy stood guard at the gate,
With logic too cautious, ornate—
The check was redundant (we always create!),
So we stripped it away, made it clean and first-rate.
Now simpler pathways let JSON validate! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: removing a redundant if (validate.body) guard from the body proxy's get handler. It directly relates to the core refactoring work.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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 and usage tips.

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