Skip to content

Commit 395466c

Browse files
committed
feat: improve validation chapter
1 parent 8efa445 commit 395466c

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed
Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,71 @@
11
---
22
title: Validation
3+
description: "Tempest's validation is based on built-in PHP types, but provides many attribute-based rules to cover a wide variety of situations."
34
---
45

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
67

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.
89

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.
1911

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
2113

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`}.
2315

2416
```php
2517
use Tempest\Validation\Validator;
2618

2719
$validator = new Validator();
28-
2920
$failingRules = $validator->validateValuesForClass(Book::class, [
3021
'title' => 'Timeline Taxi',
3122
'description' => 'My sci-fi novel',
3223
'publishedAt' => '2024-10-01',
3324
]);
3425
```
3526

27+
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+
3642
If validation fails, `$failingRules` will contain a list of fields and their respective failed rules.
3743

38-
## Validation Rules
44+
### Adding more rules
3945

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.
4147

4248
```php
43-
use Tempest\Validation\Rules\Length;
44-
use Tempest\Validation\Rules\NotEmpty;
45-
use Tempest\Validation\Rules\DateTimeFormat;
49+
use Tempest\Validation\Rules;
4650

4751
final class Book
4852
{
49-
#[Length(min: 5, max: 50)]
53+
#[Rules\Length(min: 5, max: 50)]
5054
public string $title;
5155

52-
#[NotEmpty]
56+
#[Rules\NotEmpty]
5357
public string $description;
5458

55-
#[DateTimeFormat('Y-m-d')]
56-
public ?DateTimeImmutable $publishedAt = null;
59+
#[Rules\DateTimeFormat('Y-m-d')]
60+
public ?DateTime $publishedAt = null;
5761
}
5862
```
5963

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).
6165

62-
## Skipping Validation
66+
### Skipping validation
6367

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.
6569

6670
```php
6771
use Tempest\Validation\SkipValidation;
@@ -72,3 +76,21 @@ final class Book
7276
public string $title;
7377
}
7478
```
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.
83+
84+
```php
85+
$validator->validateValues([
86+
'name' => 'Jon Doe',
87+
'email' => '[email protected]',
88+
'age' => 25,
89+
], [
90+
'name' => [new IsString(), new NotNull()],
91+
'email' => [new Email()],
92+
'age' => [new IsInteger(), new NotNull()],
93+
]);
94+
```
95+
96+
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

Comments
 (0)