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

Commit 8c50eec

Browse files
authored
Merge pull request #76 from programmatordev/YAPV-69-refactor-error-messages
Refactor error messages
2 parents 19ddb26 + adf2c8b commit 8c50eec

File tree

87 files changed

+283
-325
lines changed

Some content is hidden

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

87 files changed

+283
-325
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $validator = Validator::type('int')->greaterThanOrEqual(18);
3030

3131
// and validate with these:
3232
$validator->validate(16); // returns bool: false
33-
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
33+
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18.
3434
```
3535

3636
## Documentation

docs/01-get-started.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ $validator = Validator::type('int')->greaterThanOrEqual(18);
2828

2929
// and validate with these:
3030
$validator->validate(16); // returns bool: false
31-
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
31+
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18.
3232
```

docs/03-rules_blank.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Validator::blank(normalizer: fn($value) => trim($value))->validate(' '); // true
4040

4141
### `message`
4242

43-
type: `?string` default: `The {{ name }} value should be blank, {{ value }} given.`
43+
type: `?string` default: `The {{ name }} value should be blank.`
4444

4545
Message that will be shown if the value is not blank.
4646

docs/03-rules_choice.md

+4-7
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2, max: 3)->val
4242
> [!NOTE]
4343
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and the input value is not an `array`.
4444
45-
> [!NOTE]
46-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
47-
4845
## Options
4946

5047
### `constraints`
@@ -78,7 +75,7 @@ For example, if `max` is 2, the input array must have at most 2 values.
7875

7976
### `message`
8077

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

8380
Message that will be shown if input value is not a valid choice.
8481

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

9390
### `multipleMessage`
9491

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

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

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

107104
### `minMessage`
108105

109-
type: `?string` default: `The {{ name }} value must have at least {{ min }} choices, {{ numElements }} choices given.`
106+
type: `?string` default: `The {{ name }} value must have at least {{ min }} choice(s).`
110107

111108
Message that will be shown when `multiple` is `true` and input array has fewer values than the defined in `min`.
112109

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

124121
### `maxMessage`
125122

126-
type: `?string` default: `The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.`
123+
type: `?string` default: `The {{ name }} value must have at most {{ max }} choice(s).`
127124

128125
Message that will be shown when `multiple` is `true` and input array has more values than the defined in `max`.
129126

docs/03-rules_count.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
3232
> [!NOTE]
3333
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
3434
35-
> [!NOTE]
36-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
37-
3835
> [!NOTE]
3936
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
4037
@@ -54,7 +51,7 @@ It defines the maximum number of elements required.
5451

5552
### `minMessage`
5653

57-
type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more, {{ numElements }} elements given.`
54+
type: `?string` default: `The {{ name }} value should contain {{ min }} elements or more.`
5855

5956
Message that will be shown when the input value has fewer elements than the defined in `min`.
6057

@@ -70,7 +67,7 @@ The following parameters are available:
7067

7168
### `maxMessage`
7269

73-
type: `?string` default: `The {{ name }} value should contain {{ max }} elements or less, {{ numElements }} elements given.`
70+
type: `?string` default: `The {{ name }} value should contain {{ max }} elements or less.`
7471

7572
Message that will be shown when the input value has more elements than the defined in `max`.
7673

@@ -86,7 +83,7 @@ The following parameters are available:
8683

8784
### `exactMessage`
8885

89-
type: `?string` default: `The {{ name }} value should contain exactly {{ min }} elements, {{ numElements }} elements given.`
86+
type: `?string` default: `The {{ name }} value should contain exactly {{ min }} elements.`
9087

9188
Message that will be shown when `min` and `max` options have the same value and the input value has a different number of elements.
9289

docs/03-rules_country.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Available options:
4141

4242
### `message`
4343

44-
type: `?string` default: `The {{ name }} value is not a valid country, {{ value }} given.`
44+
type: `?string` default: `The {{ name }} value is not a valid country.`
4545

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

docs/03-rules_each-key.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ Validator that will validate each key of an `array` or object implementing `\Tra
3434

3535
### `message`
3636

37-
type: `?string` default: `Invalid key: {{ message }}`
37+
type: `?string` default: `Invalid key {{ key }}: {{ message }}`
3838

3939
Message that will be shown if at least one input value key is invalid according to the given `validator`.
4040

4141
```php
42-
// Throws: Invalid key: The color key value should be of type "string", 1 given.
42+
// throws: Invalid key 1: The value should be of type "string".
4343
Validator::eachKey(
4444
Validator::type('string')
4545
)->assert(['red' => '#f00', 1 => '#0f0'], 'color');

docs/03-rules_each-value.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type: `?string` default: `At key "{{ key }}": {{ message }}`
3939
Message that will be shown if at least one input value element is invalid according to the given `validator`.
4040

4141
```php
42-
// Throws: At key 2: The color value should not be blank, "" given.
42+
// throws: At key 2: The color value should not be blank.
4343
Validator::eachValue(
4444
Validator::notBlank()
4545
)->assert(['red', 'green', ''], 'color');

docs/03-rules_email.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Validator::email(normalizer: fn($value) => trim($value))->validate('test@example
5656

5757
### `message`
5858

59-
type: `?string` default: `The {{ name }} value is not a valid email address, {{ value }} given.`
59+
type: `?string` default: `The {{ name }} value is not a valid email address.`
6060

6161
Message that will be shown if the input value is not a valid email address.
6262

docs/03-rules_greater-than-or-equal.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -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 }}.`
4848

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

docs/03-rules_greater-than.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -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 }}.`
4848

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

docs/03-rules_is-false.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Validator::isFalse()->validate(false); // true
2121

2222
### `message`
2323

24-
type: `?string` default: `The {{ name }} value should be false, {{ value }} given.`
24+
type: `?string` default: `The {{ name }} value should be false.`
2525

2626
Message that will be shown if the value is false.
2727

docs/03-rules_is-null.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Validator::isNull()->validate(null); // true
2121

2222
### `message`
2323

24-
type: `?string` default: `The {{ name }} value should be null, {{ value }} given.`
24+
type: `?string` default: `The {{ name }} value should be null.`
2525

2626
Message that will be shown if the value is null.
2727

docs/03-rules_is-true.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Validator::isTrue()->validate(true); // true
2121

2222
### `message`
2323

24-
type: `?string` default: `The {{ name }} value should be true, {{ value }} given.`
24+
type: `?string` default: `The {{ name }} value should be true.`
2525

2626
Message that will be shown if the value is true.
2727

docs/03-rules_language.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Available options:
4141

4242
### `message`
4343

44-
type: `?string` default: `The {{ name }} value is not a valid language, {{ value }} given.`
44+
type: `?string` default: `The {{ name }} value is not a valid language.`
4545

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

docs/03-rules_length.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ Validator::length(max: 1, countUnit: 'graphemes')->validate('🔥'); // true
4343
> [!NOTE]
4444
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
4545
46-
> [!NOTE]
47-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
48-
4946
> [!NOTE]
5047
> An `UnexpectedValueException` will be thrown when the `charset` value is not a valid option.
5148
> Check all the supported character encodings [here](https://www.php.net/manual/en/mbstring.supported-encodings.php).
@@ -107,7 +104,7 @@ Validator::length(max: 3, normalizer: fn($value) => trim($value))->validate('abc
107104

108105
### `minMessage`
109106

110-
type: `?string` default: `The {{ name }} value should have {{ min }} characters or more, {{ numChars }} characters given.`
107+
type: `?string` default: `The {{ name }} value should have {{ min }} character(s) or more.`
111108

112109
Message that will be shown when the input value has fewer characters than the defined in `min`.
113110

@@ -125,7 +122,7 @@ The following parameters are available:
125122

126123
### `maxMessage`
127124

128-
type: `?string` default: `The {{ name }} value should have {{ max }} characters or less, {{ numChars }} characters given.`
125+
type: `?string` default: `The {{ name }} value should have {{ max }} character(s) or less.`
129126

130127
Message that will be shown when the input value has more characters than the defined in `max`.
131128

@@ -143,7 +140,7 @@ The following parameters are available:
143140

144141
### `exactMessage`
145142

146-
type: `?string` default: `The {{ name }} value should have exactly {{ min }} characters, {{ numChars }} characters given.`
143+
type: `?string` default: `The {{ name }} value should have exactly {{ min }} characters.`
147144

148145
Message that will be shown when `min` and `max` options have the same value and the input value has a different number of characters.
149146

docs/03-rules_less-than-or-equal.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -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 }}.`
4848

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

docs/03-rules_less-than.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -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 }}.`
4848

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

docs/03-rules_locale.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ If `true`, the input value will be normalized before validation, according to th
4040

4141
### `message`
4242

43-
type: `?string` default: `The {{ name }} value is not a valid locale, {{ value }} given.`
43+
type: `?string` default: `The {{ name }} value is not a valid locale.`
4444

4545
Message that will be shown if the input value is not a valid locale code.
4646

docs/03-rules_not-blank.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Validator::notBlank(normalizer: fn($value) => trim($value))->validate(' '); // f
4040

4141
### `message`
4242

43-
type: `?string` default: `The {{ name }} value should not be blank, {{ value }} given.`
43+
type: `?string` default: `The {{ name }} value should not be blank.`
4444

4545
Message that will be shown if the value is blank.
4646

docs/03-rules_range.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(
3636
> [!NOTE]
3737
> An `UnexpectedValueException` will be thrown when trying to compare incomparable values, like a `string` with an `int`.
3838
39-
> [!NOTE]
40-
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.
41-
4239
## Options
4340

4441
### `min`
@@ -57,7 +54,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.
5754

5855
### `message`
5956

60-
type: `?string` default: `The {{ name }} value should be between {{ min }} and {{ max }}, {{ value }} given.`
57+
type: `?string` default: `The {{ name }} value should be between {{ min }} and {{ max }}.`
6158

6259
Message that will be shown if the value is not between the minimum and maximum values.
6360

docs/03-rules_timezone.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Check the [official country codes](https://en.wikipedia.org/wiki/ISO_3166-1#Curr
7575

7676
### `message`
7777

78-
type: `?string` default: `The {{ name }} value is not a valid timezone, {{ value }} given.`
78+
type: `?string` default: `The {{ name }} value is not a valid timezone.`
7979

8080
Message that will be shown if the input value is not a valid timezone.
8181

docs/03-rules_type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Available character type constraints:
7777

7878
### `message`
7979

80-
type: `?string` default: `The {{ name }} value should be of type {{ constraint }}, {{ value }} given.`
80+
type: `?string` default: `The {{ name }} value should be of type {{ constraint }}.`
8181

8282
Message that will be shown if input value is not of a specific type.
8383

docs/03-rules_url.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Validator::url(normalizer: fn($value) => trim($value))->validate('https://exampl
5858

5959
### `message`
6060

61-
type: `?string` default: `The {{ name }} value is not a valid URL address, {{ value }} given.`
61+
type: `?string` default: `The {{ name }} value is not a valid URL address.`
6262

6363
Message that will be shown if the input value is not a valid URL address.
6464

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class InvalidOptionException extends UnexpectedValueException
6+
{
7+
public function __construct(string $name, string|array|null $expected = null)
8+
{
9+
$message = \sprintf('The "%s" option is not valid.', $name);
10+
11+
if (\is_array($expected)) {
12+
$message = \sprintf('%s Accepted values are: "%s".', $message, \implode('", "', $expected));
13+
}
14+
else if (\is_string($expected)) {
15+
$message = \sprintf('%s %s', $message, $expected);
16+
}
17+
18+
parent::__construct($message);
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\Validator\Exception;
4+
5+
class OptionDefinitionException extends UnexpectedValueException {}

src/Exception/UnexpectedComparableException.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
class UnexpectedComparableException extends UnexpectedValueException
66
{
7-
public function __construct(string $value1, string $value2)
7+
public function __construct(mixed $value1, mixed $value2)
88
{
9-
$message = \sprintf('Cannot compare a type "%s" with a type "%s".', $value1, $value2);
9+
$message = \sprintf(
10+
'Cannot compare a type "%s" with a type "%s".',
11+
\get_debug_type($value1),
12+
\get_debug_type($value2)
13+
);
1014

1115
parent::__construct($message);
1216
}

src/Exception/UnexpectedOptionException.php

-13
This file was deleted.

src/Exception/UnexpectedTypeException.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
class UnexpectedTypeException extends UnexpectedValueException
66
{
7-
public function __construct(string $expected, string $given)
7+
public function __construct(mixed $value, string $expectedType)
88
{
9-
$message = \sprintf('Expected value of type "%s", "%s" given.', $expected, $given);
9+
$message = \sprintf('Expected value of type "%s", "%s" given.', $expectedType, \get_debug_type($value));
1010

1111
parent::__construct($message);
1212
}

0 commit comments

Comments
 (0)