Skip to content

Commit 78eba7c

Browse files
15webprokofievpchelk1n
authored andcommitted
feat(valinor): добавил перевод ошибок валидации
1 parent b57e4ba commit 78eba7c

File tree

2 files changed

+130
-6
lines changed

2 files changed

+130
-6
lines changed

backend/src/Infrastructure/Request/BuildValidationError.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Infrastructure\AsService;
88
use CuyZ\Valinor\Mapper\MappingError;
9+
use CuyZ\Valinor\Mapper\Tree\Message\Formatter\TranslationMessageFormatter;
910
use CuyZ\Valinor\Mapper\Tree\Message\Messages;
1011

1112
/**
@@ -14,6 +15,14 @@
1415
#[AsService]
1516
final readonly class BuildValidationError
1617
{
18+
private TranslationMessageFormatter $messageFormatter;
19+
20+
public function __construct()
21+
{
22+
$this->messageFormatter = (new TranslationMessageFormatter())
23+
->withTranslations(ValidationError::TRANSLATIONS);
24+
}
25+
1726
/**
1827
* @return non-empty-list<non-empty-string>
1928
*/
@@ -24,15 +33,20 @@ public function __invoke(MappingError $error): array
2433
);
2534

2635
$allMessages = [];
27-
foreach ($messages->errors() as $message) {
28-
$allMessages[] = $message
29-
->withBody('{node_path}: {original_message}')
36+
37+
foreach ($messages->errors() as $node) {
38+
$message = $this->messageFormatter
39+
->format($node)
3040
->toString();
41+
42+
if (!$node->node()->isRoot()) {
43+
$message = \sprintf('%s: %s', $node->node()->path(), $message);
44+
}
45+
46+
$allMessages[] = $message;
3147
}
3248

33-
/**
34-
* @var non-empty-list<non-empty-string> $allMessages
35-
*/
49+
/** @var non-empty-list<non-empty-string> $allMessages */
3650
return $allMessages;
3751
}
3852
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Infrastructure\Request;
6+
7+
/**
8+
* Перевод ошибок валидации
9+
*/
10+
final readonly class ValidationError
11+
{
12+
public const array TRANSLATIONS = [
13+
'Unexpected key(s) {keys}, expected {expected_keys}.' => [
14+
'en' => 'Недопустимые аргументы {keys}, допустимые: {expected_keys}.',
15+
],
16+
'Value {source_value} does not match any of {allowed_values}.' => [
17+
'en' => 'Value {source_value} does not match any of {allowed_values}.',
18+
],
19+
'Value {source_value} does not match any of {allowed_types}.' => [
20+
'en' => 'Value {source_value} does not match any of {allowed_types}.',
21+
],
22+
'Cannot be empty and must be filled with a value matching any of {allowed_types}.' => [
23+
'en' => 'Cannot be empty and must be filled with a value matching any of {allowed_types}.',
24+
],
25+
'Value {source_value} does not match type {expected_type}.' => [
26+
'en' => 'Value {source_value} does not match type {expected_type}.',
27+
],
28+
'Value {source_value} does not match {expected_value}.' => [
29+
'en' => 'Value {source_value} does not match {expected_value}.',
30+
],
31+
'Value {source_value} does not match boolean value {expected_value}.' => [
32+
'en' => 'Value {source_value} does not match boolean value {expected_value}.',
33+
],
34+
'Value {source_value} does not match float value {expected_value}.' => [
35+
'en' => 'Value {source_value} does not match float value {expected_value}.',
36+
],
37+
'Value {source_value} does not match integer value {expected_value}.' => [
38+
'en' => 'Value {source_value} does not match integer value {expected_value}.',
39+
],
40+
'Value {source_value} does not match string value {expected_value}.' => [
41+
'en' => 'Value {source_value} does not match string value {expected_value}.',
42+
],
43+
'Value {source_value} is not null.' => [
44+
'en' => 'Value {source_value} is not null.',
45+
],
46+
'Value {source_value} is not a valid boolean.' => [
47+
'en' => 'Value {source_value} is not a valid boolean.',
48+
],
49+
'Value {source_value} is not a valid float.' => [
50+
'en' => 'Value {source_value} is not a valid float.',
51+
],
52+
'Value {source_value} is not a valid integer.' => [
53+
'en' => 'Value {source_value} is not a valid integer.',
54+
],
55+
'Value {source_value} is not a valid string.' => [
56+
'en' => 'Value {source_value} is not a valid string.',
57+
],
58+
'Value {source_value} is not a valid negative integer.' => [
59+
'en' => 'Value {source_value} is not a valid negative integer.',
60+
],
61+
'Value {source_value} is not a valid positive integer.' => [
62+
'en' => 'Value {source_value} is not a valid positive integer.',
63+
],
64+
'Value {source_value} is not a valid non-empty string.' => [
65+
'en' => 'Value {source_value} is not a valid non-empty string.',
66+
],
67+
'Value {source_value} is not a valid numeric string.' => [
68+
'en' => 'Value {source_value} is not a valid numeric string.',
69+
],
70+
'Value {source_value} is not a valid integer between {min} and {max}.' => [
71+
'en' => 'Value {source_value} is not a valid integer between {min} and {max}.',
72+
],
73+
'Value {source_value} is not a valid timezone.' => [
74+
'en' => 'Value {source_value} is not a valid timezone.',
75+
],
76+
'Value {source_value} is not a valid class string.' => [
77+
'en' => 'Value {source_value} is not a valid class string.',
78+
],
79+
'Value {source_value} is not a valid class string of `{expected_class_type}`.' => [
80+
'en' => 'Value {source_value} is not a valid class string of `{expected_class_type}`.',
81+
],
82+
'Invalid value {source_value}.' => [
83+
'en' => 'Invalid value {source_value}.',
84+
],
85+
'Invalid value {source_value}, it matches at least two types from union.' => [
86+
'en' => 'Invalid value {source_value}, it matches at least two types from union.',
87+
],
88+
'Invalid value {source_value}, it matches at least two types from {allowed_types}.' => [
89+
'en' => 'Invalid value {source_value}, it matches at least two types from {allowed_types}.',
90+
],
91+
'Invalid sequential key {key}, expected {expected}.' => [
92+
'en' => 'Invalid sequential key {key}, expected {expected}.',
93+
],
94+
'Cannot be empty.' => [
95+
'en' => 'Cannot be empty.',
96+
],
97+
'Cannot be empty and must be filled with a value matching type {expected_type}.' => [
98+
'en' => 'Cannot be empty and must be filled with a value matching type {expected_type}.',
99+
],
100+
'Key {key} does not match type {expected_type}.' => [
101+
'en' => 'Key {key} does not match type {expected_type}.',
102+
],
103+
'Value {source_value} does not match a valid date format.' => [
104+
'en' => 'Value {source_value} does not match a valid date format.',
105+
],
106+
'Value {source_value} does not match any of the following formats: {formats}.' => [
107+
'en' => 'Value {source_value} does not match any of the following formats: {formats}.',
108+
],
109+
];
110+
}

0 commit comments

Comments
 (0)