From 1b1689bdeaa4f0af9af1444bbba446eccd4359a6 Mon Sep 17 00:00:00 2001 From: Alexandre Gaigalas Date: Fri, 16 Jan 2015 02:34:15 -0200 Subject: [PATCH] AbstractRelated is accepts '' as valid by default --- library/Rules/AbstractRelated.php | 3 ++- tests/library/Respect/Validation/Rules/KeyTest.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/library/Rules/AbstractRelated.php b/library/Rules/AbstractRelated.php index 0eb776d3d..9f9472b91 100644 --- a/library/Rules/AbstractRelated.php +++ b/library/Rules/AbstractRelated.php @@ -66,10 +66,11 @@ public function check($input) public function validate($input) { $hasReference = $this->hasReference($input); + if ($this->mandatory && !$hasReference) { return false; } - return $this->decision('validate', $hasReference, $input); + return $this->decision('validate', $hasReference, $input) || $this->getReferenceValue($input) === ''; } } diff --git a/tests/library/Respect/Validation/Rules/KeyTest.php b/tests/library/Respect/Validation/Rules/KeyTest.php index 001b787ed..0d07b90e2 100644 --- a/tests/library/Respect/Validation/Rules/KeyTest.php +++ b/tests/library/Respect/Validation/Rules/KeyTest.php @@ -13,6 +13,16 @@ public function testArrayWithPresentKeyShouldReturnTrue() $this->assertTrue($validator->validate($obj)); } + public function testArrayWithEmptyKeyShouldReturnTrue() + { + $validator = new Key('someEmptyKey'); + $obj = array(); + $obj['someEmptyKey'] = ''; + $this->assertTrue($validator->assert($obj)); + $this->assertTrue($validator->check($obj)); + $this->assertTrue($validator->validate($obj)); + } + /** * @expectedException Respect\Validation\Exceptions\KeyException */