Skip to content

Commit

Permalink
Type: mergeDefaults() are disabled by default (BC BREAK) [Closes #28, C…
Browse files Browse the repository at this point in the history
…loses #31]
  • Loading branch information
dg committed Sep 12, 2021
1 parent 1ce3601 commit 393abfb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -169,7 +169,7 @@ The parameter can also be a schema, so we can write:
Expect::arrayOf(Expect::bool())
```

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


Enumeration: anyOf()
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Elements/Type.php
Expand Up @@ -37,7 +37,7 @@ final class Type implements Schema
private $pattern;

/** @var bool */
private $merge = true;
private $merge = false;


public function __construct(string $type)
Expand Down
22 changes: 9 additions & 13 deletions tests/Schema/Expect.array.phpt
Expand Up @@ -42,7 +42,7 @@ test('not merging', function () {
'key2' => 'val2',
'val3',
'arr' => ['item'],
])->mergeDefaults(false);
]);

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

Expand All @@ -59,7 +59,7 @@ test('merging', function () {
'key2' => 'val2',
'val3',
'arr' => ['item'],
]);
])->mergeDefaults(true);

Assert::same([
'key1' => 'val1',
Expand Down Expand Up @@ -136,7 +136,7 @@ test('merging & other items validation', function () {
'key1' => 'val1',
'key2' => 'val2',
'val3',
])->items('string');
])->mergeDefaults(true)->items('string');

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


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

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

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

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


test('items() & structure', function () {
$schema = Expect::array([
'a' => 'defval',
])->items(Expect::structure(['k' => Expect::string()]));
$schema = Expect::array([])->items(Expect::structure(['k' => Expect::string()]));

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

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

Assert::equal(
['a' => 'defval', 'b' => (object) ['k' => 'val']],
['b' => (object) ['k' => 'val']],
(new Processor)->process($schema, ['b' => ['k' => 'val']])
);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/Schema/Expect.list.phpt
Expand Up @@ -38,7 +38,7 @@ test('without default value', function () {


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

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

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


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

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

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


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

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

Expand Down

0 comments on commit 393abfb

Please sign in to comment.