Skip to content

Commit 9ad9759

Browse files
committed
Validator: fixed validating empty array as valid map value [closes #17]
1 parent 0815267 commit 9ad9759

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function validate($data): Result
241241
}
242242
}
243243
} elseif ($type === 'map') {
244-
if ((is_array($node) || $node instanceof \stdClass) && !Helpers::isArray($node)) {
244+
if ((is_array($node) || $node instanceof \stdClass) && ((is_array($node) && count($node) === 0) || !Helpers::isArray($node))) {
245245
$isValid = $this->validateInnerProperties($node, $schema, $path, $stack, $errors);
246246
if ($isValid) {
247247
break;

tests/cases/validate.map.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ coercion:
4141
type: map
4242
properties:
4343
live: bool
44+
alloptional:
45+
type: map
46+
properties:
47+
?type: string
4448
NEON
4549
);
4650

@@ -175,3 +179,16 @@ Assert::same(true, $result->getData()['live']);
175179
$result = $validator->validate((object) ['live' => '1']);
176180
Assert::true($result->isValid());
177181
Assert::same(true, $result->getData()->live);
182+
183+
184+
// =====================================================================================================================
185+
186+
187+
$validator = new Validator(prepareSchema($config['alloptional']));
188+
$validator->coerceStringToBool = true;
189+
190+
$result = $validator->validate([]);
191+
Assert::true($result->isValid());
192+
193+
$result = $validator->validate((object) []);
194+
Assert::true($result->isValid());

0 commit comments

Comments
 (0)