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

Commit 254934a

Browse files
committed
fix: do not allow validator to assert/validate with no rules given
1 parent e3ea76e commit 254934a

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Validator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator;
44

55
use ProgrammatorDev\YetAnotherPhpValidator\Exception\RuleNotFoundException;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
67
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
78
use ProgrammatorDev\YetAnotherPhpValidator\Factory\Factory;
89
use ProgrammatorDev\YetAnotherPhpValidator\Rule\RuleInterface;
@@ -48,6 +49,10 @@ public function __call(string $ruleName, array $arguments = []): self
4849
*/
4950
public function assert(mixed $value, string $name): void
5051
{
52+
if (empty($this->getRules())) {
53+
throw new UnexpectedValueException('Validator rules not found: at least one rule is required.');
54+
}
55+
5156
foreach ($this->getRules() as $rule) {
5257
$rule->assert($value, $name);
5358
}

tests/ValidatorTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProgrammatorDev\YetAnotherPhpValidator\Test;
44

55
use PHPUnit\Framework\Attributes\DataProvider;
6+
use ProgrammatorDev\YetAnotherPhpValidator\Exception\UnexpectedValueException;
67
use ProgrammatorDev\YetAnotherPhpValidator\Exception\ValidationException;
78
use ProgrammatorDev\YetAnotherPhpValidator\Rule\GreaterThan;
89
use ProgrammatorDev\YetAnotherPhpValidator\Rule\LessThan;
@@ -12,6 +13,15 @@
1213

1314
class ValidatorTest extends AbstractTest
1415
{
16+
public function testValidatorRequiredRules()
17+
{
18+
$this->expectException(UnexpectedValueException::class);
19+
$this->expectExceptionMessage('Validator rules not found: at least one rule is required.');
20+
21+
$validator = new Validator();
22+
$validator->assert(true, 'test');
23+
}
24+
1525
#[DataProvider('provideValidatorUsageApproachData')]
1626
public function testValidatorGetRules(Validator $validator)
1727
{
@@ -23,7 +33,6 @@ public function testValidatorGetRules(Validator $validator)
2333
public function testValidatorFailureCondition(Validator $validator)
2434
{
2535
$this->assertFalse($validator->validate(false));
26-
2736
$this->expectException(ValidationException::class);
2837
$validator->assert(false, 'test');
2938
}
@@ -32,7 +41,6 @@ public function testValidatorFailureCondition(Validator $validator)
3241
public function testValidatorSuccessCondition(Validator $validator)
3342
{
3443
$this->assertTrue($validator->validate(15));
35-
3644
$validator->assert(15, 'test');
3745
}
3846

@@ -52,7 +60,7 @@ public static function provideValidatorUsageApproachData(): \Generator
5260
)
5361
];
5462
yield 'method approach' => [
55-
(new Validator())
63+
(new Validator)
5664
->addRule(new NotBlank())
5765
->addRule(new GreaterThan(10))
5866
->addRule(new LessThan(20))

0 commit comments

Comments
 (0)