-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[12.x] Binary File Size Validation Support #56223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 12.x
Are you sure you want to change the base?
Conversation
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
287d154
to
48777b8
Compare
090d399
to
f0cf58b
Compare
f0cf58b
to
914b354
Compare
To me it would be better to just use |
@taylorotwell Updated! |
4429142
to
a032838
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, we should embrace Unit Value Objects:
File::default()->max(new readonly class {
public function __construct(
public int $size,
public BinaryUnit|InternationalUnit $unit = BinaryUnit::Bytes,
) {}
});
💡 Description
This PR introduces hybrid file size validation that automatically detects binary vs international units from suffixes while providing instance method fallbacks, solving the long-standing confusion between 1024-based and 1000-based file size calculations in Laravel applications.
Problem
Laravel's file validation exclusively uses international units (1000-based), creating confusion and validation failures when developers expect binary units (1024-based) that match operating system file displays.
Industry Standards Conflict:
Real-World Impact:
File::default()->max('1MB')
validationSolution
Hybrid Suffix Detection automatically determines unit system from IEC/ISO standard suffixes, with instance method fallbacks for naked numeric values.
Precedence Logic:
MiB
/GiB
/TiB
forces Binary,MB
/GB
/TB
forces Internationalbinary()
orinternational()
method calls⚙️ Usage Examples
Real-World Scenarios
Migration Support
Backward Compatibility
🏗️ Technical Implementation
Core Architecture
Intelligent Suffix Detection:
Multiplier Methods:
Mathematical Precision
Integer Precision Preservation:
Overflow Protection:
Enhanced Input Parsing:
"1,024MB"
,"2,048KiB"
" 2GB "
,"1.5 MB "
mb
,MB
,Mb
,mB
"0.5MiB"
,"1.5GB"
Supported Units
KiB
KB
MiB
MB
GiB
GB
TiB
TB
🪵 Changelog
src/Illuminate/Validation/Rules/File.php
tests/Validation/ValidationFileRuleTest.php
♻️ Backward Compatibility
Zero breaking changes - purely additive enhancement:
✅ Testing
Added 30 new tests covering comprehensive scenarios:
Hybrid Functionality
✓ Suffix precedence - Confirms
MiB
/GiB
override instance settings✓ Naked value fallback - Instance methods work for numeric values
✓ Mixed unit constraints - Complex international + binary combinations
✓ All binary suffixes -
KiB
,MiB
,GiB
,TiB
validationMathematical Precision
✓ Integer precision - Exact arithmetic for whole numbers
✓ Overflow protection - Large values on 32-bit systems
✓ Fractional calculations -
0.5MiB
,1.5GB
decimal handling✓ Boundary testing - Files at exact size limits
Input Processing
✓ Case insensitive -
mb
,MB
,Mb
,mB
all supported✓ Whitespace tolerance -
" 2GB "
,"1.5 MB "
handled correctly✓ Comma parsing -
"1,024MB"
with locale awareness✓ Error validation - Invalid suffixes and negative values rejected
Framework Integration
✓ Fluent chaining - All method combinations work seamlessly
✓ Backward compatibility - Existing validation rules unchanged
✓ Production scenarios - Real-world file size boundaries tested
✓ Cross-platform - 32-bit and 64-bit system compatibility
💁🏼♂️ The author is available for hire -- inquire at yitzwillroth@gmail.com.