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