Skip to content

fix: report file size limits correctly in image validation errors - #3717

Open
eeshsaxena wants to merge 1 commit into
ohcnetwork:developfrom
eeshsaxena:fix/humanize-bytes-rstrip
Open

fix: report file size limits correctly in image validation errors#3717
eeshsaxena wants to merge 1 commit into
ohcnetwork:developfrom
eeshsaxena:fix/humanize-bytes-rstrip

Conversation

@eeshsaxena

@eeshsaxena eeshsaxena commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

ImageSizeValidator._humanize_bytes formats a byte count and then trims it with rstrip(".0"):

return f"{f'{size:.2f}'.rstrip('.0')} {unit}"

rstrip takes a set of characters, not a suffix, so it keeps eating trailing . and 0 past the decimal point and into the number itself. Any round size that ends in a zero comes out wrong:

bytes message says should say
10 * 1024 1 KB 10 KB
100 * 1024 1 KB 100 KB
10 * 1024 * 1024 1 MB 10 MB
500 5 B 500 B
0 (empty) B 0 B

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:

f"{size:.2f}".rstrip("0").rstrip(".")

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

    • Improved image size limit messages to display accurate, human-readable values across bytes, kilobytes, and megabytes.
    • Ensured minimum and maximum image size validation errors report the configured thresholds consistently.
  • Tests

    • Added coverage for image size formatting and validation messages across multiple size units.

@eeshsaxena
eeshsaxena requested a review from a team as a code owner July 27, 2026 04:20
@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 18f6284a-bc03-4815-a41c-59981b5f3cda

📥 Commits

Reviewing files that changed from the base of the PR and between 6eb0df0 and e812d89.

📒 Files selected for processing (2)
  • care/utils/models/validators.py
  • care/utils/tests/test_image_validator.py

📝 Walkthrough

Walkthrough

Changes

Image size validation

Layer / File(s) Summary
Format limits and validate messages
care/utils/models/validators.py, care/utils/tests/test_image_validator.py
ImageSizeValidator centralizes fixed-point size formatting, accepts floating-point byte values when humanizing sizes, and tests exact minimum and maximum validation messages across byte units.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: vigneshhari

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is informative but does not follow the required template and omits the Proposed Changes, Associated Issue, and Merge Checklist sections. Rewrite it using the repository template, including the required sections and a completed merge checklist.
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 (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main fix to image validation error reporting.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@greptile-apps

greptile-apps Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Corrects 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/5

The 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.

Important Files Changed

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 eeshsaxena 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.

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.

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