Fix message overriding bug in NestedArrayFormatter

This commit resolves an issue where validation messages would overwrite
each other when multiple validators failed on the same path or key
(e.g., within an `Each` or `Key` validator).

Changes to `NestedArrayFormatter`:
- Implemented a merge strategy: Key collisions now result in a list of
  messages instead of the last message winning.
- Improved handling of mixed key types: When both numeric and string
  keys are present (common in composite validators), numeric keys are now
  replaced by the validator's ID (e.g., `arrayType`, `equals`) to provide
  meaningful, distinct keys.
- Preserved list behavior: Purely numeric key sets are treated as lists,
  maintaining their sequence without re-keying logic.
- Refactored the class to use smaller, single-purpose methods and
  `array_reduce` for clarity.

Tests:
- Updated feature tests (`EachTest`, `AttributesTest`, etc.) to expect the
  full set of validation errors.
- Enhanced `NestedArrayFormatterTest` with scenarios for key collisions,
  mixed keys, and ID substitution.
This commit is contained in:
Alexandre Gomes Gaigalas 2026-01-11 01:09:35 -03:00
commit cd96d01364
6 changed files with 191 additions and 23 deletions

View file

@ -13,6 +13,7 @@ use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Message\StandardFormatter\ResultCreator;
use Respect\Validation\Path;
use Respect\Validation\Result;
use Respect\Validation\Test\Builders\ResultBuilder;
use Respect\Validation\Test\Message\TestingMessageRenderer;
@ -37,7 +38,7 @@ final class NestedArrayFormatterTest extends TestCase
self::assertSame($expected, $formatter->format($result, $renderer, $templates));
}
/** @return array<string, array{0: Result, 1: array<string, mixed>, 2?: array<string, mixed>}> */
/** @return array<string, array{0: Result, 1: array<int|string, mixed>, 2?: array<int|string, mixed>}> */
public static function provideForArray(): array
{
return [
@ -76,6 +77,65 @@ final class NestedArrayFormatterTest extends TestCase
'3rd' => '__3rd_original__',
],
],
'with string key collision' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path('foo'))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path('foo'))->build(),
)->build(),
[
'foo' => ['msg1', 'msg2'],
],
],
'with numeric key collision (list)' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path(0))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path(0))->build(),
)->build(),
[
'__root__' => 'root_msg',
0 => 'msg1',
1 => 'msg2',
],
],
'with mixed keys replacement' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path('foo'))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path(0))->build(),
)->build(),
[
'__root__' => 'root_msg',
'foo' => 'msg1',
'c2' => 'msg2',
],
],
'with mixed keys and ID collision' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path('foo'))->build(),
(new ResultBuilder())->id('sameId')->template('msg2')->withPath(new Path(0))->build(),
(new ResultBuilder())->id('sameId')->template('msg3')->withPath(new Path(1))->build(),
)->build(),
[
'__root__' => 'root_msg',
'foo' => 'msg1',
'sameId' => ['msg2', 'msg3'],
],
],
'with pure numeric keys' => [
(new ResultBuilder())->id('root')->template('root_msg')
->children(
(new ResultBuilder())->id('c1')->template('msg1')->withPath(new Path(10))->build(),
(new ResultBuilder())->id('c2')->template('msg2')->withPath(new Path(20))->build(),
)->build(),
[
'__root__' => 'root_msg',
0 => 'msg1',
1 => 'msg2',
],
],
];
}
}