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

Commit 08358d5

Browse files
authored
Merge pull request #41 from programmatordev/1.x
1.x
2 parents 9202aee + 520b431 commit 08358d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+183
-174
lines changed

docs/01-get-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ $validator = new Validator(
4141

4242
// Validate with these:
4343
$validator->validate(16); // returns bool: false
44-
$validator->assert(16, 'Age'); // throws exception: The "Age" value should be greater than or equal to "18", "16" given.
44+
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
4545
```

docs/02-usage.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
3030
*/
3131
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
3232
{
33-
Validator::range(-90, 90)->assert($latitude, 'Latitude');
34-
Validator::range(-180, 180)->assert($longitude, 'Longitude');
35-
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
33+
Validator::range(-90, 90)->assert($latitude, 'latitude');
34+
Validator::range(-180, 180)->assert($longitude, 'longitude');
35+
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'unit system');
3636

3737
// ...
3838
}
@@ -50,9 +50,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
5050
*/
5151
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
5252
{
53-
(new Validator(new Rule\Range(-90, 90)))->assert($latitude, 'Latitude');
54-
(new Validator(new Rule\Range(-180, 180)))->assert($longitude, 'Longitude');
55-
(new Validator(new Rule\NotBlank(), new Rule\Choice(['METRIC', 'IMPERIAL'])))->assert($unitSystem, 'Unit System');
53+
(new Validator(new Rule\Range(-90, 90)))->assert($latitude, 'latitude');
54+
(new Validator(new Rule\Range(-180, 180)))->assert($longitude, 'longitude');
55+
(new Validator(new Rule\NotBlank(), new Rule\Choice(['METRIC', 'IMPERIAL'])))->assert($unitSystem, 'unit system');
5656

5757
// ...
5858
}
@@ -68,7 +68,7 @@ This method throws a `ValidationException` when a rule fails, otherwise nothing
6868
/**
6969
* @throws ValidationException
7070
*/
71-
assert(mixed $value, string $name): void;
71+
assert(mixed $value, ?string $name = null): void;
7272
```
7373

7474
An example on how to handle an error:
@@ -79,9 +79,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
7979

8080
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
8181
{
82-
Validator::range(-90, 90)->assert($latitude, 'Latitude');
83-
Validator::range(-180, 180)->assert($longitude, 'Longitude');
84-
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
82+
Validator::range(-90, 90)->assert($latitude, 'latitude');
83+
Validator::range(-180, 180)->assert($longitude, 'longitude');
84+
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'unit system');
8585

8686
// ...
8787
}
@@ -90,7 +90,7 @@ try {
9090
getWeatherTemperature(latitude: 100, longitude: 50, unitSystem: 'METRIC');
9191
}
9292
catch (ValidationException $exception) {
93-
echo $exception->getMessage(); // The "Latitude" value should be between "-90" and "90", "100" given.
93+
echo $exception->getMessage(); // The latitude value should be between -90 and 90, 100 given.
9494
}
9595
```
9696
> **Note**
@@ -175,7 +175,7 @@ function calculateDiscount(float $price, float $discount, string $type): float
175175
$discountValidator->addRule(new Rule\LessThanOrEqual(100));
176176
}
177177

178-
$discountValidator->assert($discount, 'Discount');
178+
$discountValidator->assert($discount, 'discount');
179179

180180
// ...
181181
}
@@ -197,9 +197,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception;
197197
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
198198

199199
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');
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');
203203
}
204204
catch (Exception\RangeException $exception) {
205205
// Do something when Range fails
@@ -219,9 +219,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
219219
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
220220

221221
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');
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');
225225
}
226226
catch (ValidationException $exception) {
227227
// Do something when a rule fails
@@ -261,8 +261,8 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
261261

262262
Validator::choice(
263263
constraints: ['red', 'green', 'blue'],
264-
message: '"{{ value }}" is not a valid {{ name }}! You must select one of {{ constraints }}.'
264+
message: '{{ value }} is not a valid {{ name }}! You must select one of {{ constraints }}.'
265265
)->assert('yellow', 'color');
266266

267-
// Throws: "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"].
268268
```

docs/03x-rules-choice.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ Choice(
88
bool $multiple = false,
99
?int $minConstraint = null,
1010
?int $maxConstraint = null,
11-
string $message = 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
12-
string $multipleMessage = 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
13-
string $minMessage = 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
14-
string $maxMessage = 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
11+
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
12+
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
13+
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
14+
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
1515
);
1616
```
1717

@@ -78,7 +78,7 @@ For example, if `maxConstraint` is 2, the input array must have at most 2 values
7878

7979
### `message`
8080

81-
type `string` default: `The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".`
81+
type `string` default: `The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.`
8282

8383
Message that will be shown if input value is not a valid choice.
8484

@@ -92,7 +92,7 @@ The following parameters are available:
9292

9393
### `multipleMessage`
9494

95-
type `string` default: `The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".`
95+
type `string` default: `The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.`
9696

9797
Message that will be shown when `multiple` is `true` and at least one of the input array values is not a valid choice.
9898

@@ -106,7 +106,7 @@ The following parameters are available:
106106

107107
### `minMessage`
108108

109-
type `string` default: `The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
109+
type `string` default: `The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.`
110110

111111
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `minConstraint`.
112112

@@ -123,7 +123,7 @@ The following parameters are available:
123123

124124
### `maxMessage`
125125

126-
type `string` default: `The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
126+
type `string` default: `The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.`
127127

128128
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `maxConstraint`.
129129

docs/03x-rules-country.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Validates that a value is a valid country code.
55
```php
66
Country(
77
string $code = 'alpha-2',
8-
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
8+
string $message = 'The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.'
99
);
1010
```
1111

@@ -42,7 +42,7 @@ Available options:
4242

4343
### `message`
4444

45-
type: `string` default: `The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.`
45+
type: `string` default: `The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.`
4646

4747
Message that will be shown if the input value is not a valid country code.
4848

docs/03x-rules-each-key.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ type: `string` default: `Invalid key: {{ message }}`
3434
Message that will be shown if at least one input value key is invalid according to the given `validator`.
3535

3636
```php
37-
Validator::eachKey(Validator::notBlank())->assert(['red' => '#f00', 1 => '#0f0'], 'Test');
38-
// Throws: Invalid key: The "Test" key should be of type "string", "1" given.
37+
Validator::eachKey(Validator::notBlank())->assert(['red' => '#f00', 1 => '#0f0'], 'color');
38+
// Throws: Invalid key: The color key value should be of type "string", 1 given.
3939
```
4040

4141
The following parameters are available:

docs/03x-rules-each-value.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Validates every element of an `array` or object implementing `\Traversable` with
55
```php
66
EachValue(
77
Validator $validator,
8-
string $message = 'At key "{{ key }}": {{ message }}'
8+
string $message = 'At key {{ key }}: {{ message }}'
99
);
1010
```
1111

@@ -34,8 +34,8 @@ type: `string` default: `At key "{{ key }}": {{ message }}`
3434
Message that will be shown if at least one input value element is invalid according to the given `validator`.
3535

3636
```php
37-
Validator::eachValue(Validator::notBlank())->assert(['red', 'green', ''], 'Test');
38-
// Throws: At key "2": The "Test" value should not be blank, "" given.
37+
Validator::eachValue(Validator::notBlank())->assert(['red', 'green', ''], 'color');
38+
// Throws: At key 2: The color value should not be blank, "" given.
3939
```
4040

4141
The following parameters are available:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
66
```php
77
GreaterThanOrEqual(
88
mixed $constraint,
9-
string $message = 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.'
9+
string $message = 'The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.'
1010
);
1111
```
1212

@@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
4444

4545
### `message`
4646

47-
type: `string` default: `The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.`
47+
type: `string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.`
4848

4949
Message that will be shown if the value is not greater than or equal to the constraint value.
5050

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
66
```php
77
GreaterThan(
88
mixed $constraint,
9-
string $message = 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.'
9+
string $message = 'The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.'
1010
);
1111
```
1212

@@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
4444

4545
### `message`
4646

47-
type: `string` default: `The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.`
47+
type: `string` default: `The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.`
4848

4949
Message that will be shown if the value is not greater than the constraint value.
5050

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
66
```php
77
LessThanOrEqual(
88
mixed $constraint,
9-
string $message = 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.'
9+
string $message = 'The {{ name }} value should be less than or equal to {{ constraint }}, {{ value }} given.'
1010
);
1111
```
1212

@@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
4444

4545
### `message`
4646

47-
type: `string` default: `The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.`
47+
type: `string` default: `The {{ name }} value should be less than or equal to {{ constraint }}, {{ value }} given.`
4848

4949
Message that will be shown if the value is not less than or equal to the constraint value.
5050

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
66
```php
77
LessThan(
88
mixed $constraint,
9-
string $message = 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.'
9+
string $message = 'The {{ name }} value should be less than {{ constraint }}, {{ value }} given.'
1010
);
1111
```
1212

@@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
4444

4545
### `message`
4646

47-
type: `string` default: `The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.`
47+
type: `string` default: `The {{ name }} value should be less than {{ constraint }}, {{ value }} given.`
4848

4949
Message that will be shown if the value is not less than the constraint value.
5050

0 commit comments

Comments
 (0)