From d01ecd5751f1ce6dd866844e13183e5e034b0ebb Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 20 Dec 2020 22:52:11 +0100 Subject: [PATCH] Type: mergeDefaults() are disabled by default (BC break) [Closes #28, Closes #31] --- readme.md | 2 +- src/Schema/Elements/Type.php | 6 +++++- tests/Schema/Expect.array.phpt | 34 +++++++++++++++------------------- tests/Schema/Expect.list.phpt | 12 ++++++------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/readme.md b/readme.md index e5296c0..b9febfa 100644 --- a/readme.md +++ b/readme.md @@ -177,7 +177,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 a 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 a default value and call `mergeDefaults()`, it will be merged with the passed data. Enumeration: anyOf() diff --git a/src/Schema/Elements/Type.php b/src/Schema/Elements/Type.php index 69d5299..7f5d68d 100644 --- a/src/Schema/Elements/Type.php +++ b/src/Schema/Elements/Type.php @@ -26,7 +26,7 @@ final class Type implements Schema /** @var array{?float, ?float} */ private array $range = [null, null]; private ?string $pattern = null; - private bool $merge = true; + private bool $merge = false; public function __construct(string $type) @@ -44,8 +44,12 @@ public function nullable(): self } + /** @deprecated mergeDefaults is disabled by default */ public function mergeDefaults(bool $state = true): self { + if ($state === true) { + trigger_error(__METHOD__ . '() is deprecated and will be removed in the next major version.', E_USER_DEPRECATED); + } $this->merge = $state; return $this; } diff --git a/tests/Schema/Expect.array.phpt b/tests/Schema/Expect.array.phpt index 5979da5..dc03625 100644 --- a/tests/Schema/Expect.array.phpt +++ b/tests/Schema/Expect.array.phpt @@ -36,13 +36,13 @@ test('without default value', function () { }); -test('not merging', function () { +test('not merging default value', function () { $schema = Expect::array([ 'key1' => 'val1', 'key2' => 'val2', 'val3', 'arr' => ['item'], - ])->mergeDefaults(false); + ]); Assert::same([], (new Processor)->process($schema, [])); @@ -53,13 +53,13 @@ test('not merging', function () { }); -test('merging', function () { - $schema = Expect::array([ +test('merging default value', function () { + $schema = @Expect::array([ // mergeDefaults() is deprecated 'key1' => 'val1', 'key2' => 'val2', 'val3', 'arr' => ['item'], - ]); + ])->mergeDefaults(true); Assert::same([ 'key1' => 'val1', @@ -131,12 +131,12 @@ test('merging', function () { }); -test('merging & other items validation', function () { - $schema = Expect::array([ +test('merging default value & other items validation', function () { + $schema = @Expect::array([ // mergeDefaults() is deprecated 'key1' => 'val1', 'key2' => 'val2', 'val3', - ])->items('string'); + ])->mergeDefaults(true)->items('string'); Assert::same([ 'key1' => 'val1', @@ -169,7 +169,7 @@ test('merging & other items validation', function () { }); -test('merging & other items validation', function () { +test('merging default value & other items validation', function () { $schema = Expect::array()->items('string'); Assert::same([ @@ -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]); @@ -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']); @@ -264,7 +260,7 @@ test('items() & structure', function () { }, ["Unexpected item 'b\u{a0}›\u{a0}a', did you mean 'k'?"]); Assert::equal( - ['a' => 'defval', 'b' => (object) ['k' => 'val']], + ['b' => (object) ['k' => 'val']], (new Processor)->process($schema, ['b' => ['k' => 'val']]), ); }); diff --git a/tests/Schema/Expect.list.phpt b/tests/Schema/Expect.list.phpt index 52a3e53..f565b53 100644 --- a/tests/Schema/Expect.list.phpt +++ b/tests/Schema/Expect.list.phpt @@ -37,8 +37,8 @@ test('without default value', function () { }); -test('not merging', function () { - $schema = Expect::list([1, 2, 3])->mergeDefaults(false); +test('not merging default value', function () { + $schema = Expect::list([1, 2, 3]); Assert::same([], (new Processor)->process($schema, [])); @@ -48,8 +48,8 @@ test('not merging', function () { }); -test('merging', function () { - $schema = Expect::list([1, 2, 3]); +test('merging default value', function () { + $schema = @Expect::list([1, 2, 3])->mergeDefaults(true); // mergeDefaults() is deprecated Assert::same([1, 2, 3], (new Processor)->process($schema, [])); @@ -59,8 +59,8 @@ test('merging', function () { }); -test('merging & other items validation', function () { - $schema = Expect::list([1, 2, 3])->items('string'); +test('merging default value & other items validation', function () { + $schema = @Expect::list([1, 2, 3])->mergeDefaults(true)->items('string'); // mergeDefaults() is deprecated Assert::same([1, 2, 3], (new Processor)->process($schema, []));