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

Commit 6e6e5e3

Browse files
committed
chore: check if min is greater than max constraint
1 parent 49b449d commit 6e6e5e3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Rule/Choice.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ public function assert(mixed $value, string $name): void
4343
);
4444
}
4545

46+
if (
47+
$this->multiple
48+
&& $this->minConstraint !== null
49+
&& $this->maxConstraint !== null
50+
&& $this->minConstraint > $this->maxConstraint
51+
) {
52+
throw new UnexpectedValueException(
53+
'Max constraint value must be greater than or equal to min constraint value.'
54+
);
55+
}
56+
4657
if ($this->multiple) {
4758
foreach ($value as $input) {
4859
if (!\in_array($input, $this->constraints, true)) {

tests/ChoiceTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ class ChoiceTest extends AbstractTest
1818

1919
public static function provideRuleUnexpectedValueData(): \Generator
2020
{
21-
yield 'multiple but not array' => [
22-
new Choice([1, 2], true), 1, '/Expected value of type "array" when multiple, "(.*)" given/'
23-
];
21+
$constraints = [1, 2, 3, 4, 5];
22+
$multipleMessage = '/Expected value of type "array" when multiple, "(.*)" given/';
23+
$constraintMessage = '/Max constraint value must be greater than or equal to min constraint value./';
24+
25+
yield 'multiple not array' => [new Choice($constraints, true), 1, $multipleMessage];
26+
yield 'min greater than max constraint' => [new Choice($constraints, true, 3, 2), [1, 2], $constraintMessage];
2427
}
2528

2629
public static function provideRuleFailureConditionData(): \Generator
@@ -50,7 +53,8 @@ public static function provideRuleSuccessConditionData(): \Generator
5053
yield 'multiple valid choices' => [new Choice($constraints, true), [1, 2, 3]];
5154
yield 'min constraint' => [new Choice($constraints, true, 2), [1, 2]];
5255
yield 'max constraint' => [new Choice($constraints, true, null, 2), [1, 2]];
53-
yield 'min and max constraint' => [new Choice($constraints, true, 2, 2), [1, 2]];
56+
yield 'min and max constraint' => [new Choice($constraints, true, 2, 4), [1, 2, 3]];
57+
yield 'same min and max constraint' => [new Choice($constraints, true, 2, 2), [1, 2]];
5458
}
5559

5660
public static function provideRuleMessageOptionData(): \Generator

0 commit comments

Comments
 (0)