Skip to content

Commit 75a2315

Browse files
committed
Improve messages in an array format
1 parent 1915b6f commit 75a2315

File tree

3 files changed

+137
-20
lines changed

3 files changed

+137
-20
lines changed

library/Message/StandardFormatter.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,39 +106,42 @@ public function array(Result $result, array $templates, Translator $translator):
106106
{
107107
$selectedTemplates = $this->selectTemplates($result, $templates);
108108
$deduplicatedChildren = $this->extractDeduplicatedChildren($result);
109-
if (count($deduplicatedChildren) === 0 || $this->isFinalTemplate($result, $selectedTemplates)) {
110-
return [
111-
$result->getDeepestPath() ?? $result->id => $this->renderer->render(
112-
$this->getTemplated($result->withDeepestPath(), $selectedTemplates),
109+
$messages = [
110+
'_self' => [
111+
$result->id => $this->renderer->render(
112+
$this->getTemplated($result, $selectedTemplates)->withWithoutPath(),
113113
$translator
114-
),
115-
];
116-
}
117-
118-
$messages = [];
114+
)
115+
],
116+
'_children' => []
117+
];
119118
foreach ($deduplicatedChildren as $child) {
119+
if ($child->path === null) {
120+
$messages['_self'][$child->id] = $this->renderer->render(
121+
$this->getTemplated($child, $selectedTemplates)->withWithoutPath(),
122+
$translator
123+
);
124+
continue;
125+
}
120126
$key = $child->getDeepestPath() ?? $child->id;
121-
$messages[$key] = $this->array(
127+
$messages['_children'][$key] = $this->array(
122128
$this->resultWithPath($result, $child),
123129
$this->selectTemplates($child, $selectedTemplates),
124130
$translator
125131
);
126-
if (count($messages[$key]) !== 1) {
132+
if (count($messages['_children'][$key]) !== 1) {
127133
continue;
128134
}
129135

130-
$messages[$key] = current($messages[$key]);
136+
$messages['_children'][$key] = current($messages['_children'][$key]);
131137
}
132138

133-
if (count($messages) > 1) {
134-
$self = [
135-
'__root__' => $this->renderer->render(
136-
$this->getTemplated($result->withDeepestPath(), $selectedTemplates),
137-
$translator
138-
),
139-
];
139+
if ($messages['_children'] === []) {
140+
unset($messages['_children']);
141+
}
140142

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

144147
return $messages;

library/Result.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ public function withDeepestPath(): self
132132
);
133133
}
134134

135+
public function withWithoutPath(): self
136+
{
137+
return new self(
138+
$this->isValid,
139+
$this->input,
140+
$this->rule,
141+
$this->parameters,
142+
$this->template,
143+
$this->mode,
144+
$this->name,
145+
$this->id,
146+
$this->adjacent?->withWithoutPath(),
147+
null,
148+
...($this->children)
149+
);
150+
}
151+
135152
public function getDeepestPath(): ?string
136153
{
137154
if ($this->path === null) {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
test('https://github.com/Respect/Validation/discussions/1427', expectAll(
11+
fn () => v::each(
12+
v::arrayVal()
13+
->key('groups', v::each(v::intVal()))
14+
->key('permissions', v::each(v::boolVal()))
15+
)
16+
->assert([
17+
16 => [
18+
'groups' => [1, 'A', 3, 4, 5],
19+
'permissions' => [
20+
'perm1' => true,
21+
'perm2' => false,
22+
'perm3' => 'boom!',
23+
],
24+
],
25+
18 => false,
26+
24 => ['permissions' => false],
27+
]),
28+
'`.16.groups.1` must be an integer value',
29+
<<<'FULL_MESSAGE'
30+
- Each item in `[16: ["groups": [1, "A", 3, 4, 5], "permissions": ["perm1": true, "perm2": false, "perm3": "boom!"]], 18: false, ... ]` must be valid
31+
- `.16` must pass the rules
32+
- Each item in `.groups` must be valid
33+
- `.1` must be an integer value
34+
- Each item in `.permissions` must be valid
35+
- `.perm3` must be a boolean value
36+
- `.18` must pass all the rules
37+
- `.18` must be an array value
38+
- `.groups` must be present
39+
- `.permissions` must be present
40+
- `.24` must pass the rules
41+
- `.groups` must be present
42+
- `.permissions` must be iterable
43+
FULL_MESSAGE,
44+
[
45+
'_self' => ['each' => 'Each item in `[16: ["groups": [1, "A", 3, 4, 5], "permissions": ["perm1": true, "perm2": false, "perm3": "boom!"]], 18: false, ... ]` must be valid'],
46+
'_children' => [
47+
16 => [
48+
'_self' => ['allOf' => '`["groups": [1, "A", 3, 4, 5], "permissions": ["perm1": true, "perm2": false, "perm3": "boom!"]]` must pass the rules'],
49+
'_children' => [
50+
'groups' => [
51+
'_self' => ['each' => 'Each item in `[1, "A", 3, 4, 5]` must be valid'],
52+
'_children' => [
53+
1 => [
54+
'_self' => ['intVal' => '"A" must be an integer value'],
55+
],
56+
],
57+
],
58+
'permissions' => [
59+
'_self' => ['each' => 'Each item in `["perm1": true, "perm2": false, "perm3": "boom!"]` must be valid'],
60+
'_children' => [
61+
'perm3' => [
62+
'_self' => ['boolVal' => '"boom!" must be a boolean value'],
63+
],
64+
],
65+
],
66+
],
67+
],
68+
18 => [
69+
'_self' => [
70+
'allOf' => '`false` must pass all the rules',
71+
'arrayVal' => '`false` must be an array value',
72+
],
73+
'_children' => [
74+
'groups' => [
75+
'_self' => ['keyExists' => '`false` must be present'],
76+
],
77+
'permissions' => [
78+
'_self' => ['keyExists' => '`false` must be present'],
79+
],
80+
],
81+
],
82+
24 => [
83+
'_self' => [
84+
'allOf' => '`["permissions": false]` must pass the rules',
85+
],
86+
'_children' => [
87+
'groups' => [
88+
'_self' => ['keyExists' => '`["permissions": false]` must be present'],
89+
],
90+
'permissions' => [
91+
'_self' => ['each' => '`false` must be iterable'],
92+
],
93+
],
94+
],
95+
],
96+
]
97+
));

0 commit comments

Comments
 (0)