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 1/2] 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 From d9a7fec7566af835957e42fb0f4e3f5bd60ce800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20Pimpa=CC=83o?= Date: Tue, 1 Apr 2025 16:05:46 +0100 Subject: [PATCH 2/2] chore: rename getConstraints to toArray --- README.md | 10 +++++----- src/Validator.php | 2 +- tests/ValidatorTest.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9e93824..cb154aa 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ offering an easy-to-use and intuitive API to validate user input or other data i - [validate](#validate) - [assert](#assert) - [isValid](#isvalid) - - [getConstraints](#getconstraints) + - [toArray](#toarray) - [addNamespace](#addnamespace) - [setTranslator](#settranslator) - [Custom Constraints](#custom-constraints) @@ -154,13 +154,13 @@ if (!Validator::email()->isValid($email)) { } ``` -### `getConstraints` +### `toArray` ```php use Symfony\Component\Validator\Constraint; /** @return Constraint[] */ -getConstraints(): array +toArray(): array ``` Returns an array with all added constraints. @@ -168,7 +168,7 @@ Returns an array with all added constraints. ```php use ProgrammatorDev\FluentValidator\Validator; -$constraints = Validator::notBlank()->email()->getConstraints(); +$constraints = Validator::notBlank()->email()->toArray(); ``` It is useful for `Composite` constraints (i.e., a constraint that is composed of other constraints) @@ -180,7 +180,7 @@ use ProgrammatorDev\FluentValidator\Validator; // validate that array should have at least one value // and each value should be between 0 and 100 $errors = Validator::count(min: 1) - ->all(Validator::range(min: 0, max: 100)->getConstraints()) + ->all(Validator::range(min: 0, max: 100)->toArray()) ->validate($value); ``` diff --git a/src/Validator.php b/src/Validator.php index 72031d8..a108405 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -87,7 +87,7 @@ public function isValid(mixed $value, string|GroupSequence|array|null $groups = return $violations->count() === 0; } - public function getConstraints(): array + public function toArray(): array { return $this->constraints; } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 8488d61..80b14c2 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -67,9 +67,9 @@ public function testIsValid(): void $this->assertTrue($this->validator->isValid(18)); } - public function testGetConstraints(): void + public function testToArray(): void { - $constraints = $this->validator->getConstraints(); + $constraints = $this->validator->toArray(); $this->assertInstanceOf(NotBlank::class, $constraints[0]); $this->assertInstanceOf(GreaterThanOrEqual::class, $constraints[1]);