Skip to content

Commit 62aeef9

Browse files
committed
docs: document new validation usage
1 parent 3ae1b02 commit 62aeef9

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

docs/2-features/03-validation.md

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ While validation and [data mapping](./01-mapper) often work together, the two ar
1111

1212
## Validating against objects
1313

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`}.
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`}. Note that the validator needs to be [resolved from the container](../1-essentials/05-container.md#injecting-dependencies).
1515

1616
```php
17-
use Tempest\Validation\Validator;
18-
19-
$validator = new Validator();
20-
$failingRules = $validator->validateValuesForClass(Book::class, [
17+
$failingRules = $this->validator->validateValuesForClass(Book::class, [
2118
'title' => 'Timeline Taxi',
2219
'description' => 'My sci-fi novel',
2320
'publishedAt' => '2024-10-01',
@@ -50,18 +47,20 @@ use Tempest\Validation\Rules;
5047

5148
final class Book
5249
{
53-
#[Rules\Length(min: 5, max: 50)]
50+
#[Rules\HasLength(min: 5, max: 50)]
5451
public string $title;
5552

56-
#[Rules\NotEmpty]
53+
#[Rules\IsNotEmptyString]
5754
public string $description;
5855

59-
#[Rules\DateTimeFormat('Y-m-d')]
56+
#[Rules\HasDateTimeFormat('Y-m-d')]
6057
public ?DateTime $publishedAt = null;
6158
}
6259
```
6360

61+
:::info
6462
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/packages/validation/src/Rules).
63+
:::
6564

6665
### Skipping validation
6766

@@ -79,54 +78,70 @@ final class Book
7978

8079
## Validating against specific rules
8180

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.
81+
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.
8382

8483
```php
85-
$validator->validateValues([
84+
$this->validator->validateValues([
8685
'name' => 'Jon Doe',
8786
'email' => '[email protected]',
8887
'age' => 25,
8988
], [
90-
'name' => [new IsString(), new NotNull()],
91-
'email' => [new Email()],
92-
'age' => [new IsInteger(), new NotNull()],
89+
'name' => [new IsString(), new IsNotNull()],
90+
'email' => [new IsEmail()],
91+
'age' => [new IsInteger(), new IsNotNull()],
9392
]);
9493
```
9594

9695
If validation fails, `validateValues()` returns a list of fields and their respective failing rules.
9796

97+
:::info
9898
A list of all available validation rules can be found on [GitHub](https://github.com/tempestphp/tempest-framework/tree/main/packages/validation/src/Rules).
99+
:::
99100

100101
## Validating a single value
101102

102-
You may validate a single value against a set of rules using the `validateValue` method.
103+
You may validate a single value against a set of rules using the `validateValue()` method.
103104

104105
```php
105-
$validator->validateValue('[email protected]', [new Email()]);
106+
$this->validator->validateValue('[email protected]', [new IsEmail()]);
106107
```
107108

108109
Alternatively, you may provide a closure for validation. The closure should return `true` if validation passes, or `false` otherwise. You may also return a string to specify the validation failure message.
109110

110111
```php
111-
$validator->validateValue('[email protected]', function (mixed $value) {
112+
$this->validator->validateValue('[email protected]', function (mixed $value) {
112113
return str_contains($value, '@');
113114
});
114115
```
115116

116117
## Accessing error messages
117118

118-
When validation fails, a list of fields and their respective failing rules is returned. You may call the `message` method on any rule to get a validation message.
119+
When validation fails, a list of fields and their respective failing rules is returned. You may call the `getErrorMessage` method on the validator to get a [localized](./11-localization.md) validation message.
119120

120121
```php
121122
use Tempest\Support\Arr;
122123

123124
// Validate some value
124-
$failures = $validator->validateValue('[email protected]', new Email());
125+
$failures = $this->validator->validateValue('[email protected]', new Email());
125126

126127
// Map failures to their message
127-
$errors = Arr\map($failures, fn (Rule $failure) => $failure->message());
128+
$errors = Arr\map($failures, fn (Rule $failure) => $this->validator->getErrorMessage($failure));
128129
```
129130

130-
:::info
131-
Note that we expect to improve the way validation messages work in the future. See [this conversation](https://discord.com/channels/1236153076688359495/1294321824498323547/1294321824498323547) on our [Discord server](https://tempestphp.com/discord).
132-
:::
131+
You may also specify the field name of the validation failure to get a localized message for that field.
132+
133+
```php
134+
$this->validator->getErrorMessage($failure, 'email');
135+
// => 'Email must be a valid email address'
136+
```
137+
138+
## Overriding translation messages
139+
140+
You may override the default validation messages by adding a [translation file](../2-features/11-localization.md#defining-translation-messages) anywhere in your codebase. Note that Tempest uses the [MessageFormat 2.0](https://messageformat.unicode.org/) format for localization.
141+
142+
```php app/Localization/validation.en.yml
143+
validation_error:
144+
is_email: |
145+
.input {$field :string}
146+
{$field} must be a valid email address.
147+
```

0 commit comments

Comments
 (0)