Skip to content

Commit

Permalink
Fix optional structures can be null.
Browse files Browse the repository at this point in the history
  • Loading branch information
briedis committed Mar 22, 2017
1 parent cb623ed commit edad841
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Briedis/ApiBuilder/StructureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions tests/MultiDepthValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}

0 comments on commit edad841

Please sign in to comment.