Skip to content

Commit

Permalink
Fix empty array validation
Browse files Browse the repository at this point in the history
  • Loading branch information
briedis committed Apr 27, 2015
1 parent c53e6a2 commit 930df6c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Briedis/ApiBuilder/StructureValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function validate(array $input){

// Check for missing fields
foreach($this->structure->getItems() as $k => $v){
if(!array_key_exists($k, $input)){
if(!$v->isOptional && !array_key_exists($k, $input)){
$exception->addMissingField($k, $v);
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/SingleDepthValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ public function testUnexpectedParameter(){

$this->assertTrue($caught, 'Exception is caught');
}

public function testOptionalParameterMissing(){
$this->s->int('missing')->optional();
$input = [];
$this->assertTrue($this->v->validate($input), 'Optional parameter can be omitted');
}

public function testEmptyArrayIsValid(){
$this->s->int('numbers')->multiple();
$input = [
'numbers' => [],
];
$this->assertTrue($this->v->validate($input), 'Empty array is a valid type');
}
}

0 comments on commit 930df6c

Please sign in to comment.