Skip to content

Commit

Permalink
Fix #626: Disallow $min greater than amount of $attributes in `At…
Browse files Browse the repository at this point in the history
…Least` configuration
  • Loading branch information
arogachev committed Aug 26, 2023
1 parent 4e79a26 commit 2f76c17
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- Chg #623: List translated attributes in error message for `OneOf` and `AtLeast` rules (@arogachev)
- Chg #624: Fix meaning of error message in `OneOf` rule (@arogachev)
- Chg #625: Improve meaning and use pluralization in error message for `OneOf` and `AtLeast` rules (@arogachev)
- Chg #626: Disallow `$min` greater than amount of `$attributes` in `AtLeast` configuration (@arogachev)

## 1.1.0 April 06, 2023

Expand Down
6 changes: 6 additions & 0 deletions src/Rule/AtLeast.php
Expand Up @@ -6,6 +6,7 @@

use Attribute;
use Closure;
use InvalidArgumentException;
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
use Yiisoft\Validator\Rule\Trait\WhenTrait;
Expand All @@ -14,6 +15,8 @@
use Yiisoft\Validator\SkipOnErrorInterface;
use Yiisoft\Validator\WhenInterface;

use function count;

/**
* Defines validation options to check that a minimum number of specified attributes are filled.
*
Expand Down Expand Up @@ -64,6 +67,9 @@ public function __construct(
private bool $skipOnError = false,
private Closure|null $when = null
) {
if ($min > count($this->attributes)) {
throw new InvalidArgumentException('$min must be no greater than amount of $attributes.');
}
}

public function getName(): string
Expand Down
26 changes: 21 additions & 5 deletions tests/Rule/AtLeastTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Validator\Tests\Rule;

use InvalidArgumentException;
use Yiisoft\Validator\EmptyCondition\NeverEmpty;
use Yiisoft\Validator\Rule\AtLeast;
use Yiisoft\Validator\Rule\AtLeastHandler;
Expand All @@ -21,9 +22,16 @@ final class AtLeastTest extends RuleTestCase
use SkipOnErrorTestTrait;
use WhenTestTrait;

public function testMinGreaterThanAttributesCount(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('$min must be no greater than amount of $attributes.');
new AtLeast(['attr'], min: 2);
}

public function testGetName(): void
{
$rule = new AtLeast([]);
$rule = new AtLeast(['attr']);
$this->assertSame('atLeast', $rule->getName());
}

Expand Down Expand Up @@ -168,6 +176,14 @@ public function __construct()
['attr1' => 1, 'attr2' => 2],
[new AtLeast(['attr1', 'attr2'])],
],
'min equals amount of attributes' => [
['attr1' => 1, 'attr2' => 2],
[new AtLeast(['attr1', 'attr2'], min: 2)],
],
'min equals amount of attributes, 0' => [
[],
[new AtLeast([], min: 0)],
],
'class attribute' => [
new AtLeastDto(1),
],
Expand Down Expand Up @@ -225,8 +241,8 @@ public function dataValidationFailed(): array
],
'array, custom min' => [
$array,
[new AtLeast(['attr2'], min: 2)],
['' => ['At least 2 attributes from this list must be filled: "attr2".']],
[new AtLeast(['attr1', 'attr2'], min: 2)],
['' => ['At least 2 attributes from this list must be filled: "attr1", "attr2".']],
],
'custom message' => [
$class,
Expand All @@ -253,13 +269,13 @@ public function dataValidationFailed(): array

public function testSkipOnError(): void
{
$this->testSkipOnErrorInternal(new AtLeast([]), new AtLeast([], skipOnError: true));
$this->testSkipOnErrorInternal(new AtLeast(['attr']), new AtLeast(['attr'], skipOnError: true));
}

public function testWhen(): void
{
$when = static fn (mixed $value): bool => $value !== null;
$this->testWhenInternal(new AtLeast([]), new AtLeast([], when: $when));
$this->testWhenInternal(new AtLeast(['attr']), new AtLeast(['attr'], when: $when));
}

protected function getDifferentRuleInHandlerItems(): array
Expand Down

0 comments on commit 2f76c17

Please sign in to comment.