Skip to content

Commit

Permalink
Improve messages in an array format
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquemoody committed Dec 27, 2024
1 parent 1915b6f commit 75a2315
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 20 deletions.
43 changes: 23 additions & 20 deletions library/Message/StandardFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,42 @@ public function array(Result $result, array $templates, Translator $translator):
{
$selectedTemplates = $this->selectTemplates($result, $templates);
$deduplicatedChildren = $this->extractDeduplicatedChildren($result);
if (count($deduplicatedChildren) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) {
return [
$result->getDeepestPath() ?? $result->id => $this->renderer->render(
$this->getTemplated($result->withDeepestPath(), $selectedTemplates),
$messages = [
'_self' => [
$result->id => $this->renderer->render(
$this->getTemplated($result, $selectedTemplates)->withWithoutPath(),
$translator
),
];
}

$messages = [];
)
],
'_children' => []
];
foreach ($deduplicatedChildren as $child) {
if ($child->path === null) {
$messages['_self'][$child->id] = $this->renderer->render(
$this->getTemplated($child, $selectedTemplates)->withWithoutPath(),
$translator
);
continue;
}
$key = $child->getDeepestPath() ?? $child->id;
$messages[$key] = $this->array(
$messages['_children'][$key] = $this->array(
$this->resultWithPath($result, $child),
$this->selectTemplates($child, $selectedTemplates),
$translator
);
if (count($messages[$key]) !== 1) {
if (count($messages['_children'][$key]) !== 1) {
continue;
}

$messages[$key] = current($messages[$key]);
$messages['_children'][$key] = current($messages['_children'][$key]);
}

if (count($messages) > 1) {
$self = [
'__root__' => $this->renderer->render(
$this->getTemplated($result->withDeepestPath(), $selectedTemplates),
$translator
),
];
if ($messages['_children'] === []) {
unset($messages['_children']);
}

return $self + $messages;
if ($result->path !== null) {
return [$result->getDeepestPath() => $messages];
}

return $messages;
Expand Down
17 changes: 17 additions & 0 deletions library/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,23 @@ public function withDeepestPath(): self
);
}

public function withWithoutPath(): self
{
return new self(
$this->isValid,
$this->input,
$this->rule,
$this->parameters,
$this->template,
$this->mode,
$this->name,
$this->id,
$this->adjacent?->withWithoutPath(),
null,
...($this->children)
);
}

public function getDeepestPath(): ?string
{
if ($this->path === null) {
Expand Down
97 changes: 97 additions & 0 deletions tests/feature/Issues/Issue1427Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

test('https://github.com/Respect/Validation/discussions/1427', expectAll(
fn () => v::each(
v::arrayVal()
->key('groups', v::each(v::intVal()))
->key('permissions', v::each(v::boolVal()))
)
->assert([
16 => [
'groups' => [1, 'A', 3, 4, 5],
'permissions' => [
'perm1' => true,
'perm2' => false,
'perm3' => 'boom!',
],
],
18 => false,
24 => ['permissions' => false],
]),
'`.16.groups.1` must be an integer value',
<<<'FULL_MESSAGE'
- Each item in `[16: ["groups": [1, "A", 3, 4, 5], "permissions": ["perm1": true, "perm2": false, "perm3": "boom!"]], 18: false, ... ]` must be valid
- `.16` must pass the rules
- Each item in `.groups` must be valid
- `.1` must be an integer value
- Each item in `.permissions` must be valid
- `.perm3` must be a boolean value
- `.18` must pass all the rules
- `.18` must be an array value
- `.groups` must be present
- `.permissions` must be present
- `.24` must pass the rules
- `.groups` must be present
- `.permissions` must be iterable
FULL_MESSAGE,
[
'_self' => ['each' => 'Each item in `[16: ["groups": [1, "A", 3, 4, 5], "permissions": ["perm1": true, "perm2": false, "perm3": "boom!"]], 18: false, ... ]` must be valid'],
'_children' => [
16 => [
'_self' => ['allOf' => '`["groups": [1, "A", 3, 4, 5], "permissions": ["perm1": true, "perm2": false, "perm3": "boom!"]]` must pass the rules'],
'_children' => [
'groups' => [
'_self' => ['each' => 'Each item in `[1, "A", 3, 4, 5]` must be valid'],
'_children' => [
1 => [
'_self' => ['intVal' => '"A" must be an integer value'],
],
],
],
'permissions' => [
'_self' => ['each' => 'Each item in `["perm1": true, "perm2": false, "perm3": "boom!"]` must be valid'],
'_children' => [
'perm3' => [
'_self' => ['boolVal' => '"boom!" must be a boolean value'],
],
],
],
],
],
18 => [
'_self' => [
'allOf' => '`false` must pass all the rules',
'arrayVal' => '`false` must be an array value',
],
'_children' => [
'groups' => [
'_self' => ['keyExists' => '`false` must be present'],
],
'permissions' => [
'_self' => ['keyExists' => '`false` must be present'],
],
],
],
24 => [
'_self' => [
'allOf' => '`["permissions": false]` must pass the rules',
],
'_children' => [
'groups' => [
'_self' => ['keyExists' => '`["permissions": false]` must be present'],
],
'permissions' => [
'_self' => ['each' => '`false` must be iterable'],
],
],
],
],
]
));

0 comments on commit 75a2315

Please sign in to comment.