Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -154,21 +154,21 @@ 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.

```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)
Expand All @@ -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);
```

Expand Down
1 change: 1 addition & 0 deletions src/Factory/ConstraintFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -86,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;
}
Expand Down
14 changes: 4 additions & 10 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -65,15 +63,13 @@ public function testAssertSuccess(): void

public function testIsValid(): void
{
// test fail
$this->assertFalse($this->validator->isValid(16));
// test success
$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]);
Expand All @@ -84,22 +80,20 @@ 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'));
}

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());
}
}