diff --git a/src/Schema/Elements/Structure.php b/src/Schema/Elements/Structure.php index 25dc3b9..6eede02 100644 --- a/src/Schema/Elements/Structure.php +++ b/src/Schema/Elements/Structure.php @@ -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; @@ -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)); diff --git a/tests/Schema/Expect.structure.phpt b/tests/Schema/Expect.structure.phpt index ade898a..088d248 100644 --- a/tests/Schema/Expect.structure.phpt +++ b/tests/Schema/Expect.structure.phpt @@ -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']]) + ); });