Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor error messages #76

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $validator = Validator::type('int')->greaterThanOrEqual(18);

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

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion docs/01-get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ $validator = Validator::type('int')->greaterThanOrEqual(18);

// and validate with these:
$validator->validate(16); // returns bool: false
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18, 16 given.
$validator->assert(16, 'age'); // throws exception: The age value should be greater than or equal to 18.
```
2 changes: 1 addition & 1 deletion docs/03-rules_blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Validator::blank(normalizer: fn($value) => trim($value))->validate(' '); // true

### `message`

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

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

Expand Down
11 changes: 4 additions & 7 deletions docs/03-rules_choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ Validator::choice(['red', 'green', 'blue'], multiple: true, min: 2, max: 3)->val
> [!NOTE]
> An `UnexpectedValueException` will be thrown when `multiple` is `true` and the input value is not an `array`.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.

## Options

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

### `message`

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

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

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

### `multipleMessage`

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

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

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

### `minMessage`

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

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

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

### `maxMessage`

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

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

Expand Down
9 changes: 3 additions & 6 deletions docs/03-rules_count.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ Validator::count(min: 3, max: 3)->validate(['a', 'b', 'c']); // true
> [!NOTE]
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.
> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.
> [!NOTE]
> An `UnexpectedValueException` will be thrown when the input value is not an `array` or an object implementing `\Countable`.
Expand All @@ -54,7 +51,7 @@ It defines the maximum number of elements required.

### `minMessage`

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

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

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

### `maxMessage`

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

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

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

### `exactMessage`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_country.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Available options:

### `message`

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

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03-rules_each-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ Validator that will validate each key of an `array` or object implementing `\Tra

### `message`

type: `?string` default: `Invalid key: {{ message }}`
type: `?string` default: `Invalid key {{ key }}: {{ message }}`

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

```php
// Throws: Invalid key: The color key value should be of type "string", 1 given.
// throws: Invalid key 1: The value should be of type "string".
Validator::eachKey(
Validator::type('string')
)->assert(['red' => '#f00', 1 => '#0f0'], 'color');
Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_each-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type: `?string` default: `At key "{{ key }}": {{ message }}`
Message that will be shown if at least one input value element is invalid according to the given `validator`.

```php
// Throws: At key 2: The color value should not be blank, "" given.
// throws: At key 2: The color value should not be blank.
Validator::eachValue(
Validator::notBlank()
)->assert(['red', 'green', ''], 'color');
Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_email.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Validator::email(normalizer: fn($value) => trim($value))->validate('test@example

### `message`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_greater-than-or-equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `?string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be greater than or equal to {{ constraint }}.`

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_greater-than.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `?string` default: `The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be greater than {{ constraint }}.`

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_is-false.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Validator::isFalse()->validate(false); // true

### `message`

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

Message that will be shown if the value is false.

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_is-null.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Validator::isNull()->validate(null); // true

### `message`

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

Message that will be shown if the value is null.

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_is-true.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Validator::isTrue()->validate(true); // true

### `message`

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

Message that will be shown if the value is true.

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_language.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Available options:

### `message`

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

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

Expand Down
9 changes: 3 additions & 6 deletions docs/03-rules_length.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ Validator::length(max: 1, countUnit: 'graphemes')->validate('🔥'); // true
> [!NOTE]
> An `UnexpectedValueException` will be thrown when either `min` or `max` options are not given.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than the `max` value.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `charset` value is not a valid option.
> Check all the supported character encodings [here](https://www.php.net/manual/en/mbstring.supported-encodings.php).
Expand Down Expand Up @@ -107,7 +104,7 @@ Validator::length(max: 3, normalizer: fn($value) => trim($value))->validate('abc

### `minMessage`

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

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

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

### `maxMessage`

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

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

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

### `exactMessage`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_less-than-or-equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `?string` default: `The {{ name }} value should be less than or equal to {{ constraint }}, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be less than or equal to {{ constraint }}.`

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_less-than.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `?string` default: `The {{ name }} value should be less than {{ constraint }}, {{ value }} given.`
type: `?string` default: `The {{ name }} value should be less than {{ constraint }}.`

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_locale.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If `true`, the input value will be normalized before validation, according to th

### `message`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_not-blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Validator::notBlank(normalizer: fn($value) => trim($value))->validate(' '); // f

### `message`

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

Message that will be shown if the value is blank.

Expand Down
5 changes: 1 addition & 4 deletions docs/03-rules_range.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ Validator::range(new DateTime('yesterday'), new DateTime('tomorrow'))->validate(
> [!NOTE]
> An `UnexpectedValueException` will be thrown when trying to compare incomparable values, like a `string` with an `int`.

> [!NOTE]
> An `UnexpectedValueException` will be thrown when the `min` value is greater than or equal to the `max` value.

## Options

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

### `message`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_timezone.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Check the [official country codes](https://en.wikipedia.org/wiki/ISO_3166-1#Curr

### `message`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_type.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Available character type constraints:

### `message`

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/03-rules_url.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Validator::url(normalizer: fn($value) => trim($value))->validate('https://exampl

### `message`

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

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

Expand Down
20 changes: 20 additions & 0 deletions src/Exception/InvalidOptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

class InvalidOptionException extends UnexpectedValueException
{
public function __construct(string $name, string|array|null $expected = null)
{
$message = \sprintf('The "%s" option is not valid.', $name);

if (\is_array($expected)) {
$message = \sprintf('%s Accepted values are: "%s".', $message, \implode('", "', $expected));
}
else if (\is_string($expected)) {
$message = \sprintf('%s %s', $message, $expected);
}

parent::__construct($message);
}
}
5 changes: 5 additions & 0 deletions src/Exception/OptionDefinitionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace ProgrammatorDev\Validator\Exception;

class OptionDefinitionException extends UnexpectedValueException {}
8 changes: 6 additions & 2 deletions src/Exception/UnexpectedComparableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

class UnexpectedComparableException extends UnexpectedValueException
{
public function __construct(string $value1, string $value2)
public function __construct(mixed $value1, mixed $value2)
{
$message = \sprintf('Cannot compare a type "%s" with a type "%s".', $value1, $value2);
$message = \sprintf(
'Cannot compare a type "%s" with a type "%s".',
\get_debug_type($value1),
\get_debug_type($value2)
);

parent::__construct($message);
}
Expand Down
13 changes: 0 additions & 13 deletions src/Exception/UnexpectedOptionException.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Exception/UnexpectedTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

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

parent::__construct($message);
}
Expand Down
Loading
Loading