Skip to content
Open
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
12 changes: 9 additions & 3 deletions packages/validation/src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ public function validateObject(object $object): void

$value = $property->getValue($object);

$failingRules[$property->getName()] = $this->validateValueForProperty($property, $value);
$failingRulesForProperty = $this->validateValueForProperty($property, $value);

if ($failingRulesForProperty !== []) {
$failingRules[$property->getName()] = $failingRulesForProperty;
}
}

if ($failingRules !== []) {
throw $this->createValidationFailureException($failingRules, $object);
throw $this->createValidationFailureException($failingRules, $object, $object::class);
}
}

Expand Down Expand Up @@ -153,9 +157,11 @@ public function validateValueForProperty(PropertyReflector $property, mixed $val

$key = $property->getAttribute(TranslationKey::class)?->key;

$field = $property->getName();

return Arr\map(
array: $this->validateValue($value, $rules),
map: fn (FailingRule $rule) => $rule->withKey($key),
map: fn (FailingRule $rule) => $rule->withField($field)->withKey($key),
);
}

Expand Down
31 changes: 29 additions & 2 deletions packages/validation/tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Tempest\Reflection\ClassReflector;
use Tempest\Validation\Exceptions\ValidationFailed;
use Tempest\Validation\HasErrorMessage;
use Tempest\Validation\Rules\HasLength;
use Tempest\Validation\Rules\IsBoolean;
use Tempest\Validation\Rules\IsEmail;
use Tempest\Validation\Rules\IsEnum;
Expand Down Expand Up @@ -37,9 +38,35 @@ final class ValidatorTest extends TestCase

public function test_validate(): void
{
$this->expectException(ValidationFailed::class);
try {
$this->validator->validateObject(new ObjectToBeValidated(name: 'a'));
$this->fail('Expected ValidationFailed to be thrown.');
} catch (ValidationFailed $e) {
$this->assertArrayHasKey('name', $e->failingRules);
$this->assertCount(1, $e->failingRules['name']);
$this->assertInstanceOf(HasLength::class, $e->failingRules['name'][0]->rule);
$this->assertSame('name', $e->failingRules['name'][0]->field);
$this->assertSame('a', $e->failingRules['name'][0]->value);
$this->assertSame(ObjectToBeValidated::class, $e->targetClass);
}
}

public function test_validate_passes_with_valid_object(): void
{
$this->validator->validateObject(new ObjectToBeValidated(name: 'ab'));

$this->expectNotToPerformAssertions();
}

public function test_validate_value_for_property_sets_field_and_value(): void
{
$property = (new ClassReflector(ValidateObjectA::class))->getProperty('title');

$this->validator->validateObject(new ObjectToBeValidated(name: 'a'));
$failingRules = $this->validator->validateValueForProperty($property, 123);

$this->assertCount(1, $failingRules);
$this->assertSame('title', $failingRules[0]->field);
$this->assertSame(123, $failingRules[0]->value);
}

public function test_validate_value(): void
Expand Down