Skip to content

Commit 6795cf5

Browse files
committed
Update SemVer constraint documentation to reflect latest changes
- Remove requirePrefix, allowPreRelease, and allowBuildMetadata options - Add strict option (default: true) as the only configuration option - Update examples to show behavior with strict mode enabled/disabled
1 parent f584de1 commit 6795cf5

File tree

1 file changed

+21
-147
lines changed

1 file changed

+21
-147
lines changed

reference/constraints/SemVer.rst

Lines changed: 21 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -80,151 +80,21 @@ Basic Usage
8080
Options
8181
-------
8282

83-
``requirePrefix``
84-
~~~~~~~~~~~~~~~~~
85-
86-
**type**: ``boolean`` **default**: ``false``
87-
88-
When set to ``true``, the version string must start with a "v" prefix
89-
(e.g., "v1.2.3" instead of "1.2.3").
90-
91-
.. configuration-block::
92-
93-
.. code-block:: php-attributes
94-
95-
// src/Entity/Package.php
96-
namespace App\Entity;
97-
98-
use Symfony\Component\Validator\Constraints as Assert;
99-
100-
class Package
101-
{
102-
#[Assert\SemVer(requirePrefix: true)]
103-
protected string $version;
104-
}
105-
106-
.. code-block:: yaml
107-
108-
# config/validator/validation.yaml
109-
App\Entity\Package:
110-
properties:
111-
version:
112-
- SemVer:
113-
requirePrefix: true
114-
115-
.. code-block:: xml
116-
117-
<!-- config/validator/validation.xml -->
118-
<?xml version="1.0" encoding="UTF-8" ?>
119-
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
120-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
121-
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
122-
123-
<class name="App\Entity\Package">
124-
<property name="version">
125-
<constraint name="SemVer">
126-
<option name="requirePrefix">true</option>
127-
</constraint>
128-
</property>
129-
</class>
130-
</constraint-mapping>
131-
132-
.. code-block:: php
133-
134-
// src/Entity/Package.php
135-
namespace App\Entity;
136-
137-
use Symfony\Component\Validator\Constraints as Assert;
138-
use Symfony\Component\Validator\Mapping\ClassMetadata;
139-
140-
class Package
141-
{
142-
// ...
143-
144-
public static function loadValidatorMetadata(ClassMetadata $metadata): void
145-
{
146-
$metadata->addPropertyConstraint('version', new Assert\SemVer([
147-
'requirePrefix' => true,
148-
]));
149-
}
150-
}
151-
152-
``allowPreRelease``
153-
~~~~~~~~~~~~~~~~~~~
83+
``strict``
84+
~~~~~~~~~~
15485

15586
**type**: ``boolean`` **default**: ``true``
15687

157-
Whether to allow pre-release versions (e.g., "1.2.3-beta", "2.0.0-rc.1").
158-
When set to ``false``, only stable versions are considered valid.
159-
160-
.. configuration-block::
161-
162-
.. code-block:: php-attributes
163-
164-
// src/Entity/Package.php
165-
namespace App\Entity;
166-
167-
use Symfony\Component\Validator\Constraints as Assert;
168-
169-
class Package
170-
{
171-
#[Assert\SemVer(allowPreRelease: false)]
172-
protected string $version;
173-
}
174-
175-
.. code-block:: yaml
176-
177-
# config/validator/validation.yaml
178-
App\Entity\Package:
179-
properties:
180-
version:
181-
- SemVer:
182-
allowPreRelease: false
183-
184-
.. code-block:: xml
185-
186-
<!-- config/validator/validation.xml -->
187-
<?xml version="1.0" encoding="UTF-8" ?>
188-
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
189-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
190-
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
191-
192-
<class name="App\Entity\Package">
193-
<property name="version">
194-
<constraint name="SemVer">
195-
<option name="allowPreRelease">false</option>
196-
</constraint>
197-
</property>
198-
</class>
199-
</constraint-mapping>
200-
201-
.. code-block:: php
88+
When set to ``true``, the version must strictly follow the official
89+
`Semantic Versioning`_ specification. This means:
20290

203-
// src/Entity/Package.php
204-
namespace App\Entity;
91+
- No "v" prefix is allowed (use "1.2.3", not "v1.2.3")
92+
- A full version is required (major.minor.patch)
20593

206-
use Symfony\Component\Validator\Constraints as Assert;
207-
use Symfony\Component\Validator\Mapping\ClassMetadata;
94+
When set to ``false``, common version variations are allowed:
20895

209-
class Package
210-
{
211-
// ...
212-
213-
public static function loadValidatorMetadata(ClassMetadata $metadata): void
214-
{
215-
$metadata->addPropertyConstraint('version', new Assert\SemVer([
216-
'allowPreRelease' => false,
217-
]));
218-
}
219-
}
220-
221-
``allowBuildMetadata``
222-
~~~~~~~~~~~~~~~~~~~~~~
223-
224-
**type**: ``boolean`` **default**: ``true``
225-
226-
Whether to allow build metadata in the version string (e.g., "1.2.3+20130313144700").
227-
When set to ``false``, build metadata is not allowed.
96+
- The "v" prefix is accepted (e.g., "v1.2.3")
97+
- Partial versions are valid (e.g., "1", "1.2")
22898

22999
.. configuration-block::
230100

@@ -237,7 +107,7 @@ When set to ``false``, build metadata is not allowed.
237107
238108
class Package
239109
{
240-
#[Assert\SemVer(allowBuildMetadata: false)]
110+
#[Assert\SemVer(strict: false)]
241111
protected string $version;
242112
}
243113
@@ -248,7 +118,7 @@ When set to ``false``, build metadata is not allowed.
248118
properties:
249119
version:
250120
- SemVer:
251-
allowBuildMetadata: false
121+
strict: false
252122
253123
.. code-block:: xml
254124
@@ -261,7 +131,7 @@ When set to ``false``, build metadata is not allowed.
261131
<class name="App\Entity\Package">
262132
<property name="version">
263133
<constraint name="SemVer">
264-
<option name="allowBuildMetadata">false</option>
134+
<option name="strict">false</option>
265135
</constraint>
266136
</property>
267137
</class>
@@ -282,7 +152,7 @@ When set to ``false``, build metadata is not allowed.
282152
public static function loadValidatorMetadata(ClassMetadata $metadata): void
283153
{
284154
$metadata->addPropertyConstraint('version', new Assert\SemVer([
285-
'allowBuildMetadata' => false,
155+
'strict' => false,
286156
]));
287157
}
288158
}
@@ -294,15 +164,19 @@ When set to ``false``, build metadata is not allowed.
294164
Valid Version Examples
295165
----------------------
296166

297-
The following are examples of valid semantic versions:
167+
When using ``strict: true`` (default), the following are valid:
298168

299-
- ``1`` (partial version)
300-
- ``1.2`` (partial version)
301169
- ``1.2.3`` (full version)
302-
- ``v1.2.3`` (with prefix)
303170
- ``1.2.3-alpha`` (pre-release)
304171
- ``1.2.3-beta.1`` (pre-release with numeric identifier)
305172
- ``1.2.3+20130313144700`` (with build metadata)
306173
- ``1.2.3-beta+exp.sha.5114f85`` (pre-release and build metadata)
307174

175+
When using ``strict: false``, additional formats are accepted:
176+
177+
- ``1`` (partial version)
178+
- ``1.2`` (partial version)
179+
- ``v1.2.3`` (with "v" prefix)
180+
- ``v1.2.3-alpha`` (prefix with pre-release)
181+
308182
.. _`Semantic Versioning`: https://semver.org/

0 commit comments

Comments
 (0)