-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Validator] Add documentation for the new SemVer
constraint
#21162
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
5
commits into
symfony:7.4
Choose a base branch
from
OskarStark:document-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.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fb37120
Add documentation for the new SemVer constraint
OskarStark f584de1
Update reference/constraints/SemVer.rst
OskarStark 6795cf5
Update SemVer constraint documentation to reflect latest changes
OskarStark 246e93e
fix
OskarStark bcb7e01
Update reference/constraints/SemVer.rst
OskarStark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
SemVer | ||
====== | ||
|
||
Validates that a value is a valid semantic version string according to the | ||
`Semantic Versioning`_ specification. This constraint supports various | ||
version formats including partial versions, pre-release versions, and | ||
build metadata. | ||
|
||
.. versionadded:: 7.4 | ||
|
||
The ``SemVer`` constraint was introduced in Symfony 7.4. | ||
|
||
========== =================================================================== | ||
Applies to :ref:`property or method <validation-property-target>` | ||
Class :class:`Symfony\\Component\\Validator\\Constraints\\SemVer` | ||
Validator :class:`Symfony\\Component\\Validator\\Constraints\\SemVerValidator` | ||
========== =================================================================== | ||
|
||
Basic Usage | ||
----------- | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-attributes | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Package | ||
{ | ||
#[Assert\SemVer] | ||
protected string $version; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\Package: | ||
properties: | ||
version: | ||
- SemVer: ~ | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/validator/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="App\Entity\Package"> | ||
<property name="version"> | ||
<constraint name="SemVer" /> | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class Package | ||
{ | ||
// ... | ||
|
||
public static function loadValidatorMetadata(ClassMetadata $metadata): void | ||
{ | ||
$metadata->addPropertyConstraint('version', new Assert\SemVer()); | ||
} | ||
} | ||
|
||
.. include:: /reference/constraints/_empty-values-are-valid.rst.inc | ||
|
||
Options | ||
------- | ||
|
||
``requirePrefix`` | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
**type**: ``boolean`` **default**: ``false`` | ||
|
||
When set to ``true``, the version string must start with a "v" prefix | ||
(e.g., "v1.2.3" instead of "1.2.3"). | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-attributes | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Package | ||
{ | ||
#[Assert\SemVer(requirePrefix: true)] | ||
protected string $version; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\Package: | ||
properties: | ||
version: | ||
- SemVer: | ||
requirePrefix: true | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/validator/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="App\Entity\Package"> | ||
<property name="version"> | ||
<constraint name="SemVer"> | ||
<option name="requirePrefix">true</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class Package | ||
{ | ||
// ... | ||
|
||
public static function loadValidatorMetadata(ClassMetadata $metadata): void | ||
{ | ||
$metadata->addPropertyConstraint('version', new Assert\SemVer([ | ||
'requirePrefix' => true, | ||
])); | ||
} | ||
} | ||
|
||
``allowPreRelease`` | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
**type**: ``boolean`` **default**: ``true`` | ||
|
||
Whether to allow pre-release versions (e.g., "1.2.3-beta", "2.0.0-rc.1"). | ||
When set to ``false``, only stable versions are considered valid. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-attributes | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Package | ||
{ | ||
#[Assert\SemVer(allowPreRelease: false)] | ||
protected string $version; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\Package: | ||
properties: | ||
version: | ||
- SemVer: | ||
allowPreRelease: false | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/validator/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="App\Entity\Package"> | ||
<property name="version"> | ||
<constraint name="SemVer"> | ||
<option name="allowPreRelease">false</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class Package | ||
{ | ||
// ... | ||
|
||
public static function loadValidatorMetadata(ClassMetadata $metadata): void | ||
{ | ||
$metadata->addPropertyConstraint('version', new Assert\SemVer([ | ||
'allowPreRelease' => false, | ||
])); | ||
} | ||
} | ||
|
||
``allowBuildMetadata`` | ||
~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
**type**: ``boolean`` **default**: ``true`` | ||
|
||
Whether to allow build metadata in the version string (e.g., "1.2.3+20130313144700"). | ||
When set to ``false``, build metadata is not allowed. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: php-attributes | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class Package | ||
{ | ||
#[Assert\SemVer(allowBuildMetadata: false)] | ||
protected string $version; | ||
} | ||
|
||
.. code-block:: yaml | ||
|
||
# config/validator/validation.yaml | ||
App\Entity\Package: | ||
properties: | ||
version: | ||
- SemVer: | ||
allowBuildMetadata: false | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/validator/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
|
||
<class name="App\Entity\Package"> | ||
<property name="version"> | ||
<constraint name="SemVer"> | ||
<option name="allowBuildMetadata">false</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
|
||
.. code-block:: php | ||
|
||
// src/Entity/Package.php | ||
namespace App\Entity; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
|
||
class Package | ||
{ | ||
// ... | ||
|
||
public static function loadValidatorMetadata(ClassMetadata $metadata): void | ||
{ | ||
$metadata->addPropertyConstraint('version', new Assert\SemVer([ | ||
'allowBuildMetadata' => false, | ||
])); | ||
} | ||
} | ||
|
||
.. include:: /reference/constraints/_groups-option.rst.inc | ||
|
||
.. include:: /reference/constraints/_payload-option.rst.inc | ||
|
||
Valid Version Examples | ||
---------------------- | ||
|
||
The following are examples of valid semantic versions: | ||
|
||
- ``1`` (partial version) | ||
- ``1.2`` (partial version) | ||
- ``1.2.3`` (full version) | ||
- ``v1.2.3`` (with prefix) | ||
- ``1.2.3-alpha`` (pre-release) | ||
- ``1.2.3-beta.1`` (pre-release with numeric identifier) | ||
- ``1.2.3+20130313144700`` (with build metadata) | ||
- ``1.2.3-beta+exp.sha.5114f85`` (pre-release and build metadata) | ||
|
||
.. _`Semantic Versioning`: https://semver.org/ |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.