6
6
import java .util .HashMap ;
7
7
import java .util .List ;
8
8
import java .util .Map ;
9
+ import java .util .Set ;
10
+ import java .util .stream .Collectors ;
9
11
import java .util .stream .Stream ;
10
12
import java .util .stream .StreamSupport ;
11
13
14
16
15
17
import graphql .ErrorClassification ;
16
18
import graphql .GraphQLError ;
17
- import graphql .language . NamedNode ;
19
+ import graphql .execution . ResultPath ;
18
20
import graphql .language .SourceLocation ;
19
21
20
22
public class BeanValidationError implements GraphQLError {
21
- private final ConstraintViolation <?> violation ;
22
- private final List <NamedNode <?>> requestedPath ;
23
+ private final Set <ConstraintViolation <?>> violations ;
24
+ private final ResultPath resultPath ;
25
+ private final List <SourceLocation > sourceLocations ;
23
26
24
- public BeanValidationError (ConstraintViolation <?> violation , List <NamedNode <?>> requestedPath ) {
25
- this .violation = violation ;
26
- this .requestedPath = requestedPath ;
27
+ public BeanValidationError (
28
+ Set <ConstraintViolation <?>> violations ,
29
+ ResultPath resultPath ,
30
+ List <SourceLocation > sourceLocations ) {
31
+ this .violations = violations ;
32
+ this .resultPath = resultPath ;
33
+ this .sourceLocations = sourceLocations ;
27
34
}
28
35
29
36
@ Override
@@ -33,28 +40,33 @@ public ErrorClassification getErrorType() {
33
40
34
41
@ Override
35
42
public String getMessage () {
36
- return "validation failed: " + violation .getPropertyPath () + " " + violation .getMessage ();
43
+ String joinedMessage = violations .stream ()
44
+ .map (violation -> violation .getPropertyPath () + " " + violation .getMessage ())
45
+ .collect (Collectors .joining (", " ));
46
+ return "validation failed: " + joinedMessage ;
37
47
}
38
48
39
49
@ Override
40
50
public List <SourceLocation > getLocations () {
41
- return requestedPath . stream (). map ( NamedNode :: getSourceLocation ). collect ( toList ()) ;
51
+ return sourceLocations ;
42
52
}
43
53
44
54
@ Override
45
55
public List <Object > getPath () {
46
- return requestedPath . stream (). map ( argument -> ( Object ) argument . getName ()). collect ( toList () );
56
+ return resultPath . toList ();
47
57
}
48
58
49
59
@ Override
50
60
public Map <String , Object > getExtensions () {
51
- Map <String , Object > extensions = new HashMap <>();
52
- extensions .put ("violation.message" , violation .getMessage ());
53
- extensions .put ("violation.propertyPath" ,
54
- toStream (violation .getPropertyPath ()).flatMap (this ::items ).collect (toList ()));
55
- extensions .put ("violation.invalidValue" , violation .getInvalidValue ());
56
- extensions .put ("violation.constraint" , getConstraintAttributes ());
57
- return extensions ;
61
+ return Map .of ("violations" , violations .stream ().map (this ::getViolationAttributes ).collect (toList ()));
62
+ }
63
+
64
+ private Map <String , Object > getViolationAttributes (ConstraintViolation <?> violation ) {
65
+ return Map .of (
66
+ "message" , violation .getMessage (),
67
+ "propertyPath" , toStream (violation .getPropertyPath ()).flatMap (this ::items ).collect (toList ()),
68
+ "invalidValue" , violation .getInvalidValue (),
69
+ "constraint" , getConstraintAttributes (violation ));
58
70
}
59
71
60
72
private Stream <String > items (Path .Node node ) {
@@ -63,7 +75,7 @@ private Stream<String> items(Path.Node node) {
63
75
return Stream .of (node .getIndex ().toString (), node .getName ());
64
76
}
65
77
66
- private Map <String , Object > getConstraintAttributes () {
78
+ private Map <String , Object > getConstraintAttributes (ConstraintViolation <?> violation ) {
67
79
Map <String , Object > attributes = new HashMap <>(violation .getConstraintDescriptor ().getAttributes ());
68
80
attributes .computeIfPresent ("groups" , BeanValidationError ::classNames );
69
81
attributes .computeIfPresent ("payload" , BeanValidationError ::classNames );
0 commit comments