Skip to content

Commit 72b5fc4

Browse files
committed
Type: mergeDefaults() are disabled by default (BC BREAK) [Closes #28, Closes #31]
1 parent 64f3117 commit 72b5fc4

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ The parameter can also be a schema, so we can write:
177177
Expect::arrayOf(Expect::bool())
178178
```
179179

180-
The default value is an empty array. If you specify a default value, it will be merged with the passed data. This can be disabled using `mergeDefaults(false)`.
180+
The default value is an empty array. If you specify a default value and call `mergeDefaults()`, it will be merged with the passed data.
181181

182182

183183
Enumeration: anyOf()

src/Schema/Elements/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Type implements Schema
2626
/** @var array{?float, ?float} */
2727
private array $range = [null, null];
2828
private ?string $pattern = null;
29-
private bool $merge = true;
29+
private bool $merge = false;
3030

3131

3232
public function __construct(string $type)

tests/Schema/Expect.array.phpt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test('not merging', function () {
4242
'key2' => 'val2',
4343
'val3',
4444
'arr' => ['item'],
45-
])->mergeDefaults(false);
45+
]);
4646

4747
Assert::same([], (new Processor)->process($schema, []));
4848

@@ -59,7 +59,7 @@ test('merging', function () {
5959
'key2' => 'val2',
6060
'val3',
6161
'arr' => ['item'],
62-
]);
62+
])->mergeDefaults(true);
6363

6464
Assert::same([
6565
'key1' => 'val1',
@@ -136,7 +136,7 @@ test('merging & other items validation', function () {
136136
'key1' => 'val1',
137137
'key2' => 'val2',
138138
'val3',
139-
])->items('string');
139+
])->mergeDefaults(true)->items('string');
140140

141141
Assert::same([
142142
'key1' => 'val1',
@@ -204,11 +204,9 @@ test('merging & other items validation', function () {
204204

205205

206206
test('items() & scalar', function () {
207-
$schema = Expect::array([
208-
'a' => 'defval',
209-
])->items('string');
207+
$schema = Expect::array()->items('string');
210208

211-
Assert::same(['a' => 'defval'], (new Processor)->process($schema, []));
209+
Assert::same([], (new Processor)->process($schema, []));
212210

213211
checkValidationErrors(function () use ($schema) {
214212
(new Processor)->process($schema, [1, 2, 3]);
@@ -232,16 +230,14 @@ test('items() & scalar', function () {
232230
(new Processor)->process($schema, ['b' => null]);
233231
}, ["The item 'b' expects to be string, null given."]);
234232

235-
Assert::same(['a' => 'defval', 'b' => 'val'], (new Processor)->process($schema, ['b' => 'val']));
233+
Assert::same(['b' => 'val'], (new Processor)->process($schema, ['b' => 'val']));
236234
});
237235

238236

239237
test('items() & structure', function () {
240-
$schema = Expect::array([
241-
'a' => 'defval',
242-
])->items(Expect::structure(['k' => Expect::string()]));
238+
$schema = Expect::array([])->items(Expect::structure(['k' => Expect::string()]));
243239

244-
Assert::same(['a' => 'defval'], (new Processor)->process($schema, []));
240+
Assert::same([], (new Processor)->process($schema, []));
245241

246242
checkValidationErrors(function () use ($schema) {
247243
(new Processor)->process($schema, ['a' => 'val']);
@@ -264,7 +260,7 @@ test('items() & structure', function () {
264260
}, ["Unexpected item 'b\u{a0}\u{a0}a', did you mean 'k'?"]);
265261

266262
Assert::equal(
267-
['a' => 'defval', 'b' => (object) ['k' => 'val']],
263+
['b' => (object) ['k' => 'val']],
268264
(new Processor)->process($schema, ['b' => ['k' => 'val']]),
269265
);
270266
});

tests/Schema/Expect.list.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('without default value', function () {
3838

3939

4040
test('not merging', function () {
41-
$schema = Expect::list([1, 2, 3])->mergeDefaults(false);
41+
$schema = Expect::list([1, 2, 3]);
4242

4343
Assert::same([], (new Processor)->process($schema, []));
4444

@@ -49,7 +49,7 @@ test('not merging', function () {
4949

5050

5151
test('merging', function () {
52-
$schema = Expect::list([1, 2, 3]);
52+
$schema = Expect::list([1, 2, 3])->mergeDefaults(true);
5353

5454
Assert::same([1, 2, 3], (new Processor)->process($schema, []));
5555

@@ -60,7 +60,7 @@ test('merging', function () {
6060

6161

6262
test('merging & other items validation', function () {
63-
$schema = Expect::list([1, 2, 3])->items('string');
63+
$schema = Expect::list([1, 2, 3])->mergeDefaults(true)->items('string');
6464

6565
Assert::same([1, 2, 3], (new Processor)->process($schema, []));
6666

0 commit comments

Comments
 (0)