@@ -30,9 +30,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
30
30
*/
31
31
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
32
32
{
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 ');
36
36
37
37
// ...
38
38
}
@@ -50,9 +50,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
50
50
*/
51
51
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
52
52
{
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 ');
56
56
57
57
// ...
58
58
}
@@ -68,7 +68,7 @@ This method throws a `ValidationException` when a rule fails, otherwise nothing
68
68
/**
69
69
* @throws ValidationException
70
70
*/
71
- assert(mixed $value, string $name): void;
71
+ assert(mixed $value, ? string $name = null ): void;
72
72
```
73
73
74
74
An example on how to handle an error:
@@ -79,9 +79,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
79
79
80
80
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
81
81
{
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 ');
85
85
86
86
// ...
87
87
}
90
90
getWeatherTemperature(latitude: 100, longitude: 50, unitSystem: 'METRIC');
91
91
}
92
92
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.
94
94
}
95
95
```
96
96
> ** Note**
@@ -175,7 +175,7 @@ function calculateDiscount(float $price, float $discount, string $type): float
175
175
$discountValidator->addRule(new Rule\LessThanOrEqual(100));
176
176
}
177
177
178
- $discountValidator->assert($discount, 'Discount ');
178
+ $discountValidator->assert($discount, 'discount ');
179
179
180
180
// ...
181
181
}
@@ -197,9 +197,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception;
197
197
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
198
198
199
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 ');
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
203
}
204
204
catch (Exception\RangeException $exception) {
205
205
// Do something when Range fails
@@ -219,9 +219,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
219
219
use ProgrammatorDev\YetAnotherPhpValidator\Validator;
220
220
221
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 ');
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
225
}
226
226
catch (ValidationException $exception) {
227
227
// Do something when a rule fails
@@ -261,8 +261,8 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
261
261
262
262
Validator::choice(
263
263
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 }}.'
265
265
)->assert('yellow', 'color');
266
266
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" ].
268
268
```
0 commit comments