Skip to content

Commit

Permalink
feat: allow command and query validation rules to return null
Browse files Browse the repository at this point in the history
In this instance, null indicates no errors.
  • Loading branch information
lindyhopchris committed Nov 22, 2023
1 parent b1d1e37 commit 423e8ca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/Bus/Validation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
},
);
}
}
9 changes: 7 additions & 2 deletions tests/Unit/Bus/Validation/CommandValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions tests/Unit/Bus/Validation/QueryValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 423e8ca

Please sign in to comment.