-
-
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
Open
OskarStark
wants to merge
14
commits into
symfony:7.4
Choose a base branch
from
OskarStark:feature/semver-constraint
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+337
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add a new constraint to validate semantic versioning strings according to the SemVer 2.0.0 specification. Supports partial versions (e.g., "3", "3.1"), full versions (e.g., "3.1.2"), optional "v" prefix, pre-release versions (e.g., "3.1.2-beta"), and build metadata (e.g., "3.1.2+20130313144700"). Configurable options: - requirePrefix: Enforce the "v" prefix - allowPreRelease: Allow pre-release versions (default: true) - allowBuildMetadata: Allow build metadata (default: true)
SemVer
constraint for semantic versioning validation
- Add missing newlines at end of files - Break up long regex patterns with inline comments for better readability - Convert PHPDoc @dataProvider annotations to PHP 8 attributes
- Fix LOOSE_SEMVER_PATTERN to only allow pre-release and build metadata with full version (major.minor.patch) - Remove empty string from invalid test cases as it's handled separately - Revert to PHPDoc @dataProvider annotations for PHPUnit 9.6 compatibility
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
- 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 (partial versions, 'v' prefix) - Update tests to reflect the new behavior
OskarStark
commented
Jul 1, 2025
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
- 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
Do you agree with the default |
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
stof
reviewed
Jul 1, 2025
- 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
OskarStark
commented
Jul 1, 2025
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
OskarStark
commented
Jul 1, 2025
src/Symfony/Component/Validator/Constraints/SemVerValidator.php
Outdated
Show resolved
Hide resolved
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add a new constraint to validate semantic versioning strings according to the SemVer 2.0.0 specification. Supports partial versions (e.g., "3", "3.1"), full versions (e.g., "3.1.2"), optional "v" prefix, pre-release versions (e.g., "3.1.2-beta"), and build metadata (e.g., "3.1.2+20130313144700").
Configurable options
strict
(default: true)