Skip to content

Commit 8fffe25

Browse files
authored
Merge pull request #3 from programmatordev/FV-1-fix-name-on-assert-message
Improve violation path on assert message when validating arrays
2 parents 7ba7647 + d7ac84b commit 8fffe25

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

src/Factory/ConstraintFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function create(string $constraintName, array $arguments = []): Constrain
1717
foreach ($this->namespaces as $namespace) {
1818
$class = sprintf('%s\%s', $namespace, $constraintName);
1919

20+
// if class exists and is an instance of Constraint
2021
if (class_exists($class) && is_a($class, Constraint::class, true)) {
2122
return new $class(...$arguments);
2223
}

src/Validator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ public function assert(mixed $value, ?string $name = null, string|GroupSequence|
6969
$violations = $this->validate($value, $name, $groups);
7070

7171
if ($violations->count() > 0) {
72-
$message = $violations->get(0)->getMessage();
72+
$violation = $violations->get(0);
73+
$message = $violation->getMessage();
7374

7475
if ($name !== null) {
75-
$message = sprintf('%s: %s', $name, $message);
76+
$message = sprintf('%s: %s', $violation->getPropertyPath(), $message);
7677
}
7778

7879
throw new ValidationFailedException($message, $value, $violations);

tests/ValidatorTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public function testConstraintThatDoesNotExist(): void
4040

4141
public function testValidate(): void
4242
{
43-
// test fail
4443
$violations = $this->validator->validate(16);
4544
$this->assertInstanceOf(ConstraintViolationList::class, $violations);
4645
$this->assertCount(1, $violations);
4746

48-
// test success
4947
$violations = $this->validator->validate(18);
5048
$this->assertInstanceOf(ConstraintViolationList::class, $violations);
5149
$this->assertCount(0, $violations);
@@ -65,9 +63,7 @@ public function testAssertSuccess(): void
6563

6664
public function testIsValid(): void
6765
{
68-
// test fail
6966
$this->assertFalse($this->validator->isValid(16));
70-
// test success
7167
$this->assertTrue($this->validator->isValid(18));
7268
}
7369

@@ -84,22 +80,20 @@ public function testCustomConstraint(): void
8480
{
8581
Validator::addNamespace('ProgrammatorDev\FluentValidator\Test\Constraint');
8682

87-
// test fail
8883
$this->assertFalse(Validator::containsAlphanumeric()->isValid('!'));
89-
// test success
9084
$this->assertTrue(Validator::containsAlphanumeric()->isValid('v4l1d'));
9185
}
9286

9387
public function testSetTranslator(): void
9488
{
9589
// by default, error is in English
9690
$violations = $this->validator->validate('');
97-
$this->assertEquals('This value should not be blank.', $violations[0]->getMessage());
91+
$this->assertEquals('This value should not be blank.', $violations->get(0)->getMessage());
9892

9993
// set translator and then try again
10094
Validator::setTranslator(new Translator('pt'));
10195
// now error is in Portuguese
10296
$violations = $this->validator->validate('');
103-
$this->assertEquals('Este valor não deveria ser vazio.', $violations[0]->getMessage());
97+
$this->assertEquals('Este valor não deveria ser vazio.', $violations->get(0)->getMessage());
10498
}
10599
}

0 commit comments

Comments
 (0)