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
description: "Tempest's validation is based on built-in PHP types, but provides many attribute-based rules to cover a wide variety of situations."
3
4
---
4
5
5
-
Validation with Tempest is done by taking an array of raw input data, and validating whether that array of data is valid against a class. While validation and [data mapping](./01-mapper) often work together, the two are separate components and can also be used separately.
6
+
## Overview
6
7
7
-
Here's an object that can be validated:
8
+
Tempest provides a {`\Tempest\Validation\Validator`} object capable of validating an array of values against the public properties of a class or an array of validation rules.
8
9
9
-
```php
10
-
final class Book
11
-
{
12
-
public string $title;
13
-
14
-
public string $description;
15
-
16
-
public ?DateTimeImmutable $publishedAt = null;
17
-
}
18
-
```
10
+
While validation and [data mapping](./01-mapper) often work together, the two are separate components and can also be used separately.
19
11
20
-
As you can see, there are no explicit validation rules, that's because Tempest will first look at the object's type definitions and infer validation rules based on those: `$title` and `$description` are required since these aren't nullable properties, they should both be text; `$publishedAt` is optional, and it expects a valid date time string.
12
+
## Validating against objects
21
13
22
-
Validating an array of data against this `Book` object would look like this:
14
+
When you have raw data and an associated model or data transfer object, you may use the `validateValuesForClass` method on the {b`\Tempest\Validation\Validator`}.
This method accepts a fully-qualified class name as the first argument, and an array of data as the second. The values of the data array will be validated against the public properties of the class.
28
+
29
+
In this case, validation works by inferring validation rules from the built-in PHP types. In the example above, the `Book` class has the following public properties:
30
+
31
+
```php
32
+
use Tempest\DateTime\DateTime;
33
+
34
+
final class Book
35
+
{
36
+
public string $title;
37
+
public string $description;
38
+
public ?DateTime $publishedAt = null;
39
+
}
40
+
```
41
+
36
42
If validation fails, `$failingRules` will contain a list of fields and their respective failed rules.
37
43
38
-
##Validation Rules
44
+
### Adding more rules
39
45
40
-
As mentioned before, the validator makes use of rules to validate an array of data against a class. While the validator will infer some rules based on property types, there are a whole lot more that can be added via attributes:
46
+
Most of the time, the built-in PHP types will not be enough to fully validate your data. You may then add validation attributes to the model or data transfer object.
41
47
42
48
```php
43
-
use Tempest\Validation\Rules\Length;
44
-
use Tempest\Validation\Rules\NotEmpty;
45
-
use Tempest\Validation\Rules\DateTimeFormat;
49
+
use Tempest\Validation\Rules;
46
50
47
51
final class Book
48
52
{
49
-
#[Length(min: 5, max: 50)]
53
+
#[Rules\Length(min: 5, max: 50)]
50
54
public string $title;
51
55
52
-
#[NotEmpty]
56
+
#[Rules\NotEmpty]
53
57
public string $description;
54
58
55
-
#[DateTimeFormat('Y-m-d')]
56
-
public ?DateTimeImmutable $publishedAt = null;
59
+
#[Rules\DateTimeFormat('Y-m-d')]
60
+
public ?DateTime $publishedAt = null;
57
61
}
58
62
```
59
63
60
-
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/src/Tempest/Validation/src/Rules).
64
+
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/packages/validation/src/Rules).
61
65
62
-
## Skipping Validation
66
+
###Skipping validation
63
67
64
-
Some properties should never be validated. You can use the `#[SkipValidation]` attribute to exclude them from the validator:
68
+
You may have situations where you don't want specific properties on a model to be validated. In this case, you may use the {b`#[Tempest\Validation\SkipValidation]`} attribute to prevent them from being validated.
65
69
66
70
```php
67
71
use Tempest\Validation\SkipValidation;
@@ -72,3 +76,21 @@ final class Book
72
76
public string $title;
73
77
}
74
78
```
79
+
80
+
## Validating against specific rules
81
+
82
+
If you don't have a model or data transfer object to validate data against, you may alternatively use the `validateValues` and provide an array of rules.
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/packages/validation/src/Rules).
0 commit comments