You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/05-validator-for-data-models.md
+32-17Lines changed: 32 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,33 +3,31 @@
3
3
# Validator for data models
4
4
5
5
The `dogado/json-api-common` package allows the conversion from resource instances to data models. It's usually the case, that we need some sort of validation on the JSON API data that is coming in.
6
-
The `Dogado\JsonApi\Server\Validator\Validator` class offers the possibility to validate any model in it the current state, based on `symfony/validator`.
6
+
The `Dogado\JsonApi\Server\Validator\ModelValidator` class offers the possibility to validate any model in it the current state, based on `symfony/validator`.
7
7
8
-
Usually, you can use any validation you like. Although this validator will throw a custom JSON API compatible `\Dogado\JsonApi\Exception\JsonApi\ValidationException` containing error objects representing the Symfony Validator violations.
8
+
Usually, you can use any validation you like. Although this validator will throw a custom JSON API compatible `Dogado\JsonApi\Exception\JsonApi\ValidationException` containing error objects representing the Symfony Validator violations.
9
9
10
10
## Code example
11
11
12
12
### Model class definition
13
13
```php
14
-
use Dogado\JsonApi\Annotations\Attribute;
15
-
use Dogado\JsonApi\Annotations\Type;
14
+
use Dogado\JsonApi\Attribute\Attribute;
15
+
use Dogado\JsonApi\Attribute\Type;
16
16
use Symfony\Component\Validator\Constraints as Assert;
17
17
18
-
/**
19
-
* @Type("validation-test")
20
-
*/
18
+
#[Type('validation-test')]
21
19
class Model
22
20
{
23
-
/**
24
-
* @Attribute()
25
-
* @Assert\Email()
26
-
*/
21
+
#[
22
+
Attribute,
23
+
Assert\Email
24
+
]
27
25
private string $email = 'noValidEmail';
28
26
29
-
/**
30
-
* @Attribute("jsonApiValueObject")
31
-
* @Assert\Valid()
32
-
*/
27
+
#[
28
+
Attribute('jsonApiValueObject'),
29
+
Assert\Valid
30
+
]
33
31
private ValueObjectModel $valueObject;
34
32
35
33
public function __construct()
@@ -39,7 +37,7 @@ class Model
39
37
}
40
38
```
41
39
42
-
It is also possible to validate value objects. To do that, you need to add the `@Assert\Valid()` annotation to the object property. Within the value object, you can continue to use the regular assertions.
40
+
It is also possible to validate value objects. To do that, you need to add the `Assert\Valid` attribute to the object property. Within the value object, you can continue to use the regular assertions.
43
41
44
42
### Execute validation
45
43
@@ -51,7 +49,7 @@ use Symfony\Component\Validator\ValidatorBuilder;
In the above example we are using the annotation mapping that Symfony Validator offers by default. Feel free to use a different way of defining your validation rules. More details and validation examples can be found in the [Symfony documentation](https://symfony.com/doc/current/validation.html).
62
60
61
+
## php 8 validation limitations and Doctrine annotation support
62
+
63
+
Symfony added the php 8 attribute support for the assertion constraints in
64
+
[`symfony/validator:5.2.0`](https://symfony.com/blog/new-in-symfony-5-2-constraints-as-php-attributes), but the
65
+
alternative Doctrine annotation support is still given, even in Symfony 6. You can activate it like this:
The benefit of using the php 8 attributes instead is that you don't have to require doctrine packages as dependencies.
73
+
Attributes are a php 8 natively implemented feature and therefore faster to use. However, there
74
+
are a few Symfony Constraints
75
+
[that are not compatible to attributes yet](https://github.com/symfony/symfony/issues/38503), but that will probably
76
+
change with php 8.1.
77
+
63
78
## Custom violation converter
64
79
65
80
The Symfony validator creates violations for each failing assertion rule. The JSON API server validator will send all violations against a `Dogado\JsonApi\Server\Validator\ViolationConverterInterface`. There already is a default `ViolationConverter` which will be used, but feel free to create a custom one and pass that one as parameter to the Validator instantiation.
0 commit comments