-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add SemVer
constraint for semantic versioning validation
#60995
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: 7.4
Are you sure you want to change the base?
Conversation
SemVer
constraint for semantic versioning validation
Is checking for a version really common enough to justify adding it to the Validator component? |
I also thought about just using regex, but as you can see, the regex(es) itself are quite complicated, so I thought it would be a nice addition. Ofc lets wait what others say |
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
Do you agree with the default |
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
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.
Would it be useful to add the $min
and $max
parameters using version_compare
to check the value?
Great idea 💡 I can do that in a follow up PR 🙋♂️ |
Min and max must then be a "valid" semantic version based on "strict" parameter, right? |
Let's do it here :) |
Will do it tomorrow 🙌 |
- Replace requirePrefix, allowPreRelease, and allowBuildMetadata with a single 'strict' boolean option - When strict=true: follows official SemVer spec (no 'v' prefix, requires full version) - When strict=false: allows common variations (parti 8000 al versions, 'v' prefix) - Update tests to reflect the new behavior
- Default behavior now follows the official SemVer specification - Users must explicitly set strict=false to allow loose validation
- Remove off on off off off off off off off off on off on off on off off off on off off off on on off off off on on off off off off off off off on off off off off on off on off off off off off on off on on off off off on off on on off on on off off on on on off on on off on off off off off on off off off on off off on off on off off off on on off on off on off off on off off off on off off off off off off off off on off on off on on on off on on off off on off on on on off on on off on off on off off off off off on on off off on off off off off off on off off on on off on off off on off off off on off off off off on on off on off off off off off on off on off off off off off off off off off off on on off on off off off parameter from constructor as array-based configuration is deprecated in Symfony 8.0 - Only support named arguments for configuration - This follows the modern Symfony constraint pattern
- Convert regex patterns to multi-line format with x modifier - Add inline comments to explain each part of the pattern - Makes complex regex patterns more maintainable
- Add HasNamedArguments attribute for XML/YAML mapping compatibility - Move property defaults to constructor arguments as non-nullable - Follow modern Symfony constraint pattern
Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>
- Add min and max parameters to SemVer constraint - Implement version comparison logic using version_compare() - Add normalizeVersion() method to handle version normalization - Add comprehensive test coverage for min/max functionality - Validate min/max parameters follow strict mode rules
ceb986e
to
a62f9f9
Compare
src/Symfony/Component/Validator/Tests/Constraints/SemVerValidatorTest.php
Outdated
Show resolved
Hide resolved
… strict parameter required in data providers
cfe6583
to
5dce356
Compare
src/Symfony/Component/Validator/Tests/Constraints/SemVerValidatorTest.php
Outdated
Show resolved
Hide resolved
Done! |
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.
Excellent!
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/SemVerValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/SemVerValidatorTest.php
Outdated
Show resolved
Hide resolved
…torTest.php Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>
…torTest.php Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>
As per Alex Daubois's review comments, removed explanatory comments that were deemed unnecessary for clean, self-documenting code.
done @alexandre-daubois |
Description
This PR adds a new
SemVer
constraint to validate semantic versioning strings according to the Semantic Versioning 2.0.0 specification.Options
Specific Options
strict
(default:true
): Whether to use strict SemVer validation (no 'v' prefix, no partial versions) or loose validationmin
(default:null
): Minimum allowed version (usesversion_compare()
for comparison)max
(default:null
): Maximum allowed version (usesversion_compare()
for comparison)message
(default:'This value is not a valid semantic version.'
): Message for invalid version formatminMessage
(default:'This value should be {{ min }} or more.'
): Message when version is below minimummaxMessage
(default:'This value should be {{ max }} or less.'
): Message when version exceeds maximumDefault Options
groups
: The validation groups this constraint belongs topayload
: Domain-specific data attached to the constraintFeatures
Basic Validation
The constraint validates version strings with two modes:
1.2.3
,1.0.0-alpha
,1.2.3+build123
)v1.2.3
,1.2
,1
)Version Range Validation
The constraint supports
min
andmax
options to validate version ranges:Usage Examples
Basic usage (strict mode by default):
Loose mode:
With version constraints:
Custom error messages:
Implementation Details
version_compare()
for min/max comparisons to ensure correct semantic version ordering