fix: report file size limits correctly in image validation errors - #3717
fix: report file size limits correctly in image validation errors#3717eeshsaxena wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughChangesImage size validation
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches🧪 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 |
Greptile SummaryCorrects image-size validation messages by safely trimming fixed-point zeros and adds public-validator regression tests for byte, kilobyte, and megabyte limits. Confidence Score: 5/5The PR appears safe to merge with no actionable defects identified. The formatter now removes only fractional trailing zeros and a remaining decimal point, while the added tests exercise the intended validation path across representative limits.
|
| Filename | Overview |
|---|---|
| care/utils/models/validators.py | Extracts safe numeric formatting so significant trailing zeros in configured image-size limits are preserved. |
| care/utils/tests/test_image_validator.py | Adds regression coverage for correctly rendered minimum and maximum size validation messages. |
Reviews (1): Last reviewed commit: "fix: report file size limits correctly i..." | Re-trigger Greptile
eeshsaxena
left a comment
There was a problem hiding this comment.
Good catch, the old formatting was genuinely wrong. f'{size:.2f}'.rstrip('.0') strips every trailing . and 0 character, so it mangled significant digits: 500.00 becomes 5, 10.00 becomes 1, 20.00 becomes 2. So the error messages were reporting a truncated limit.
The new _format_size does .rstrip('0').rstrip('.'), which only drops trailing zeros after the decimal and then a single trailing dot. The . stops the first rstrip from eating into the integer part (200.00 -> 200, 1.50 -> 1.5, 2.00 -> 2), so whole numbers like 500 and 10 survive intact now. The added subTest matrix over B/KB/MB and the boundary covers it well. Correct.
ImageSizeValidator._humanize_bytesformats a byte count and then trims it withrstrip(".0"):rstriptakes a set of characters, not a suffix, so it keeps eating trailing.and0past the decimal point and into the number itself. Any round size that ends in a zero comes out wrong:So a validator configured with a 10 MB ceiling tells the user their image is "greater than the maximum allowed size of 1 MB".
The two values wired up today,
cover_image_validator's 1 KB minimum and 2 MB maximum, happen to survive the strip, which is why the existing tests pass and nobody has hit it yet. Any other limit is misreported.Fix is to strip the trailing zeros first and the bare decimal point second, which cannot reach a significant digit:
Added two tests that assert the rendered message for 500 B, 1.5 KB, 10 KB and 10 MB limits, going through the public validator rather than the helper. Both fail on the current code.
A note on verification: I could not run the Django suite here (no local Postgres or project settings), so I checked the formatting change by running the old and new helper side by side over 12 sizes, and confirmed ruff check and ruff format are clean on both files. The assertions in the new tests are the exact strings from the message templates, but CI is the real check.
Summary by CodeRabbit
Bug Fixes
Tests