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

Commit ec2ce8b

Browse files
authored
Merge pull request #22 from programmatordev/YAPV-12-create-documentation
Create documentation
2 parents 9d8e1d4 + d545044 commit ec2ce8b

14 files changed

+273
-88
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Yet Another PHP Validator
22

3-
Versatile library focused on validating development code with expressive error messages.
3+
PHP validator focused on validating development code with expressive error messages.
4+
5+
> **Note**
6+
> This library is not in version 1.x mainly because there are few available rules.
7+
> Hopefully, that will change in the near future.
48
59
## Requirements
610

@@ -44,15 +48,23 @@ $validator->assert(16, 'Age'); // throws exception: The "Age" value should be gr
4448

4549
## Documentation
4650

51+
- [Get Started](docs/01-get-started.md)
52+
- [Usage](docs/02-usage.md)
53+
- [Usage](docs/02-usage.md#usage)
54+
- [Methods](docs/02-usage.md#methods)
55+
- [Error Handling](docs/02-usage.md#error-handling)
56+
- [Custom Error Messages](docs/02-usage.md#custom-error-messages)
57+
- [Rules](docs/03-rules.md)
58+
- [Custom Rules](docs/04-custom-rules.md)
4759

4860
## Contributing
4961

50-
Any form of contribution to improve this library will be welcome and appreciated.
62+
Any form of contribution to improve this library (including requests) will be welcome and appreciated.
5163
Make sure to open a pull request or issue.
5264

5365
## Acknowledgments
5466

55-
This library is inspired by [Respect's Validation](https://github.com/Respect/Validation) and [Symfony's Validator](https://symfony.com/doc/current/validation.html).
67+
This library is inspired by [respect/validation](https://github.com/Respect/Validation) and [symfony/validator](https://github.com/symfony/validator).
5668

5769
## License
5870

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "programmatordev/yet-another-php-validator",
3-
"description": "PHP Validator",
3+
"description": "PHP validator focused on validating development code with expressive error messages",
44
"type": "library",
5-
"keywords": ["PHP", "Validator", "Validation"],
5+
"keywords": ["PHP", "PHP8", "Validator", "Validation"],
66
"license": "MIT",
77
"authors": [
88
{

docs/01-get-started.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,13 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
3333
// Do this...
3434
$validator = Validator::notBlank()->greaterThanOrEqual(18);
3535

36-
// ...or this...
36+
// Or this...
3737
$validator = new Validator(
3838
new Rule\NotBlank(),
3939
new Rule\GreaterThanOrEqual(18)
4040
);
4141

42-
// ...or even this...
43-
$validator = (new Validator())
44-
->addRule(new Rule\NotBlank())
45-
->addRule(new Rule\GreaterThanOrEqual(18));
46-
47-
// ...and validate with these:
42+
// Validate with these:
4843
$validator->validate(16); // returns bool: false
4944
$validator->assert(16, 'Age'); // throws exception: The "Age" value should be greater than or equal to "18", "16" given.
5045
```

docs/02-usage.md

Lines changed: 81 additions & 20 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,75 @@ 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, an exception is thrown when a rule fails.
191+
192+
Each rule has a unique exception, formed by the name of the rule followed by the word Exception, like `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+
echo $exception->getMessage();
229+
}
230+
```
231+
232+
When using both the [`assert`](#assert) or [`validate`](#validate) methods,
233+
an `UnexpectedValueException` is thrown when the provided input data is not valid to perform the validation.
234+
235+
For example, when trying to compare a date with a string:
236+
237+
```php
238+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
239+
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
240+
241+
try {
242+
Validator::greaterThanOrEqual(new DateTime('today'))->validate('alpha');
243+
}
244+
catch (UnexpectedValueException $exception) {
245+
echo $exception->getMessage(); // Cannot compare a type "string" with a type "DateTime".
246+
}
247+
```
187248

188-
## Custom Exception Messages
249+
## Custom Error Messages
189250

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

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

@@ -203,5 +264,5 @@ Validator::choice(
203264
message: '"{{ value }}" is not a valid {{ name }}! You must select one of {{ constraints }}.'
204265
)->assert('yellow', 'color');
205266

206-
// "yellow" is not a valid color! You must select one of [red, green, blue].
267+
// Throws: "yellow" is not a valid color! You must select one of [red, green, blue].
207268
```

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)

docs/03x-rules-all.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ The following parameters are available:
5454
| Parameter | Description |
5555
|-----------------|-----------------------------------------------|
5656
| `{{ value }}` | The current invalid value |
57-
| `{{ name }}` | Name of the value being validated |
57+
| `{{ name }}` | Name of the invalid value |
5858
| `{{ key }}` | The array key of the invalid array element |
5959
| `{{ message }}` | The rule message of the invalid array element |

docs/03x-rules-choice.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Validator::choice(['red', 'green', 'blue'], multiple: true, minConstraint: 2, ma
5151

5252
type: `array` `required`
5353

54-
Collection of choices to be validated against the input value.
54+
Collection of constraint choices to be validated against the input value.
5555

5656
### `multiple`
5757

@@ -84,11 +84,11 @@ Message that will be shown if input value is not a valid choice.
8484

8585
The following parameters are available:
8686

87-
| Parameter | Description |
88-
|---------------------|-----------------------------------|
89-
| `{{ value }}` | The current invalid value |
90-
| `{{ name }}` | Name of the value being validated |
91-
| `{{ constraints }}` | The array of valid choices |
87+
| Parameter | Description |
88+
|---------------------|----------------------------|
89+
| `{{ value }}` | The current invalid value |
90+
| `{{ name }}` | Name of the invalid value |
91+
| `{{ constraints }}` | The array of valid choices |
9292

9393
### `multipleMessage`
9494

@@ -98,11 +98,11 @@ Message that will be shown when `multiple` is `true` and at least one of the inp
9898

9999
The following parameters are available:
100100

101-
| Parameter | Description |
102-
|---------------------|-----------------------------------|
103-
| `{{ value }}` | The current invalid value |
104-
| `{{ name }}` | Name of the value being validated |
105-
| `{{ constraints }}` | The array of valid choices |
101+
| Parameter | Description |
102+
|---------------------|----------------------------|
103+
| `{{ value }}` | The current invalid value |
104+
| `{{ name }}` | Name of the invalid value |
105+
| `{{ constraints }}` | The array of valid choices |
106106

107107
### `minMessage`
108108

@@ -116,7 +116,7 @@ The following parameters are available:
116116
|-----------------------|--------------------------------------|
117117
| `{{ value }}` | The current invalid value |
118118
| `{{ numValues }}` | The current invalid number of values |
119-
| `{{ name }}` | Name of the value being validated |
119+
| `{{ name }}` | Name of the invalid value |
120120
| `{{ constraints }}` | The array of valid choices |
121121
| `{{ minConstraint }}` | The minimum number of valid choices |
122122
| `{{ maxConstraint }}` | The maximum number of valid choices |
@@ -133,7 +133,7 @@ The following parameters are available:
133133
|-----------------------|--------------------------------------|
134134
| `{{ value }}` | The current invalid value |
135135
| `{{ numValues }}` | The current invalid number of values |
136-
| `{{ name }}` | Name of the value being validated |
136+
| `{{ name }}` | Name of the invalid value |
137137
| `{{ constraints }}` | The array of valid choices |
138138
| `{{ minConstraint }}` | The minimum number of valid choices |
139139
| `{{ maxConstraint }}` | The maximum number of valid choices |

docs/03x-rules-greater-than-or-equal.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Validator::greaterThanOrEqual(new DateTime('today'))->validate(new DateTime('tod
2727
```
2828

2929
> **Note**
30-
> String comparison is case-sensitive, meaning that comparing `'hello'` with `'Hello'` is different.
30+
> String comparison is case-sensitive, meaning that comparing `"hello"` with `"Hello"` is different.
3131
> Check [`strcmp`](https://www.php.net/manual/en/function.strcmp.php) for more information.
3232
3333
> **Note**
@@ -50,8 +50,8 @@ Message that will be shown if the value is not greater than or equal to the cons
5050

5151
The following parameters are available:
5252

53-
| Parameter | Description |
54-
|--------------------|-----------------------------------|
55-
| `{{ value }}` | The current invalid value |
56-
| `{{ name }}` | Name of the value being validated |
57-
| `{{ constraint }}` | The comparison value |
53+
| Parameter | Description |
54+
|--------------------|---------------------------|
55+
| `{{ value }}` | The current invalid value |
56+
| `{{ name }}` | Name of the invalid value |
57+
| `{{ constraint }}` | The comparison value |

docs/03x-rules-greater-than.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Validator::greaterThan(new DateTime('today'))->validate(new DateTime('today'));
2727
```
2828

2929
> **Note**
30-
> String comparison is case-sensitive, meaning that comparing `'hello'` with `'Hello'` is different.
30+
> String comparison is case-sensitive, meaning that comparing `"hello"` with `"Hello"` is different.
3131
> Check [`strcmp`](https://www.php.net/manual/en/function.strcmp.php) for more information.
3232
3333
> **Note**
@@ -50,8 +50,8 @@ Message that will be shown if the value is not greater than the constraint value
5050

5151
The following parameters are available:
5252

53-
| Parameter | Description |
54-
|--------------------|-----------------------------------|
55-
| `{{ value }}` | The current invalid value |
56-
| `{{ name }}` | Name of the value being validated |
57-
| `{{ constraint }}` | The comparison value |
53+
| Parameter | Description |
54+
|--------------------|---------------------------|
55+
| `{{ value }}` | The current invalid value |
56+
| `{{ name }}` | Name of the invalid value |
57+
| `{{ constraint }}` | The comparison value |

docs/03x-rules-less-than-or-equal.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Validator::lessThanOrEqual(new DateTime('today'))->validate(new DateTime('today'
2727
```
2828

2929
> **Note**
30-
> String comparison is case-sensitive, meaning that comparing `'hello'` with `'Hello'` is different.
30+
> String comparison is case-sensitive, meaning that comparing `"hello"` with `"Hello"` is different.
3131
> Check [`strcmp`](https://www.php.net/manual/en/function.strcmp.php) for more information.
3232
3333
> **Note**
@@ -50,8 +50,8 @@ Message that will be shown if the value is not less than or equal to the constra
5050

5151
The following parameters are available:
5252

53-
| Parameter | Description |
54-
|--------------------|-----------------------------------|
55-
| `{{ value }}` | The current invalid value |
56-
| `{{ name }}` | Name of the value being validated |
57-
| `{{ constraint }}` | The comparison value |
53+
| Parameter | Description |
54+
|--------------------|---------------------------|
55+
| `{{ value }}` | The current invalid value |
56+
| `{{ name }}` | Name of the invalid value |
57+
| `{{ constraint }}` | The comparison value |

0 commit comments

Comments
 (0)