Skip to content

Commit a32838f

Browse files
committed
Type: mergeDefaults() are disabled by default (BC break) [Closes #28, Closes #31]
1 parent 6235ecb commit a32838f

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
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: 5 additions & 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)
@@ -44,8 +44,12 @@ public function nullable(): self
4444
}
4545

4646

47+
/** @deprecated mergeDefaults is disabled by default */
4748
public function mergeDefaults(bool $state = true): self
4849
{
50+
if ($state === true) {
51+
trigger_error(__METHOD__ . '() is deprecated and will be removed in the next major version.', E_USER_DEPRECATED);
52+
}
4953
$this->merge = $state;
5054
return $this;
5155
}

tests/Schema/Expect.array.phpt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ test('without default value', function () {
3636
});
3737

3838

39-
test('not merging', function () {
39+
test('not merging default value', function () {
4040
$schema = Expect::array([
4141
'key1' => 'val1',
4242
'key2' => 'val2',
4343
'val3',
4444
'arr' => ['item'],
45-
])->mergeDefaults(false);
45+
]);
4646

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

@@ -53,13 +53,13 @@ test('not merging', function () {
5353
});
5454

5555

56-
test('merging', function () {
57-
$schema = Expect::array([
56+
test('merging default value', function () {
57+
$schema = @Expect::array([ // mergeDefaults() is deprecated
5858
'key1' => 'val1',
5959
'key2' => 'val2',
6060
'val3',
6161
'arr' => ['item'],
62-
]);
62+
])->mergeDefaults(true);
6363

6464
Assert::same([
6565
'key1' => 'val1',
@@ -131,12 +131,12 @@ test('merging', function () {
131131
});
132132

133133

134-
test('merging & other items validation', function () {
135-
$schema = Expect::array([
134+
test('merging default value & other items validation', function () {
135+
$schema = @Expect::array([ // mergeDefaults() is deprecated
136136
'key1' => 'val1',
137137
'key2' => 'val2',
138138
'val3',
139-
])->items('string');
139+
])->mergeDefaults(true)->items('string');
140140

141141
Assert::same([
142142
'key1' => 'val1',
@@ -169,7 +169,7 @@ test('merging & other items validation', function () {
169169
});
170170

171171

172-
test('merging & other items validation', function () {
172+
test('merging default value & other items validation', function () {
173173
$schema = Expect::array()->items('string');
174174

175175
Assert::same([
@@ -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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ test('without default value', function () {
3737
});
3838

3939

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

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

@@ -48,8 +48,8 @@ test('not merging', function () {
4848
});
4949

5050

51-
test('merging', function () {
52-
$schema = Expect::list([1, 2, 3]);
51+
test('merging default value', function () {
52+
$schema = @Expect::list([1, 2, 3])->mergeDefaults(true); // mergeDefaults() is deprecated
5353

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

@@ -59,8 +59,8 @@ test('merging', function () {
5959
});
6060

6161

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

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

0 commit comments

Comments
 (0)