From a1859ed33ffd1504d352e033323fcb48ff1f37eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Mon, 17 Aug 2020 20:22:10 +0200 Subject: [PATCH] AnyOf: call normalize --- src/Schema/Elements/AnyOf.php | 1 + tests/Schema/Expect.anyOf.phpt | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Schema/Elements/AnyOf.php b/src/Schema/Elements/AnyOf.php index 81e260d..f343b67 100644 --- a/src/Schema/Elements/AnyOf.php +++ b/src/Schema/Elements/AnyOf.php @@ -73,6 +73,7 @@ public function complete($value, Nette\Schema\Context $context) if ($item instanceof Schema) { $dolly = new Context; $dolly->path = $context->path; + $value = $item->normalize($value, $dolly); $res = $item->complete($value, $dolly); if (!$dolly->errors) { return $this->doFinalize($res, $context); diff --git a/tests/Schema/Expect.anyOf.phpt b/tests/Schema/Expect.anyOf.phpt index 379e1d8..48ec0a0 100644 --- a/tests/Schema/Expect.anyOf.phpt +++ b/tests/Schema/Expect.anyOf.phpt @@ -200,3 +200,24 @@ test('Schema as default value', function () { Assert::same(['key1' => ['key2' => null]], (new Processor)->process($schema, null)); }); + + +test('normalizing', function () { + $schema = Expect::anyOf( + false, + Expect::array() + ->before(function ($val) { + if (!is_string($val)) return $val; + return explode(',', $val); + }) + ); + + $processor = new Processor; + + Assert::same(false, $processor->process($schema, false)); + Assert::same(['1','2','3'], $processor->process($schema, '1,2,3')); + + checkValidationErrors(function () use ($schema) { + (new Processor)->process($schema, 1); + }, ['The option expects to be false|array, 1 given.']); +});