From 423e8caffd38ebcc0b5ecab8b9214914579c097b Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Wed, 22 Nov 2023 19:06:20 +0000 Subject: [PATCH] feat: allow command and query validation rules to return null In this instance, null indicates no errors. --- CHANGELOG.md | 4 ++++ src/Bus/Validation/AbstractValidator.php | 7 +++++-- tests/Unit/Bus/Validation/CommandValidatorTest.php | 9 +++++++-- tests/Unit/Bus/Validation/QueryValidatorTest.php | 9 +++++++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d21cfd7..4a2d4f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. This projec ## Unreleased +### Added + +- The command and query validators now allow rules to return `null` to indicate no errors. + ### Changed - BREAKING: changed the `ErrorIterableInterface` to `ListOfErrorsInterface`. Result objects now only accept list of diff --git a/src/Bus/Validation/AbstractValidator.php b/src/Bus/Validation/AbstractValidator.php index 9d59a5d..bac6b13 100644 --- a/src/Bus/Validation/AbstractValidator.php +++ b/src/Bus/Validation/AbstractValidator.php @@ -19,6 +19,7 @@ namespace CloudCreativity\Modules\Bus\Validation; +use CloudCreativity\Modules\Bus\Results\ListOfErrors; use CloudCreativity\Modules\Bus\Results\ListOfErrorsInterface; use CloudCreativity\Modules\Toolkit\Pipeline\AccumulationProcessor; use CloudCreativity\Modules\Toolkit\Pipeline\PipeContainerInterface; @@ -77,8 +78,10 @@ protected function getPipeline(): PipelineInterface private function processor(): AccumulationProcessor { return new AccumulationProcessor( - static fn (?ListOfErrorsInterface $carry, ListOfErrorsInterface $errors): ListOfErrorsInterface => - $carry ? $carry->merge($errors) : $errors, + static function (?ListOfErrorsInterface $carry, ?ListOfErrorsInterface $errors): ListOfErrorsInterface { + $errors ??= new ListOfErrors(); + return $carry ? $carry->merge($errors) : $errors; + }, ); } } diff --git a/tests/Unit/Bus/Validation/CommandValidatorTest.php b/tests/Unit/Bus/Validation/CommandValidatorTest.php index 28d2c5a..5f8504f 100644 --- a/tests/Unit/Bus/Validation/CommandValidatorTest.php +++ b/tests/Unit/Bus/Validation/CommandValidatorTest.php @@ -43,14 +43,19 @@ public function test(): void return new ListOfErrors($error1); }; - $b = function ($actual) use ($command, $error2, $error3): ListOfErrors { + $b = function ($actual) use ($command): ?ListOfErrors { + $this->assertSame($command, $actual); + return null; + }; + + $c = function ($actual) use ($command, $error2, $error3): ListOfErrors { $this->assertSame($command, $actual); return new ListOfErrors($error2, $error3); }; $validator = new CommandValidator(new PipelineBuilderFactory()); $actual = $validator - ->using([$a, $b]) + ->using([$a, $b, $c]) ->validate($command); $this->assertInstanceOf(ListOfErrors::class, $actual); diff --git a/tests/Unit/Bus/Validation/QueryValidatorTest.php b/tests/Unit/Bus/Validation/QueryValidatorTest.php index ac93d02..11a7593 100644 --- a/tests/Unit/Bus/Validation/QueryValidatorTest.php +++ b/tests/Unit/Bus/Validation/QueryValidatorTest.php @@ -43,14 +43,19 @@ public function test(): void return new ListOfErrors($error1); }; - $b = function ($actual) use ($query, $error2, $error3): ListOfErrors { + $b = function ($actual) use ($query): ?ListOfErrors { + $this->assertSame($query, $actual); + return null; + }; + + $c = function ($actual) use ($query, $error2, $error3): ListOfErrors { $this->assertSame($query, $actual); return new ListOfErrors($error2, $error3); }; $validator = new QueryValidator(new PipelineBuilderFactory()); $actual = $validator - ->using([$a, $b]) + ->using([$a, $b, $c]) ->validate($query); $this->assertInstanceOf(ListOfErrors::class, $actual);