Skip to content
Permalink
Browse files

Structure: is normalized to array in normalize() [Closes #9]

  • Loading branch information...
dg committed Sep 15, 2019
1 parent fcfe93a commit 5e3034206454c89f0def6d1345945062bc3e7c02
Showing with 12 additions and 2 deletions.
  1. +3 −2 src/Schema/Elements/Structure.php
  2. +9 −0 tests/Schema/Expect.structure.phpt
@@ -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));
@@ -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']])
);
});

0 comments on commit 5e30342

Please sign in to comment.
You can’t perform that action at this time.