Skip to content

Commit

Permalink
Merge pull request #41 from programmatordev/1.x
Browse files Browse the repository at this point in the history
1.x
  • Loading branch information
andrepimpao committed Nov 3, 2023
2 parents 9202aee + 520b431 commit 08358d5
Show file tree
Hide file tree
Showing 48 changed files with 183 additions and 174 deletions.
2 changes: 1 addition & 1 deletion docs/01-get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ $validator = new Validator(

// 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, 16 given.
```
40 changes: 20 additions & 20 deletions docs/02-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;
*/
function getWeatherTemperature(float $latitude, float $longitude, string $unitSystem): float
{
Validator::range(-90, 90)->assert($latitude, 'Latitude');
Validator::range(-180, 180)->assert($longitude, 'Longitude');
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
Validator::range(-90, 90)->assert($latitude, 'latitude');
Validator::range(-180, 180)->assert($longitude, 'longitude');
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'unit system');

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

// ...
}
Expand All @@ -68,7 +68,7 @@ This method throws a `ValidationException` when a rule fails, otherwise nothing
/**
* @throws ValidationException
*/
assert(mixed $value, string $name): void;
assert(mixed $value, ?string $name = null): void;
```

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

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

// ...
}
Expand All @@ -90,7 +90,7 @@ try {
getWeatherTemperature(latitude: 100, longitude: 50, unitSystem: 'METRIC');
}
catch (ValidationException $exception) {
echo $exception->getMessage(); // The "Latitude" value should be between "-90" and "90", "100" given.
echo $exception->getMessage(); // The latitude value should be between -90 and 90, 100 given.
}
```
> **Note**
Expand Down Expand Up @@ -175,7 +175,7 @@ function calculateDiscount(float $price, float $discount, string $type): float
$discountValidator->addRule(new Rule\LessThanOrEqual(100));
}

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

// ...
}
Expand All @@ -197,9 +197,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception;
use ProgrammatorDev\YetAnotherPhpValidator\Validator;

try {
Validator::range(-90, 90)->assert($latitude, 'Latitude');
Validator::range(-180, 180)->assert($longitude, 'Longitude');
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
Validator::range(-90, 90)->assert($latitude, 'latitude');
Validator::range(-180, 180)->assert($longitude, 'longitude');
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'unit system');
}
catch (Exception\RangeException $exception) {
// Do something when Range fails
Expand All @@ -219,9 +219,9 @@ use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
use ProgrammatorDev\YetAnotherPhpValidator\Validator;

try {
Validator::range(-90, 90)->assert($latitude, 'Latitude');
Validator::range(-180, 180)->assert($longitude, 'Longitude');
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'Unit System');
Validator::range(-90, 90)->assert($latitude, 'latitude');
Validator::range(-180, 180)->assert($longitude, 'longitude');
Validator::notBlank()->choice(['METRIC', 'IMPERIAL'])->assert($unitSystem, 'unit system');
}
catch (ValidationException $exception) {
// Do something when a rule fails
Expand Down Expand Up @@ -261,8 +261,8 @@ use ProgrammatorDev\YetAnotherPhpValidator\Validator;

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

// Throws: "yellow" is not a valid color! You must select one of [red, green, blue].
// Throws: "yellow" is not a valid color! You must select one of ["red", "green", "blue"].
```
16 changes: 8 additions & 8 deletions docs/03x-rules-choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Choice(
bool $multiple = false,
?int $minConstraint = null,
?int $maxConstraint = null,
string $message = 'The "{{ name }}" value is not a valid choice, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
string $multipleMessage = 'The "{{ name }}" value has one or more invalid choices, "{{ value }}" given. Accepted values are: "{{ constraints }}".',
string $minMessage = 'The "{{ name }}" value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
string $maxMessage = 'The "{{ name }}" value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
string $message = 'The {{ name }} value is not a valid choice, {{ value }} given. Accepted values are: {{ constraints }}.',
string $multipleMessage = 'The {{ name }} value has one or more invalid choices, {{ value }} given. Accepted values are: {{ constraints }}.',
string $minMessage = 'The {{ name }} value must have at least {{ minConstraint }} choices, {{ numValues }} choices given.',
string $maxMessage = 'The {{ name }} value must have at most {{ maxConstraint }} choices, {{ numValues }} choices given.'
);
```

Expand Down Expand Up @@ -78,7 +78,7 @@ For example, if `maxConstraint` 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, {{ value }} given. Accepted values are: {{ constraints }}.`

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

Expand All @@ -92,7 +92,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, {{ value }} given. 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 +106,7 @@ The following parameters are available:

### `minMessage`

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

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

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

### `maxMessage`

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

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-country.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Validates that a value is a valid country code.
```php
Country(
string $code = 'alpha-2',
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
string $message = 'The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.'
);
```

Expand Down Expand Up @@ -42,7 +42,7 @@ Available options:

### `message`

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

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-each-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type: `string` default: `Invalid key: {{ message }}`
Message that will be shown if at least one input value key is invalid according to the given `validator`.

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

The following parameters are available:
Expand Down
6 changes: 3 additions & 3 deletions docs/03x-rules-each-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Validates every element of an `array` or object implementing `\Traversable` with
```php
EachValue(
Validator $validator,
string $message = 'At key "{{ key }}": {{ message }}'
string $message = 'At key {{ key }}: {{ message }}'
);
```

Expand Down Expand Up @@ -34,8 +34,8 @@ 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
Validator::eachValue(Validator::notBlank())->assert(['red', 'green', ''], 'Test');
// Throws: At key "2": The "Test" value should not be blank, "" given.
Validator::eachValue(Validator::notBlank())->assert(['red', 'green', ''], 'color');
// Throws: At key 2: The color value should not be blank, "" given.
```

The following parameters are available:
Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-greater-than-or-equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
```php
GreaterThanOrEqual(
mixed $constraint,
string $message = 'The "{{ name }}" value should be greater than or equal to "{{ constraint }}", "{{ value }}" given.'
string $message = 'The {{ name }} value should be greater than or equal to {{ constraint }}, {{ value }} given.'
);
```

Expand Down 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 }}, {{ value }} given.`

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-greater-than.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
```php
GreaterThan(
mixed $constraint,
string $message = 'The "{{ name }}" value should be greater than "{{ constraint }}", "{{ value }}" given.'
string $message = 'The {{ name }} value should be greater than {{ constraint }}, {{ value }} given.'
);
```

Expand Down 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 }}, {{ value }} given.`

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-less-than-or-equal.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
```php
LessThanOrEqual(
mixed $constraint,
string $message = 'The "{{ name }}" value should be less than or equal to "{{ constraint }}", "{{ value }}" given.'
string $message = 'The {{ name }} value should be less than or equal to {{ constraint }}, {{ value }} given.'
);
```

Expand Down 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 }}, {{ value }} given.`

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-less-than.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Can compare between strings, numbers and dates.
```php
LessThan(
mixed $constraint,
string $message = 'The "{{ name }}" value should be less than "{{ constraint }}", "{{ value }}" given.'
string $message = 'The {{ name }} value should be less than {{ constraint }}, {{ value }} given.'
);
```

Expand Down 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 }}, {{ value }} given.`

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

Expand Down
6 changes: 3 additions & 3 deletions docs/03x-rules-not-blank.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Validates that a value is not equal to a blank string, blank array, `false` or `
```php
NotBlank(
?callable $normalizer = null,
string $message = 'The "{{ name }}" value should not be blank, "{{ value }}" given.'
string $message = 'The {{ name }} value should not be blank, {{ value }} given.'
);
```

## Basic Usage

Bellow are the only cases where the rule will fail,
Bellow are the *only* cases where the rule will fail by default,
everything else is considered valid (you may want to check the [`normalizer`](#normalizer) option for a different behaviour):

```php
Expand Down Expand Up @@ -41,7 +41,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, {{ value }} given.`

Message that will be shown if the value is blank.

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-range.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Can compare between strings, numbers and dates.
Range(
mixed $minConstraint,
mixed $maxConstraint,
string $message = 'The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.'
string $message = 'The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.'
);
```

Expand Down Expand Up @@ -57,7 +57,7 @@ Can be a `string`, `int`, `float` or `DateTimeInterface` object.

### `message`

type: `string` default: `The "{{ name }}" value should be between "{{ minConstraint }}" and "{{ maxConstraint }}", "{{ value }}" given.`
type: `string` default: `The {{ name }} value should be between {{ minConstraint }} and {{ maxConstraint }}, {{ value }} given.`

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-timezone.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Validates that a value is a valid timezone identifier.
Timezone(
string $timezoneGroup = \DateTimeZone::ALL,
?string $countryCode = null,
string $message = 'The "{{ name }}" value is not a valid timezone, "{{ value }}" given.'
string $message = 'The {{ name }} value is not a valid timezone, {{ value }} given.'
);
```

Expand Down 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, {{ value }} given.`

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

Expand Down
4 changes: 2 additions & 2 deletions docs/03x-rules-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ For example, if `['alpha', 'numeric']` is provided, it will validate if the valu
```php
Type(
string|array $constraint,
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'
string $message = 'The {{ name }} value should be of type {{ constraint }}, {{ value }} given.'
);
```

Expand Down 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 }}, {{ value }} given.`

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

Expand Down
8 changes: 4 additions & 4 deletions docs/04-custom-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CustomRule extends AbstractRule implements RuleInterface
{
if ($value === 0) {
throw new CustomRuleException(
message: 'The "{{ name }}" value cannot be zero!',
message: 'The {{ name }} value cannot be zero!',
parameters: [
'name' => $name
]
Expand All @@ -76,7 +76,7 @@ $validator = new Validator(new CustomRule());
// With multiple rules
$validator = new Validator(new Range(-10, 10), new CustomRule());

$validator->assert(0, 'test'); // throws: The "test" value cannot be zero!
$validator->assert(0, 'test'); // throws: The test value cannot be zero!
$validator->validate(0); // false
```

Expand All @@ -98,11 +98,11 @@ class Favorite extends AbstractRule implements RuleInterface
private readonly string $favorite
)

public function assert(mixed $value, string $name): void
public function assert(mixed $value, ?string $name = null): void
{
if ($this->favorite !== $value) {
throw new FavoriteException(
message: 'My favorite {{ name }} is "{{ favorite }}", not "{{ value }}"!',
message: 'My favorite {{ name }} is {{ favorite }}, not {{ value }}!',
parameters: [
'name' => $name, // {{ name }}
'favorite' => $this->favorite, // {{ favorite }}
Expand Down

0 comments on commit 08358d5

Please sign in to comment.