Skip to content

Commit

Permalink
Validator: fixed validating empty array as valid map value [closes #17]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 20, 2019
1 parent 0815267 commit 9ad9759
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function validate($data): Result
}
}
} elseif ($type === 'map') {
if ((is_array($node) || $node instanceof \stdClass) && !Helpers::isArray($node)) {
if ((is_array($node) || $node instanceof \stdClass) && ((is_array($node) && count($node) === 0) || !Helpers::isArray($node))) {
$isValid = $this->validateInnerProperties($node, $schema, $path, $stack, $errors);
if ($isValid) {
break;
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/validate.map.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ coercion:
type: map
properties:
live: bool
alloptional:
type: map
properties:
?type: string
NEON
);

Expand Down Expand Up @@ -175,3 +179,16 @@ Assert::same(true, $result->getData()['live']);
$result = $validator->validate((object) ['live' => '1']);
Assert::true($result->isValid());
Assert::same(true, $result->getData()->live);


// =====================================================================================================================


$validator = new Validator(prepareSchema($config['alloptional']));
$validator->coerceStringToBool = true;

$result = $validator->validate([]);
Assert::true($result->isValid());

$result = $validator->validate((object) []);
Assert::true($result->isValid());

0 comments on commit 9ad9759

Please sign in to comment.