From b8aff938e942601aea06e9a14be6d9cd3cd49839 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 | 2 +- tests/Schema/Expect.array.phpt | 22 +++++++++------------- tests/Schema/Expect.list.phpt | 6 +++--- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/readme.md b/readme.md index 629fefe..5fb2942 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 4094442..5289e11 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) diff --git a/tests/Schema/Expect.array.phpt b/tests/Schema/Expect.array.phpt index 243e284..b703e52 100644 --- a/tests/Schema/Expect.array.phpt +++ b/tests/Schema/Expect.array.phpt @@ -42,7 +42,7 @@ test('not merging', function () { 'key2' => 'val2', 'val3', 'arr' => ['item'], - ])->mergeDefaults(false); + ]); Assert::same([], (new Processor)->process($schema, [])); @@ -59,7 +59,7 @@ test('merging', function () { 'key2' => 'val2', 'val3', 'arr' => ['item'], - ]); + ])->mergeDefaults(true); Assert::same([ 'key1' => 'val1', @@ -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', @@ -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 5fde938..d922a38 100644 --- a/tests/Schema/Expect.list.phpt +++ b/tests/Schema/Expect.list.phpt @@ -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, [])); @@ -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, [])); @@ -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, []));