Skip to content

Commit eb3a5c3

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

4 files changed

Lines changed: 27 additions & 30 deletions

File tree

readme.md

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

179-
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)`.
179+
The default value is an empty array. If you specify a default value and call `mergeDefaults()`, it will be merged with the passed data.
180180

181181

182182
Enumeration: anyOf()

src/Schema/Elements/Type.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class Type implements Schema
2525
/** @var array{?float, ?float} */
2626
private array $range = [null, null];
2727
private ?string $pattern = null;
28-
private bool $merge = true;
28+
private bool $merge = false;
2929

3030

3131
public function __construct(string $type)
@@ -46,11 +46,12 @@ public function nullable(): self
4646
}
4747

4848

49-
/**
50-
* Controls whether the default value is merged with the input array (enabled by default).
51-
*/
49+
/** @deprecated mergeDefaults is disabled by default */
5250
public function mergeDefaults(bool $state = true): self
5351
{
52+
if ($state === true) {
53+
trigger_error(__METHOD__ . '() is deprecated and will be removed in the next major version.', E_USER_DEPRECATED);
54+
}
5455
$this->merge = $state;
5556
return $this;
5657
}

tests/Schema/Expect.array.phpt

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

3636

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

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

@@ -51,13 +51,13 @@ test('not merging', function () {
5151
});
5252

5353

54-
test('merging', function () {
55-
$schema = Expect::array([
54+
test('merging default value', function () {
55+
$schema = @Expect::array([ // mergeDefaults() is deprecated
5656
'key1' => 'val1',
5757
'key2' => 'val2',
5858
'val3',
5959
'arr' => ['item'],
60-
]);
60+
])->mergeDefaults(true);
6161

6262
Assert::same([
6363
'key1' => 'val1',
@@ -129,12 +129,12 @@ test('merging', function () {
129129
});
130130

131131

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

139139
Assert::same([
140140
'key1' => 'val1',
@@ -167,7 +167,7 @@ test('merging & other items validation', function () {
167167
});
168168

169169

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

173173
Assert::same([
@@ -202,11 +202,9 @@ test('merging & other items validation', function () {
202202

203203

204204
test('items() & scalar', function () {
205-
$schema = Expect::array([
206-
'a' => 'defval',
207-
])->items('string');
205+
$schema = Expect::array()->items('string');
208206

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

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

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

236234

237235
test('items() & structure', function () {
238-
$schema = Expect::array([
239-
'a' => 'defval',
240-
])->items(Expect::structure(['k' => Expect::string()]));
236+
$schema = Expect::array([])->items(Expect::structure(['k' => Expect::string()]));
241237

242-
Assert::same(['a' => 'defval'], (new Processor)->process($schema, []));
238+
Assert::same([], (new Processor)->process($schema, []));
243239

244240
checkValidationErrors(function () use ($schema) {
245241
(new Processor)->process($schema, ['a' => 'val']);
@@ -262,7 +258,7 @@ test('items() & structure', function () {
262258
}, ["Unexpected item 'b\u{a0}\u{a0}a', did you mean 'k'?"]);
263259

264260
Assert::equal(
265-
['a' => 'defval', 'b' => (object) ['k' => 'val']],
261+
['b' => (object) ['k' => 'val']],
266262
(new Processor)->process($schema, ['b' => ['k' => 'val']]),
267263
);
268264
});

tests/Schema/Expect.list.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ test('without default value', function () {
3535
});
3636

3737

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

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

@@ -46,8 +46,8 @@ test('not merging', function () {
4646
});
4747

4848

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

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

@@ -57,8 +57,8 @@ test('merging', function () {
5757
});
5858

5959

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

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

0 commit comments

Comments
 (0)