1
1
package graphql .validation .rules ;
2
2
3
+ import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .ARGUMENT ;
4
+ import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .FIELD ;
5
+ import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .INPUT_OBJECT_FIELD ;
6
+
7
+ import java .util .ArrayList ;
8
+ import java .util .Collections ;
9
+ import java .util .HashMap ;
10
+ import java .util .List ;
11
+ import java .util .Locale ;
12
+ import java .util .Map ;
13
+
14
+ import org .springframework .web .servlet .tags .form .InputTag ;
15
+
3
16
import graphql .Assert ;
4
17
import graphql .GraphQLError ;
5
18
import graphql .PublicApi ;
20
33
import graphql .validation .locale .LocaleUtil ;
21
34
import graphql .validation .util .Util ;
22
35
23
- import java .util .ArrayList ;
24
- import java .util .Collections ;
25
- import java .util .HashMap ;
26
- import java .util .List ;
27
- import java .util .Locale ;
28
- import java .util .Map ;
29
-
30
- import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .ARGUMENT ;
31
- import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .FIELD ;
32
- import static graphql .validation .rules .ValidationEnvironment .ValidatedElement .INPUT_OBJECT_FIELD ;
33
-
34
36
/**
35
37
* TargetedValidationRules is a holder of {@link graphql.validation.rules.ValidationRule}s targeted against a specific
36
38
* type, field and possible argument via {@link ValidationCoordinates}. It then allows those rules
40
42
@ PublicApi
41
43
public class TargetedValidationRules {
42
44
45
+ private final ValidationRules validationRules ;
46
+
43
47
private final Map <ValidationCoordinates , List <ValidationRule >> rulesMap ;
44
48
45
49
public TargetedValidationRules (Builder builder ) {
46
50
this .rulesMap = new HashMap <>(builder .rulesMap );
51
+ this .validationRules =builder .validationRules ;
47
52
}
48
53
49
- public static Builder newValidationRules () {
50
- return new Builder ();
54
+ public static Builder newValidationRules (ValidationRules validationRules ) {
55
+ return new Builder (validationRules );
51
56
}
52
57
53
58
public boolean isEmpty () {
@@ -117,34 +122,37 @@ public List<GraphQLError> runValidationRules(DataFetchingEnvironment env, Messag
117
122
.locale (defaultLocale )
118
123
.build ();
119
124
120
- for (ValidationRule rule : rules ) {
121
- List <GraphQLError > ruleErrors = runValidationImpl (rule , ruleEnvironment , inputType , argValue );
122
- errors .addAll (ruleErrors );
123
- }
125
+ errors .addAll (runValidationImpl (rules , ruleEnvironment , inputType , argValue ));
124
126
}
125
127
126
128
return errors ;
127
129
}
128
130
129
131
@ SuppressWarnings ("unchecked" )
130
- private List <GraphQLError > runValidationImpl (ValidationRule rule , ValidationEnvironment validationEnvironment , GraphQLInputType inputType , Object validatedValue ) {
131
- List <GraphQLError > errors = rule .runValidation (validationEnvironment );
132
+ private List <GraphQLError > runValidationImpl (List <ValidationRule > rules , ValidationEnvironment validationEnvironment , GraphQLInputType inputType , Object validatedValue ) {
133
+ List <GraphQLError > errors = new ArrayList <GraphQLError >();
134
+ for (ValidationRule rule : rules ) {
135
+ errors .addAll (rule .runValidation (validationEnvironment ));
136
+ }
137
+
132
138
if (validatedValue == null ) {
133
139
return errors ;
134
140
}
135
141
136
142
inputType = (GraphQLInputType ) GraphQLTypeUtil .unwrapNonNull (inputType );
137
143
138
- if (GraphQLTypeUtil .isList (inputType )) {
139
- List <Object > values = new ArrayList <>(FpKit .toCollection (validatedValue ));
140
- List <GraphQLError > ruleErrors = walkListArg (rule , validationEnvironment , (GraphQLList ) inputType , values );
141
- errors .addAll (ruleErrors );
144
+
145
+ if (GraphQLTypeUtil .isList (inputType )) {
146
+ List <Object > values = new ArrayList <>(FpKit .toCollection (validatedValue ));
147
+ List <GraphQLError > ruleErrors = walkListArg (rules , validationEnvironment , (GraphQLList ) inputType , values );
148
+ errors .addAll (ruleErrors );
142
149
}
150
+
143
151
144
152
if (inputType instanceof GraphQLInputObjectType ) {
145
153
if (validatedValue instanceof Map ) {
146
154
Map <String , Object > objectValue = (Map <String , Object >) validatedValue ;
147
- List <GraphQLError > ruleErrors = walkObjectArg (rule , validationEnvironment , (GraphQLInputObjectType ) inputType , objectValue );
155
+ List <GraphQLError > ruleErrors = walkObjectArg (validationEnvironment , (GraphQLInputObjectType ) inputType , objectValue );
148
156
errors .addAll (ruleErrors );
149
157
} else {
150
158
Assert .assertShouldNeverHappen ("How can there be a `input` object type '%s' that does not have a matching Map java value" , GraphQLTypeUtil .simplePrint (inputType ));
@@ -154,15 +162,14 @@ private List<GraphQLError> runValidationImpl(ValidationRule rule, ValidationEnvi
154
162
}
155
163
156
164
157
- private List <GraphQLError > walkObjectArg (ValidationRule rule , ValidationEnvironment validationEnvironment , GraphQLInputObjectType argumentType , Map <String , Object > objectMap ) {
165
+ private List <GraphQLError > walkObjectArg (ValidationEnvironment validationEnvironment , GraphQLInputObjectType argumentType , Map <String , Object > objectMap ) {
158
166
List <GraphQLError > errors = new ArrayList <>();
159
167
160
168
// run them in a stable order
161
169
List <GraphQLInputObjectField > fieldDefinitions = Util .sort (argumentType .getFieldDefinitions (), GraphQLInputObjectField ::getName );
162
170
for (GraphQLInputObjectField inputField : fieldDefinitions ) {
163
171
164
172
GraphQLInputType fieldType = inputField .getType ();
165
- List <GraphQLDirective > directives = inputField .getDirectives ();
166
173
Object validatedValue = objectMap .getOrDefault (inputField .getName (), inputField .getDefaultValue ());
167
174
if (validatedValue == null ) {
168
175
continue ;
@@ -177,14 +184,15 @@ private List<GraphQLError> walkObjectArg(ValidationRule rule, ValidationEnvironm
177
184
.directives (inputField .getDirectives ())
178
185
.validatedElement (INPUT_OBJECT_FIELD )
179
186
);
187
+
188
+ List <ValidationRule > rulesChild = validationRules .getRulesFor (newValidationEnvironment .getArgument (), newValidationEnvironment .getFieldDefinition (), newValidationEnvironment .getFieldsContainer ());
189
+ errors .addAll (runValidationImpl (rulesChild , newValidationEnvironment , fieldType , validatedValue ));
180
190
181
- List <GraphQLError > ruleErrors = runValidationImpl (rule , newValidationEnvironment , fieldType , validatedValue );
182
- errors .addAll (ruleErrors );
183
191
}
184
192
return errors ;
185
193
}
186
194
187
- private List <GraphQLError > walkListArg (ValidationRule rule , ValidationEnvironment validationEnvironment , GraphQLList argumentType , List <Object > objectList ) {
195
+ private List <GraphQLError > walkListArg (List < ValidationRule > rules , ValidationEnvironment validationEnvironment , GraphQLList argumentType , List <Object > objectList ) {
188
196
List <GraphQLError > errors = new ArrayList <>();
189
197
190
198
GraphQLInputType listItemType = Util .unwrapOneAndAllNonNull (argumentType );
@@ -206,16 +214,21 @@ private List<GraphQLError> walkListArg(ValidationRule rule, ValidationEnvironmen
206
214
.directives (directives )
207
215
);
208
216
209
- List <GraphQLError > ruleErrors = runValidationImpl (rule , newValidationEnvironment , listItemType , value );
217
+ List <GraphQLError > ruleErrors = runValidationImpl (rules , newValidationEnvironment , listItemType , value );
210
218
errors .addAll (ruleErrors );
211
219
ix ++;
212
220
}
213
221
return errors ;
214
222
}
215
223
216
224
public static class Builder {
225
+ ValidationRules validationRules ;
217
226
Map <ValidationCoordinates , List <ValidationRule >> rulesMap = new HashMap <>();
218
227
228
+ public Builder (ValidationRules validationRules ) {
229
+ this .validationRules =validationRules ;
230
+ }
231
+
219
232
public Builder addRule (ValidationCoordinates coordinates , ValidationRule rule ) {
220
233
rulesMap .compute (coordinates , (key , listOfRules ) -> {
221
234
if (listOfRules == null ) {
0 commit comments