|
| 1 | +--- |
| 2 | +title: Validation |
| 3 | +category: framework |
| 4 | +--- |
| 5 | + |
| 6 | +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](/main/framework/validation) often work together, the two are separate components and can also be used separately. |
| 7 | + |
| 8 | +Here's an object that can be validated: |
| 9 | + |
| 10 | +```php |
| 11 | +final class Book |
| 12 | +{ |
| 13 | + public string $title; |
| 14 | + |
| 15 | + public string $description; |
| 16 | + |
| 17 | + public ?DateTimeImmutable $publishedAt = null; |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +Validating an array of data against this `Book` object would look like this: |
| 24 | + |
| 25 | +```php |
| 26 | +use Tempest\Validation\Validator; |
| 27 | + |
| 28 | +$validator = new Validator(); |
| 29 | + |
| 30 | +$failingRules = $validator->validateValuesForClass(Book::class, [ |
| 31 | + 'title' => 'Timeline Taxi', |
| 32 | + 'description' => 'My sci-fi novel', |
| 33 | + 'publishedAt' => '2024-10-01', |
| 34 | +]); |
| 35 | +``` |
| 36 | + |
| 37 | +If validation fails, `$failingRules` will contain a list of fields and their respective failed rules. |
| 38 | + |
| 39 | +## Validation Rules |
| 40 | + |
| 41 | +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: |
| 42 | + |
| 43 | +```php |
| 44 | +use Tempest\Validation\Rules\Length; |
| 45 | +use Tempest\Validation\Rules\NotEmpty; |
| 46 | +use Tempest\Validation\Rules\DateTimeFormat; |
| 47 | + |
| 48 | +final class Book |
| 49 | +{ |
| 50 | + #[Length(min: 5, max: 50)] |
| 51 | + public string $title; |
| 52 | + |
| 53 | + #[NotEmpty] |
| 54 | + public string $description; |
| 55 | + |
| 56 | + #[DateTimeFormat('Y-m-d')] |
| 57 | + public ?DateTimeImmutable $publishedAt = null; |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +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). |
| 62 | + |
| 63 | +## Skipping Validation |
| 64 | + |
| 65 | +Some properties should never be validated. You can use the `#[SkipValidation]` attribute to exclude them from the validator: |
| 66 | + |
| 67 | +```php |
| 68 | +use Tempest\Validation\SkipValidation; |
| 69 | + |
| 70 | +final class Book |
| 71 | +{ |
| 72 | + #[SkipValidation] |
| 73 | + public string $title; |
| 74 | +} |
| 75 | +``` |
0 commit comments