From edad8410c6efad5248464456d29262e005c970cc Mon Sep 17 00:00:00 2001 From: "martins.briedis" Date: Wed, 22 Mar 2017 21:42:58 +0200 Subject: [PATCH] Fix optional structures can be null. --- src/Briedis/ApiBuilder/StructureValidator.php | 4 ++++ tests/MultiDepthValidatorTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Briedis/ApiBuilder/StructureValidator.php b/src/Briedis/ApiBuilder/StructureValidator.php index 24c84c6..7e05568 100644 --- a/src/Briedis/ApiBuilder/StructureValidator.php +++ b/src/Briedis/ApiBuilder/StructureValidator.php @@ -87,6 +87,10 @@ private function validateParam($name, $value) } if ($item instanceof StructureItem) { + if ($item->isOptional && is_null($value)) { + return; + } + if ($item->isArray) { $this->validateArrayOfStructures($item, $value); return; diff --git a/tests/MultiDepthValidatorTest.php b/tests/MultiDepthValidatorTest.php index b9cc61b..4ec7c9f 100644 --- a/tests/MultiDepthValidatorTest.php +++ b/tests/MultiDepthValidatorTest.php @@ -245,4 +245,15 @@ public function testStructureOfArrayReceivesInvalidStructure() self::assertFalse(true, 'This should not execute'); } + + public function testOptionalStructuresCanBeNull() + { + $item = (new StructureBuilder('MyStructure')); + + $whole = (new SB)->struct('item', $item)->optional(); + + $structureValidator = new StructureValidator($whole); + + self::assertTrue($structureValidator->validate(['item' => null])); + } } \ No newline at end of file