Skip to content

[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
wants to merge 5 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions reference/constraints/SemVer.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
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"/>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/Package.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 65 in reference/constraints/SemVer.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
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
-------

``strict``
~~~~~~~~~~

**type**: ``boolean`` **default**: ``true``

When set to ``true``, the version must strictly follow the official
`Semantic Versioning`_ specification. This means:

- No "v" prefix is allowed (use "1.2.3", not "v1.2.3")
- A full version is required (major.minor.patch)

When set to ``false``, common version variations are allowed:

- The "v" prefix is accepted (e.g., "v1.2.3")
- Partial versions are valid (e.g., "1", "1.2")

.. configuration-block::

.. code-block:: php-attributes

// src/Entity/Package.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Package
{
#[Assert\SemVer(strict: false)]
protected string $version;
}

.. code-block:: yaml

# config/validator/validation.yaml
App\Entity\Package:
properties:
version:
- SemVer:
strict: 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="strict">false</option>
</constraint>
</property>
</class>
</constraint-mapping>

.. code-block:: php

// src/Entity/Package.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 145 in reference/constraints/SemVer.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Package
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('version', new Assert\SemVer([
'strict' => false,
]));
}
}

.. include:: /reference/constraints/_groups-option.rst.inc

.. include:: /reference/constraints/_payload-option.rst.inc

Valid Version Examples
----------------------

When using ``strict: true`` (default), the following are valid:

- ``1.2.3`` (full version)
- ``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)

When using ``strict: false``, additional formats are accepted:

- ``1`` (partial version)
- ``1.2`` (partial version)
- ``v1.2.3`` (with "v" prefix)
- ``v1.2.3-alpha`` (prefix with pre-release)

.. _`Semantic Versioning`: https://semver.org/
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ String Constraints
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
* :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
* :doc:`Regex </reference/constraints/Regex>`
* :doc:`SemVer </reference/constraints/SemVer>`
* :doc:`Twig </reference/constraints/Twig>`
* :doc:`Ulid </reference/constraints/Ulid>`
* :doc:`Url </reference/constraints/Url>`
Expand Down
Loading