Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit b49ee76

Browse files
committed
chore: added the error handling section
1 parent 9d8e1d4 commit b49ee76

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

docs/02-usage.md

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# Using Yet Another PHP Validator
22

3-
- Usage
4-
- Fluent
5-
- Dependency Injection
6-
- Methods
7-
- assert
8-
- validate
9-
- getRules
10-
- addRule
11-
- Exception Handling
12-
- Custom Exception Messages
3+
- [Usage](#usage)
4+
- [Fluent](#fluent)
5+
- [Dependency Injection](#dependency-injection)
6+
- [Methods](#methods)
7+
- [assert](#assert)
8+
- [validate](#validate)
9+
- [getRules](#getrules)
10+
- [addRule](#addrule)
11+
- [Error Handling](#error-handling)
12+
- [Custom Error Messages](#custom-error-messages)
1313

1414
## Usage
1515

16-
This library allows you to use validate data in two different ways:
16+
This library allows you to validate data in two different ways:
1717
- In a fluent way, making use of magic methods. The goal is to be able to create a set of rules with minimum setup;
1818
- In a traditional way, making use of dependency injection. You may not like the fluent approach, and prefer to work this way.
1919

@@ -71,7 +71,7 @@ This method throws a `ValidationException` when a rule fails, otherwise nothing
7171
assert(mixed $value, string $name): void;
7272
```
7373

74-
An example on how to handle an exception:
74+
An example on how to handle an error:
7575

7676
```php
7777
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
@@ -93,9 +93,11 @@ catch (ValidationException $exception) {
9393
echo $exception->getMessage(); // The "Latitude" value should be between "-90" and "90", "100" given.
9494
}
9595
```
96+
> **Note**
97+
> Check the [Error Handling](#error-handling) section for more information.
9698
9799
> **Note**
98-
> The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
100+
> The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
99101
> Check the [Usage](#usage) section for more information.
100102
101103
### `validate`
@@ -117,7 +119,7 @@ if (!Validator::range(-90, 90)->validate($latitude)) {
117119
```
118120

119121
> **Note**
120-
> The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
122+
> The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
121123
> Check the [Usage](#usage) section for more information.
122124
123125
### `getRules`
@@ -148,7 +150,7 @@ print_r($validator->getRules());
148150
```
149151

150152
> **Note**
151-
> The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
153+
> The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
152154
> Check the [Usage](#usage) section for more information.
153155
154156
### `addRule`
@@ -180,16 +182,74 @@ function calculateDiscount(float $price, float $discount, string $type): float
180182
```
181183

182184
> **Note**
183-
> The example only shows one usage approach, but both Fluent and Dependency Injection usage approaches should work the same.
185+
> The example only shows one usage approach, but both Fluent and Dependency Injection should work the same.
184186
> Check the [Usage](#usage) section for more information.
185187
186-
## Exception Handling
188+
## Error Handling
189+
190+
When using the [`assert`](#assert) method to validate a value, an exception is thrown when a rule fails.
191+
192+
Each rule has a unique exception, formed by the name of the rule plus Exception — `RuleNameException`.
193+
The following shows an example:
194+
195+
```php
196+
use ProgrammatorDev\YetAnotherPhpValidator\Exception;
197+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
198+
199+
try {
200+
Validator::range(-90, 90)->assert($latitude, 'Latitude');
201+
Validator::range(-180, 180)->assert($longitude, 'Longitude');
202+
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
203+
}
204+
catch (Exception\RangeException $exception) {
205+
// Do something when Range fails
206+
}
207+
catch (Exception\NotBlankException $exception) {
208+
// Do something when NotBlank fails
209+
}
210+
catch (Exception\ChoiceException $exception) {
211+
// Do something when Choice fails
212+
}
213+
```
214+
215+
To catch all errors with a single exception, you can use the `ValidationException`:
216+
217+
```php
218+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
219+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
220+
221+
try {
222+
Validator::range(-90, 90)->assert($latitude, 'Latitude');
223+
Validator::range(-180, 180)->assert($longitude, 'Longitude');
224+
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
225+
}
226+
catch (ValidationException $exception) {
227+
// Do something when a rule fails
228+
}
229+
```
230+
231+
An `UnexpectedValueException` is thrown when the provided input data is not valid to perform the validation.
232+
For example, when trying to compare a date with a string:
233+
234+
```php
235+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
236+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
237+
238+
try {
239+
Validator::greaterThanOrEqual(new DateTime('today'))->validate('alpha');
240+
}
241+
catch (UnexpectedValueException $exception) {
242+
echo $exception->getMessage(); // Cannot compare a type "string" with a type "DateTime".
243+
}
244+
```
245+
246+
This exception is thrown when using [`assert`](#assert) or [`validate`](#validate).
187247

188-
## Custom Exception Messages
248+
## Custom Error Messages
189249

190250
All rules have at least one error message that can be customized (some rules have more than one error message for different case scenarios).
191251

192-
Every message has a list of dynamic parameters to help create an intuitive error text (like the invalid value, constraints, names, and others).
252+
Every message has a list of dynamic parameters to help create an intuitive error (like the invalid value, constraints, names, and others).
193253
To check what parameters and messages are available, look into the Options section in the page of a rule.
194254
Go to [Rules](03-rules.md) to see all available rules.
195255

docs/03-rules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- [Basic Rules](#basic-rules)
44
- [Comparison Rules](#comparison-rules)
55
- [Choice Rules](#choice-rules)
6-
- [Array Rules](#array-rules)
6+
- [Other Rules](#other-rules)
77

88
## Basic Rules
99

@@ -21,6 +21,6 @@
2121

2222
- [Choice](03x-rules-choice.md)
2323

24-
## Array Rules
24+
## Other Rules
2525

2626
- [All](03x-rules-all.md)

0 commit comments

Comments
 (0)