12
12
namespace EventEngine \JsonSchema \Exception ;
13
13
14
14
15
- use Opis \JsonSchema \ValidationError ;
15
+ use Opis \JsonSchema \Errors \ErrorFormatter ;
16
+ use Opis \JsonSchema \Errors \ValidationError ;
16
17
17
18
class OpisJsonValidationError extends JsonValidationError
18
19
{
19
20
/**
20
21
* @var ValidationError[]
21
22
*/
22
- private $ errors ;
23
+ private array $ errors ;
24
+ private ?ErrorFormatter $ errorFormatter = null ;
23
25
24
26
public static function withError (string $ objectName , ValidationError ...$ validationErrors ): OpisJsonValidationError
25
27
{
@@ -28,15 +30,6 @@ public static function withError(string $objectName, ValidationError ...$validat
28
30
29
31
foreach ($ validationErrors as $ error ) {
30
32
$ self ->message .= $ self ->errorMessage ($ error );
31
-
32
- if ($ error ->subErrorsCount ()) {
33
- $ self ->message .= \array_reduce (
34
- $ error ->subErrors (),
35
- static function ($ message , ValidationError $ error ) use ($ self ) {
36
- return $ message . "\n" . $ self ->errorMessage ($ error );
37
- }
38
- );
39
- }
40
33
}
41
34
42
35
return $ self ;
@@ -50,18 +43,13 @@ public function errors(): array
50
43
return $ this ->errors ;
51
44
}
52
45
53
- private function errorMessage ( ValidationError $ error ): string
46
+ private function errorFormatter ( ): ErrorFormatter
54
47
{
55
- $ dataPointer = $ error ->dataPointer ();
56
-
57
- if (count ($ dataPointer ) === 0 ) {
58
- return \sprintf ('[%s] %s ' , $ error ->keyword (), \json_encode ($ error ->keywordArgs (), JSON_PRETTY_PRINT ));
59
- }
48
+ return $ this ->errorFormatter ??= new ErrorFormatter ();
49
+ }
60
50
61
- return \sprintf ('field "%s" [%s] %s ' ,
62
- implode ('. ' , $ dataPointer ),
63
- $ error ->keyword (),
64
- \json_encode ($ error ->keywordArgs (), JSON_PRETTY_PRINT )
65
- );
51
+ private function errorMessage (ValidationError $ error ): string
52
+ {
53
+ return json_encode ($ this ->errorFormatter ()->formatOutput ($ error , "basic " ), JSON_PRETTY_PRINT );
66
54
}
67
55
}
0 commit comments