@@ -80,151 +80,21 @@ Basic Usage
80
80
Options
81
81
-------
82
82
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
+ ~~~~~~~~~~
154
85
155
86
**type **: ``boolean `` **default **: ``true ``
156
87
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:
202
90
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)
205
93
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:
208
95
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")
228
98
229
99
.. configuration-block ::
230
100
@@ -237,7 +107,7 @@ When set to ``false``, build metadata is not allowed.
237
107
238
108
class Package
239
109
{
240
- #[Assert\SemVer(allowBuildMetadata : false)]
110
+ #[Assert\SemVer(strict : false)]
241
111
protected string $version;
242
112
}
243
113
@@ -248,7 +118,7 @@ When set to ``false``, build metadata is not allowed.
248
118
properties :
249
119
version :
250
120
- SemVer :
251
- allowBuildMetadata : false
121
+ strict : false
252
122
253
123
.. code-block :: xml
254
124
@@ -261,7 +131,7 @@ When set to ``false``, build metadata is not allowed.
261
131
<class name =" App\Entity\Package" >
262
132
<property name =" version" >
263
133
<constraint name =" SemVer" >
264
- <option name =" allowBuildMetadata " >false</option >
134
+ <option name =" strict " >false</option >
265
135
</constraint >
266
136
</property >
267
137
</class >
@@ -282,7 +152,7 @@ When set to ``false``, build metadata is not allowed.
282
152
public static function loadValidatorMetadata(ClassMetadata $metadata): void
283
153
{
284
154
$metadata->addPropertyConstraint('version', new Assert\SemVer([
285
- 'allowBuildMetadata ' => false,
155
+ 'strict ' => false,
286
156
]));
287
157
}
288
158
}
@@ -294,15 +164,19 @@ When set to ``false``, build metadata is not allowed.
294
164
Valid Version Examples
295
165
----------------------
296
166
297
- The following are examples of valid semantic versions :
167
+ When using `` strict: true `` (default), the following are valid :
298
168
299
- - ``1 `` (partial version)
300
- - ``1.2 `` (partial version)
301
169
- ``1.2.3 `` (full version)
302
- - ``v1.2.3 `` (with prefix)
303
170
- ``1.2.3-alpha `` (pre-release)
304
171
- ``1.2.3-beta.1 `` (pre-release with numeric identifier)
305
172
- ``1.2.3+20130313144700 `` (with build metadata)
306
173
- ``1.2.3-beta+exp.sha.5114f85 `` (pre-release and build metadata)
307
174
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
+
308
182
.. _`Semantic Versioning` : https://semver.org/
0 commit comments