@@ -32,21 +32,37 @@ public function normalize($object, $format = null, array $context = array())
32
32
$ violations = array ();
33
33
$ messages = array ();
34
34
foreach ($ object as $ violation ) {
35
- $ violations [] = array (
36
- 'propertyPath ' => $ violation ->getPropertyPath (),
37
- 'message ' => $ violation ->getMessage (),
38
- 'code ' => $ violation ->getCode (),
39
- );
40
35
$ propertyPath = $ violation ->getPropertyPath ();
36
+
37
+ $ violationEntry = array (
38
+ 'propertyPath ' => $ propertyPath ,
39
+ 'title ' => $ violation ->getMessage (),
40
+ );
41
+ if (null !== $ code = $ violation ->getCode ()) {
42
+ $ violationEntry ['type ' ] = sprintf ('urn:uuid:%s ' , $ code );
43
+ }
44
+
45
+ $ violations [] = $ violationEntry ;
46
+
41
47
$ prefix = $ propertyPath ? sprintf ('%s: ' , $ propertyPath ) : '' ;
42
48
$ messages [] = $ prefix .$ violation ->getMessage ();
43
49
}
44
50
45
- return array (
46
- 'title ' => isset ($ context ['title ' ]) ? $ context ['title ' ] : 'An error occurred ' ,
47
- 'detail ' => $ messages ? implode ("\n" , $ messages ) : '' ,
48
- 'violations ' => $ violations ,
51
+ $ result = array (
52
+ 'type ' => $ context ['type ' ] ?? 'https://symfony.com/errors/validation ' ,
53
+ 'title ' => $ context ['title ' ] ?? 'Validation Failed ' ,
49
54
);
55
+ if (isset ($ context ['status ' ])) {
56
+ $ result ['status ' ] = $ context ['status ' ];
57
+ }
58
+ if ($ messages ) {
59
+ $ result ['detail ' ] = implode ("\n" , $ messages );
60
+ }
61
+ if (isset ($ context ['instance ' ])) {
62
+ $ result ['instance ' ] = $ context ['instance ' ];
63
+ }
64
+
65
+ return $ result + array ('violations ' => $ violations );
50
66
}
51
67
52
68
/**
0 commit comments