Skip to content

Commit

Permalink
Structure: is normalized to array in normalize() [Closes #9]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 15, 2019
1 parent d6d9dc0 commit fbf4951
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Schema/Elements/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function otherItems($type = 'mixed'): self
public function normalize($value, Context $context)
{
$value = $this->doNormalize($value, $context);
if (is_object($value)) {
$value = (array) $value;
}
if (is_array($value)) {
foreach ($value as $key => $val) {
$itemSchema = $this->items[$key] ?? $this->otherItems;
Expand Down Expand Up @@ -124,8 +127,6 @@ public function complete($value, Nette\Schema\Context $context)
{
if ($value === null) {
$value = []; // is unable to distinguish null from array in NEON
} elseif (is_object($value)) {
$value = (array) $value;
}

$expected = 'array' . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
Expand Down
9 changes: 9 additions & 0 deletions tests/Schema/Expect.structure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ test(function () { // accepts object
checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, (object) ['a' => 1]);
}, ["The option 'a' expects to be string, int 1 given."]);

$schema = Expect::structure(['a' => Expect::string()->before('strrev')]);

Assert::equal((object) ['a' => 'oof'], (new Processor)->process($schema, (object) ['a' => 'foo']));

Assert::equal(
(object) ['a' => 'rab'],
(new Processor)->processMultiple($schema, [(object) ['a' => 'foo'], (object) ['a' => 'bar']])
);
});


Expand Down

0 comments on commit fbf4951

Please sign in to comment.