Skip to content

Commit 422545a

Browse files
committed
fix(validator): initialize empty constraints
1 parent 4dd053e commit 422545a

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/Validator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class Validator
1515
{
1616
/** @var Constraint[] */
17-
private array $constraints;
17+
private array $constraints = [];
1818

1919
/** @var string[] */
2020
private static array $namespaces = [];
@@ -106,4 +106,4 @@ public static function setTranslator(?TranslatorInterface $translator): void
106106
{
107107
self::$translator = $translator;
108108
}
109-
}
109+
}

tests/ValidatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public function testValidate(): void
4949
$this->assertCount(0, $violations);
5050
}
5151

52+
public function testValidateWithoutConstraints(): void
53+
{
54+
$violations = (new Validator())->validate('anything');
55+
56+
$this->assertInstanceOf(ConstraintViolationList::class, $violations);
57+
$this->assertCount(0, $violations);
58+
}
59+
5260
public function testAssertFail(): void
5361
{
5462
$this->expectException(ValidationFailedException::class);
@@ -67,6 +75,11 @@ public function testIsValid(): void
6775
$this->assertTrue($this->validator->isValid(18));
6876
}
6977

78+
public function testIsValidWithoutConstraints(): void
79+
{
80+
$this->assertTrue((new Validator())->isValid('anything'));
81+
}
82+
7083
public function testToArray(): void
7184
{
7285
$constraints = $this->validator->toArray();
@@ -76,6 +89,11 @@ public function testToArray(): void
7689
$this->assertInstanceOf(LessThan::class, $constraints[2]);
7790
}
7891

92+
public function testToArrayWithoutConstraints(): void
93+
{
94+
$this->assertSame([], (new Validator())->toArray());
95+
}
96+
7997
public function testCustomConstraint(): void
8098
{
8199
Validator::addNamespace('ProgrammatorDev\FluentValidator\Test\Fixtures\Constraint');

0 commit comments

Comments
 (0)