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

Commit 8c081aa

Browse files
committed
chore: added validation when min is greater than max
1 parent 8cd8232 commit 8c081aa

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

docs/03-rules_each-value.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## EachValue
22

3-
Validates every element of an `array` or object implementing `\Traversable` with a given set of rules.
3+
Validates every element of an `array`, or object implementing `\Traversable`, with a given set of rules.
44

55
```php
66
EachValue(

src/ChainedValidatorInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ public function choice(
1818
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.'
1919
): ChainedValidatorInterface&Validator;
2020

21+
public function count(
22+
?int $min = null,
23+
?int $max = null,
24+
?string $minMessage = null,
25+
?string $maxMessage = null,
26+
?string $exactMessage = null
27+
): ChainedValidatorInterface&Validator;
28+
2129
public function country(
2230
string $code = 'alpha-2',
2331
string $message = 'The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.'

src/Rule/Count.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use ProgrammatorDev\Validator\Exception\CountException;
66
use ProgrammatorDev\Validator\Exception\UnexpectedTypeException;
77
use ProgrammatorDev\Validator\Exception\UnexpectedValueException;
8+
use ProgrammatorDev\Validator\Validator;
89

910
class Count extends AbstractRule implements RuleInterface
1011
{
1112
private string $minMessage = 'The {{ name }} value should contain {{ min }} elements or more, {{ numElements }} elements given.';
1213
private string $maxMessage = 'The {{ name }} value should contain {{ max }} elements or less, {{ numElements }} elements given.';
13-
private string $exactMessage = 'The {{ name }} value should contain exactly {{ numElements }} elements, {{ numElements }} elements given.';
14+
private string $exactMessage = 'The {{ name }} value should contain exactly {{ min }} elements, {{ numElements }} elements given.';
1415

1516
public function __construct(
1617
private readonly ?int $min = null,
@@ -31,6 +32,14 @@ public function assert(mixed $value, ?string $name = null): void
3132
throw new UnexpectedValueException('At least one of the options "min" or "max" must be given.');
3233
}
3334

35+
if (
36+
$this->min !== null
37+
&& $this->max !== null
38+
&& !Validator::greaterThanOrEqual($this->min)->validate($this->max)
39+
) {
40+
throw new UnexpectedValueException('Maximum value must be greater than or equal to minimum value.');
41+
}
42+
3443
if (!\is_countable($value)) {
3544
throw new UnexpectedTypeException('array|\Countable', get_debug_type($value));
3645
}

src/Rule/Range.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public function assert(mixed $value, ?string $name = null): void
2828
}
2929

3030
if (!Validator::greaterThan($this->min)->validate($this->max)) {
31-
throw new UnexpectedValueException(
32-
'Maximum value must be greater than minimum value.'
33-
);
31+
throw new UnexpectedValueException('Maximum value must be greater than minimum value.');
3432
}
3533

3634
if (!Validator::greaterThanOrEqual($this->min)->lessThanOrEqual($this->max)->validate($value)) {

src/StaticValidatorInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ public static function choice(
1717
string $maxMessage = 'The {{ name }} value must have at most {{ max }} choices, {{ numElements }} choices given.'
1818
): ChainedValidatorInterface&Validator;
1919

20+
public static function count(
21+
?int $min = null,
22+
?int $max = null,
23+
?string $minMessage = null,
24+
?string $maxMessage = null,
25+
?string $exactMessage = null
26+
): ChainedValidatorInterface&Validator;
27+
2028
public static function country(
2129
string $code = 'alpha-2',
2230
string $message = 'The {{ name }} value is not a valid {{ code }} country code, {{ value }} given.'

tests/CountTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public static function provideRuleUnexpectedValueData(): \Generator
2020
{
2121
$missingOptionsMessage = '/At least one of the options "min" or "max" must be given./';
2222
$invalidTypeMessage = '/Expected value of type "array|\Countable", "(.*)" given./';
23+
$constraintMessage = '/Maximum value must be greater than or equal to minimum value./';
2324

2425
yield 'missing options' => [new Count(), [1, 2, 3], $missingOptionsMessage];
2526
yield 'invalid type value' => [new Count(min: 5, max: 10), 1, $invalidTypeMessage];
27+
yield 'min greater than max constraint' => [new Count(min: 10, max: 5), 1, $constraintMessage];
2628
}
2729

2830
public static function provideRuleFailureConditionData(): \Generator

0 commit comments

Comments
 (0)