From 8da0617eca0f21498dd583f2113280612e39e179 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 29 Jul 2020 15:37:30 -0300 Subject: [PATCH 1/4] fix: not required field validating constraints when empty/null --- src/Node/BaseNode.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Node/BaseNode.php b/src/Node/BaseNode.php index 07413ad..580c50e 100644 --- a/src/Node/BaseNode.php +++ b/src/Node/BaseNode.php @@ -275,7 +275,7 @@ public function walk($input) protected function checkConstraints(string $field, $value): void { foreach ($this->constraints as $constraint) { - if (!$constraint->validate($value)) { + if (!$constraint->validate($value) && ($this->isRequired() || !empty($value))) { throw new InvalidConstraintException($constraint->getErrorMessage($field)); } } From a10c4eccb89c81bec3467585b882a310b28639c1 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 29 Jul 2020 15:37:53 -0300 Subject: [PATCH 2/4] tests: covering the not required field scenario --- tests/Node/BaseNodeTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Node/BaseNodeTest.php b/tests/Node/BaseNodeTest.php index 4c6080f..8d82231 100644 --- a/tests/Node/BaseNodeTest.php +++ b/tests/Node/BaseNodeTest.php @@ -129,4 +129,19 @@ public function testIsGettingTransformedValue(): void $child = $base->add('foobar', 'string', ['transformer' => new DateTimeTransformer()]); $this->assertEquals(new \DateTime('2014-01-01 00:00:00'), $child->getValue('foobar', '2014-01-01 00:00:00')); } + + public function testNotRequiredWithConstraints() + { + $typeHandler = $this->prophesize(TypeHandler::class); + $typeHandler->getType('string')->willReturn(new BaseNode()); + + $base = new BaseNode(); + $base->setTypeHandler($typeHandler->reveal()); + $child = $base->add('foobar', 'string') + ->setRequired(false) + ->addConstraint(new StringSize(1, 255)); + + $this->assertNull($child->getValue('foobar', null)); + $this->assertEmpty($child->getValue('foobar', '')); + } } From 1539069eb06800356c215f32203f0abc6818b9ed Mon Sep 17 00:00:00 2001 From: Gabriel Date: Wed, 29 Jul 2020 15:43:57 -0300 Subject: [PATCH 3/4] chore(fix): unit test method void return type --- tests/Node/BaseNodeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Node/BaseNodeTest.php b/tests/Node/BaseNodeTest.php index 8d82231..a34a732 100644 --- a/tests/Node/BaseNodeTest.php +++ b/tests/Node/BaseNodeTest.php @@ -130,7 +130,7 @@ public function testIsGettingTransformedValue(): void $this->assertEquals(new \DateTime('2014-01-01 00:00:00'), $child->getValue('foobar', '2014-01-01 00:00:00')); } - public function testNotRequiredWithConstraints() + public function testNotRequiredWithConstraints() : void { $typeHandler = $this->prophesize(TypeHandler::class); $typeHandler->getType('string')->willReturn(new BaseNode()); From 3f6d2db96e1e651a87b56be8cb69444dc47e10c5 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 30 Jul 2020 17:32:48 -0300 Subject: [PATCH 4/4] chore(fix): lint --- tests/Node/BaseNodeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Node/BaseNodeTest.php b/tests/Node/BaseNodeTest.php index a34a732..0e7887c 100644 --- a/tests/Node/BaseNodeTest.php +++ b/tests/Node/BaseNodeTest.php @@ -130,7 +130,7 @@ public function testIsGettingTransformedValue(): void $this->assertEquals(new \DateTime('2014-01-01 00:00:00'), $child->getValue('foobar', '2014-01-01 00:00:00')); } - public function testNotRequiredWithConstraints() : void + public function testNotRequiredWithConstraints(): void { $typeHandler = $this->prophesize(TypeHandler::class); $typeHandler->getType('string')->willReturn(new BaseNode());