From d7ac84b942a9e8bd586b84242dd78e9b1090ab59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 1 Apr 2025 15:52:07 +0100 Subject: [PATCH] chore: improve violation path on assert message when validating arrays --- src/Factory/ConstraintFactory.php | 1 + src/Validator.php | 5 +++-- tests/ValidatorTest.php | 10 ++-------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Factory/ConstraintFactory.php b/src/Factory/ConstraintFactory.php index de6137e..46feefb 100644 --- a/src/Factory/ConstraintFactory.php +++ b/src/Factory/ConstraintFactory.php @@ -17,6 +17,7 @@ public function create(string $constraintName, array $arguments = []): Constrain foreach ($this->namespaces as $namespace) { $class = sprintf('%s\%s', $namespace, $constraintName); + // if class exists and is an instance of Constraint if (class_exists($class) && is_a($class, Constraint::class, true)) { return new $class(...$arguments); } diff --git a/src/Validator.php b/src/Validator.php index c25fee2..72031d8 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -69,10 +69,11 @@ public function assert(mixed $value, ?string $name = null, string|GroupSequence| $violations = $this->validate($value, $name, $groups); if ($violations->count() > 0) { - $message = $violations->get(0)->getMessage(); + $violation = $violations->get(0); + $message = $violation->getMessage(); if ($name !== null) { - $message = sprintf('%s: %s', $name, $message); + $message = sprintf('%s: %s', $violation->getPropertyPath(), $message); } throw new ValidationFailedException($message, $value, $violations); diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index c09d65f..8488d61 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -40,12 +40,10 @@ public function testConstraintThatDoesNotExist(): void public function testValidate(): void { - // test fail $violations = $this->validator->validate(16); $this->assertInstanceOf(ConstraintViolationList::class, $violations); $this->assertCount(1, $violations); - // test success $violations = $this->validator->validate(18); $this->assertInstanceOf(ConstraintViolationList::class, $violations); $this->assertCount(0, $violations); @@ -65,9 +63,7 @@ public function testAssertSuccess(): void public function testIsValid(): void { - // test fail $this->assertFalse($this->validator->isValid(16)); - // test success $this->assertTrue($this->validator->isValid(18)); } @@ -84,9 +80,7 @@ public function testCustomConstraint(): void { Validator::addNamespace('ProgrammatorDev\FluentValidator\Test\Constraint'); - // test fail $this->assertFalse(Validator::containsAlphanumeric()->isValid('!')); - // test success $this->assertTrue(Validator::containsAlphanumeric()->isValid('v4l1d')); } @@ -94,12 +88,12 @@ public function testSetTranslator(): void { // by default, error is in English $violations = $this->validator->validate(''); - $this->assertEquals('This value should not be blank.', $violations[0]->getMessage()); + $this->assertEquals('This value should not be blank.', $violations->get(0)->getMessage()); // set translator and then try again Validator::setTranslator(new Translator('pt')); // now error is in Portuguese $violations = $this->validator->validate(''); - $this->assertEquals('Este valor não deveria ser vazio.', $violations[0]->getMessage()); + $this->assertEquals('Este valor não deveria ser vazio.', $violations->get(0)->getMessage()); } } \ No newline at end of file