Commit ca86457
committed
feature #42403 [Validator] Define which collection keys should be checked for uniqueness (wkania)
This PR was squashed before being merged into the 6.1 branch.
Discussion
----------
[Validator] Define which collection keys should be checked for uniqueness
| Q | A
| ------------- | ---
| Branch? | 5.4
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix #9888
| License | MIT
| Doc PR | symfony/symfony-docs#16713
Currently, the validator checks each element of the collection as a whole. We already have a custom normalizer (which is great), but it would be nice to be able to check for uniqueness certain [collection](https://symfony.com/doc/current/reference/constraints/Collection.html) keys.
For example, some fields in the collection element can be identifiers. They should be unique within the collection, even when the rest of the element data are different.
Current state:
- validates that all the elements of the given collection are unique
New state:
- preserve the current state,
- all old tests pass (no changes in them),
- no breaking changes,
- define which collection fields should be checked for uniqueness (optional),
- fields are optional in each element of the collection. Use [collection constraints](https://symfony.com/doc/current/reference/constraints/Collection.html) if they are required
Examples:
1. Basic example. Each translation of the same resource must be in a different language.
```php
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Count(min=1),
* @Assert\Unique(fields={"language"}),
* @Assert\Collection(
* fields = {
* "language" = {
* @Assert\NotBlank,
* @Assert\Length(min = 2, max = 2),
* @Assert\Language
* },
* "title" = {
* @Assert\NotBlank,
* @Assert\Length(max = 255)
* },
* "description" = {
* @Assert\NotBlank,
* @Assert\Length(max = 255)
* }
* }
* )
*/
public array $translations = [];
```
2. An example where Optional is recognizable. Items with the id are changed and without are new.
```php
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Optional;
/**
* @Assert\Unique(fields={"id"}),
* @Assert\Collection(
* fields = {
* "id" = @Assert\Optional({
* @Assert\Uuid
* }),
* "name" = {
* @Assert\NotBlank,
* @Assert\Length(max = 255)
* }
* }
* )
*/
public array $items = [];
```
3. An example with composite uniqueness
```php
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Unique(fields={"latitude", "longitude"}),
* @Assert\Collection(
* fields = {
* "latitude" = {
* @Assert\NotBlank
* },
* "longitude" = {
* @Assert\NotBlank
* },
* "poi" = {
* @Assert\Length(max = 255)
* }
* }
* )
*/
public array $coordinates = [];
```
Commits
-------
0e8f4cefdb [Validator] Define which collection keys should be checked for uniquenessFile tree
4 files changed
+100
-7
lines changed- Constraints
- Tests/Constraints
4 files changed
+100
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
43 | 50 | | |
44 | 51 | | |
45 | | - | |
| 52 | + | |
| 53 | + | |
46 | 54 | | |
47 | 55 | | |
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
| 59 | + | |
51 | 60 | | |
52 | 61 | | |
53 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
44 | 50 | | |
45 | 51 | | |
46 | 52 | | |
| |||
65 | 71 | | |
66 | 72 | | |
67 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
68 | 89 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
153 | 154 | | |
154 | 155 | | |
155 | 156 | | |
156 | | - | |
| 157 | + | |
157 | 158 | | |
158 | 159 | | |
159 | | - | |
| 160 | + | |
160 | 161 | | |
161 | 162 | | |
162 | | - | |
163 | | - | |
164 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
165 | 166 | | |
166 | 167 | | |
167 | 168 | | |
| |||
220 | 221 | | |
221 | 222 | | |
222 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
223 | 285 | | |
224 | 286 | | |
225 | 287 | | |
| |||
0 commit comments