Skip to content

Commit

Permalink
Type: PREVENT_MERGING prevents merging with defaults [Closes #14, Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 18, 2020
1 parent 0ec8123 commit 87ed17a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Schema/Elements/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function normalize($value, Context $context)
}
if (is_array($value)) {
foreach ($value as $key => $val) {
if ($key === Helpers::PREVENT_MERGING) {
continue;
}
$itemSchema = $this->items[$key] ?? $this->otherItems;
if ($itemSchema) {
$context->path[] = $key;
Expand Down
11 changes: 10 additions & 1 deletion src/Schema/Elements/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public function normalize($value, Context $context)
$value = $this->doNormalize($value, $context);
if (is_array($value) && $this->items) {
foreach ($value as $key => $val) {
if ($key === Helpers::PREVENT_MERGING) {
continue;
}
$context->path[] = $key;
$value[$key] = $this->items->normalize($val, $context);
array_pop($context->path);
Expand Down Expand Up @@ -142,6 +145,12 @@ public function merge($value, $base)

public function complete($value, Context $context)
{
$merge = $this->merge;
if (is_array($value) && isset($value[Helpers::PREVENT_MERGING])) {
unset($value[Helpers::PREVENT_MERGING]);
$merge = false;
}

if ($value === null && is_array($this->default)) {
$value = []; // is unable to distinguish null from array in NEON
}
Expand Down Expand Up @@ -180,7 +189,7 @@ public function complete($value, Context $context)
}
}

if ($this->merge) {
if ($merge) {
$value = Helpers::merge($value, $this->default);
}
return $this->doFinalize($value, $context);
Expand Down
37 changes: 36 additions & 1 deletion tests/Schema/Expect.array.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Nette\Schema\Expect;
use Nette\Schema\Helpers;
use Nette\Schema\Processor;
use Tester\Assert;

Expand Down Expand Up @@ -90,7 +91,41 @@ test('merging', function () {
(new Processor)->process($schema, [
'key1' => 'newval',
'key3' => 'newval',
'newval3', 'arr' => ['newitem'],
'newval3',
'arr' => ['newitem'],
])
);

Assert::same(
[
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => ['newitem'],
],
(new Processor)->process($schema, [
Helpers::PREVENT_MERGING => true,
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => ['newitem'],
])
);

Assert::same(
[
'key1' => 'newval',
'key2' => 'val2',
'val3',
'arr' => ['newitem'],
'key3' => 'newval',
'newval3',
],
(new Processor)->process($schema, [
'key1' => 'newval',
'key3' => 'newval',
'newval3',
'arr' => [Helpers::PREVENT_MERGING => true, 'newitem'],
])
);
});
Expand Down

0 comments on commit 87ed17a

Please sign in to comment.