Skip to content

Commit 69db7e8

Browse files
committed
Add option to prevent merging of arrayOf, listOf defaults
1 parent 7a5fc30 commit 69db7e8

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Schema/Elements/Type.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ final class Type implements Schema
3333
/** @var string|null */
3434
private $pattern;
3535

36+
/** @var bool */
37+
private $preventMergingDefaults = false;
38+
3639

3740
public function __construct(string $type)
3841
{
@@ -88,6 +91,13 @@ public function pattern(?string $pattern): self
8891
}
8992

9093

94+
public function preventMergingDefaults(bool $prevent = true): self
95+
{
96+
$this->preventMergingDefaults = $prevent;
97+
return $this;
98+
}
99+
100+
91101
/********************* processing ****************d*g**/
92102

93103

@@ -165,6 +175,10 @@ public function complete($value, Context $context)
165175
}
166176
}
167177

178+
if ($this->preventMergingDefaults && is_array($value) && count($value) > 0) {
179+
$value[Helpers::PREVENT_MERGING] = true;
180+
}
181+
168182
$value = Helpers::merge($value, $this->default);
169183
return $this->doFinalize($value, $context);
170184
}

tests/Schema/Expect.array.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ test('arrayOf() & scalar', function () {
231231
});
232232

233233

234+
test('arrayOf() with defaults', function () {
235+
$schema = Expect::arrayOf('string|int')->default(['foo', 42]);
236+
237+
Assert::same(['foo', 42], (new Processor)->process($schema, null));
238+
Assert::same(['foo', 42], (new Processor)->process($schema, []));
239+
Assert::same(['foo', 42, 'bar'], (new Processor)->process($schema, ['bar']));
240+
241+
$schema->preventMergingDefaults();
242+
243+
Assert::same(['foo', 42], (new Processor)->process($schema, null));
244+
Assert::same(['foo', 42], (new Processor)->process($schema, []));
245+
Assert::same(['bar'], (new Processor)->process($schema, ['bar']));
246+
});
247+
248+
234249
test('arrayOf() error', function () {
235250
Assert::exception(function () {
236251
Expect::arrayOf(['a' => Expect::string()]);

tests/Schema/Expect.list.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ test('listOf() & scalar', function () {
8888
});
8989

9090

91+
test('listOf() with defaults', function () {
92+
$schema = Expect::listOf('string|int')->default(['foo', 42]);
93+
94+
Assert::same(['foo', 42], (new Processor)->process($schema, null));
95+
Assert::same(['foo', 42], (new Processor)->process($schema, []));
96+
Assert::same(['foo', 42, 'bar'], (new Processor)->process($schema, ['bar']));
97+
98+
$schema->preventMergingDefaults();
99+
100+
Assert::same(['foo', 42], (new Processor)->process($schema, null));
101+
Assert::same(['foo', 42], (new Processor)->process($schema, []));
102+
Assert::same(['bar'], (new Processor)->process($schema, ['bar']));
103+
});
104+
105+
91106
test('listOf() & error', function () {
92107
Assert::exception(function () {
93108
Expect::listOf(['a' => Expect::string()]);

0 commit comments

Comments
 (0)