Improve page number validation for entityIssueDetails - #1203
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThe PR tightens ChangesPageNumber query validation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/middleware/entityIssueDetails.middleware.js`:
- Line 29: The pageNumber pipeline currently uses v.transform(parseInt) which
permits mixed-format strings like "2abc" or "2.5"; update the pipeline for the
pageNumber field to first enforce a digits-only string (e.g., via a regex or
v.pattern) before converting to a number, then transform to a Number and
validate with v.integer() and v.minValue(1). In other words, modify the
pageNumber validator (the v.optional(v.pipe(v.string(), v.transform(...),
v.number(), v.integer(), v.minValue(1)))) so it inserts a strict digits-only
check (e.g., pattern /^\d+$/) prior to the transform step and only then
parse/convert to an integer.
In `@test/unit/middleware/entityIssueDetails.middleware.test.js`:
- Around line 274-280: The test for non-numeric pageNumber currently only
asserts inside the catch block so it will silently pass if
v.parse(IssueDetailsQueryParams, ...) does not throw; update the test to
explicitly assert that an exception is thrown (e.g., wrap the parse call in an
expect(...).toThrow or add an explicit fail() after the parse) while still
verifying the caught error satisfies isValiError(error); target the v.parse call
with IssueDetailsQueryParams and the test case that uses pageNumber:
'TPO335.G19'.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 58cdafe6-4522-4b72-99b0-1422c185f20e
📒 Files selected for processing (2)
src/middleware/entityIssueDetails.middleware.jstest/unit/middleware/entityIssueDetails.middleware.test.js
We are receiving URLs with alphanumeric reference numbers instead of page numbers. We do not know where these originate from at this stage but they are currently generating 500 errors, which is: - not a good experience for users - flooding Sentry with unhandled 500 errors This change updates Valibot validation to assert that a number is passed. When a number is not passed, then we return a 400 Bad Request error, which is more appropriate and provides better feedback to both us for debugging, and to the user.
a9c576c to
d2727f9
Compare
Optimisation from code rabbit, where it identified cases where decimals would be accepted and routed to integers. e.g. 2.2 >> 2 Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Description
We are receiving URLs with alphanumeric reference numbers instead of page numbers. We do not know where these originate from at this stage but they are currently generating 500 errors, which is:
This change updates Valibot validation to assert that a number is passed. When a number is not passed, then we return a 400 Bad Request error, which is more appropriate and provides better feedback to both us for debugging, and to the user.
What type of PR is this? (check all applicable)
Related Tickets & Documents
Fix ValiError for Dev/Staging config#2626
QA Instructions, Screenshots, Recordings
Problem URL: https://provide.staging.planning.data.gov.uk/organisations/local-authority:LIV/tree-preservation-zone/missing%20associated%20entity/tree-preservation-order/entity/TPO335.G19
Before
Browser:

Sentry:

After
Added/updated tests?
We encourage you to keep the code coverage percentage at 80% and above.
QA sign off
[optional] Are there any post-deployment tasks we need to perform?
[optional] Are there any dependencies on other PRs or Work?
Summary by CodeRabbit
Bug Fixes
Tests