forked from graphql-java/graphql-java-extended-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractDirectiveConstraint.java
458 lines (399 loc) · 16.9 KB
/
AbstractDirectiveConstraint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package graphql.validation.constraints;
import graphql.Assert;
import graphql.GraphQLError;
import graphql.PublicSpi;
import graphql.Scalars;
import graphql.schema.GraphQLAppliedDirective;
import graphql.schema.GraphQLAppliedDirectiveArgument;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLNamedInputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLTypeUtil;
import graphql.validation.rules.ValidationEnvironment;
import graphql.validation.util.DirectivesAndTypeWalker;
import graphql.validation.util.Util;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static graphql.schema.GraphQLTypeUtil.isList;
import static graphql.validation.rules.ValidationEnvironment.ValidatedElement.FIELD;
import static graphql.validation.util.Util.mkMap;
import static java.lang.String.format;
import static java.util.Collections.singletonList;
@SuppressWarnings("UnnecessaryLocalVariable")
@PublicSpi
public abstract class AbstractDirectiveConstraint implements DirectiveConstraint {
private final String name;
public AbstractDirectiveConstraint(String name) {
this.name = name;
}
@Override
public String toString() {
return "@" + name;
}
@Override
public String getName() {
return name;
}
protected String getMessageTemplate() {
return "graphql.validation." + getName() + ".message";
}
@Override
public boolean appliesTo(GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) {
return false;
}
@Override
public boolean appliesTo(GraphQLArgument argument, GraphQLFieldDefinition fieldDefinition, GraphQLFieldsContainer fieldsContainer) {
boolean suitable = new DirectivesAndTypeWalker().isSuitable(argument, (inputType, directive) -> {
boolean hasNamedDirective = directive.getName().equals(this.getName());
if (hasNamedDirective) {
inputType = Util.unwrapNonNull(inputType);
boolean appliesToType;
if (appliesToListElements()) {
appliesToType = appliesToType((GraphQLInputType) GraphQLTypeUtil.unwrapAll(inputType));
} else {
appliesToType = appliesToType(inputType);
}
if (appliesToType) {
return true;
}
// if they have a @Directive on there BUT it can't handle that type
// then is a really bad situation
String argType = GraphQLTypeUtil.simplePrint(inputType);
Assert.assertTrue(false, () -> format("The directive rule '%s' cannot be placed on elements of type '%s'", "@" + this.getName(), argType));
}
return false;
});
return suitable;
}
/**
* A derived class will be called to indicate whether this input type applies to the constraint
*
* @param inputType the input type
* @return true if the constraint can handle that type
*/
abstract protected boolean appliesToType(GraphQLInputType inputType);
/**
* This is called to perform the constraint validation
*
* @param validationEnvironment the validation environment
* @return a list of errors or an empty one if there are no errors
*/
abstract protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment);
@Override
public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();
// output fields are special
if (validationEnvironment.getValidatedElement() == FIELD) {
return runValidationImpl(validationEnvironment);
}
//
// all the directives validation code does NOT care for NULL ness since the graphql engine covers that.
// eg a @NonNull validation directive makes no sense in graphql like it might in Java
//
if (validatedValue == null) {
return Collections.emptyList();
}
GraphQLInputType inputType = Util.unwrapNonNull(validationEnvironment.getValidatedType());
validationEnvironment = validationEnvironment.transform(b -> b.validatedType(inputType));
return runValidationImpl(validationEnvironment);
}
private List<GraphQLError> runValidationImpl(ValidationEnvironment validationEnvironment) {
return runConstraintOnDirectives(validationEnvironment);
}
private List<GraphQLError> runConstraintOnDirectives(ValidationEnvironment validationEnvironment) {
List<GraphQLError> errors = new ArrayList<>();
List<GraphQLAppliedDirective> directives = validationEnvironment.getDirectives();
directives = Util.sort(directives, GraphQLAppliedDirective::getName);
for (GraphQLAppliedDirective directive : directives) {
// we get called for arguments and input field and field types which can have multiple directive constraints on them and hence no just for this one
boolean isOurDirective = directive.getName().equals(this.getName());
if (!isOurDirective) {
continue;
}
validationEnvironment = validationEnvironment.transform(b -> b.context(GraphQLAppliedDirective.class, directive));
//
// now run the directive rule with this directive instance
List<GraphQLError> ruleErrors = this.runConstrainOnPossibleListElements(validationEnvironment);
errors.addAll(ruleErrors);
}
return errors;
}
private List<GraphQLError> runConstrainOnPossibleListElements(ValidationEnvironment validationEnvironment) {
if (appliesToListElements()) {
final GraphQLListElementValidator validator = new GraphQLListElementValidator();
return validator.runConstraintOnListElements(validationEnvironment, this::runConstraint);
}
return runConstraint(validationEnvironment);
}
protected abstract boolean appliesToListElements();
protected boolean isOneOfTheseTypes(GraphQLInputType inputType, GraphQLScalarType... scalarTypes) {
return isOneOfTheseTypes(inputType, Arrays.asList(scalarTypes));
}
/**
* Returns true of the input type is one of the specified scalar types, regardless of non null ness
*
* @param inputType the type to check
* @param scalarTypes the array of scalar types
* @return true if its one of them
*/
protected boolean isOneOfTheseTypes(GraphQLInputType inputType, Collection<GraphQLScalarType> scalarTypes) {
GraphQLInputType type = Util.unwrapNonNull(inputType);
if (type instanceof GraphQLNamedInputType) {
final GraphQLNamedInputType unwrappedType = (GraphQLNamedInputType) type;
for (GraphQLScalarType scalarType : scalarTypes) {
if (unwrappedType.getName().equals(scalarType.getName())) {
return true;
}
}
}
return false;
}
/**
* Returns an integer argument from a directive (or its default) and throws an assertion of the argument is null
*
* @param directive the directive to check
* @param argName the argument name
* @return a non null value
*/
protected int getIntArg(GraphQLAppliedDirective directive, String argName) {
GraphQLAppliedDirectiveArgument argument = directive.getArgument(argName);
if (argument == null) {
return assertExpectedArgType(argName, "Int");
}
Number value = argument.getValue();
if (value == null) {
return assertExpectedArgType(argName, "Int");
}
return value.intValue();
}
/**
* Returns an String argument from a directive (or its default) and throws an assertion of the argument is null
*
* @param directive the directive to check
* @param argName the argument name
* @return a non null value
*/
protected String getStrArg(GraphQLAppliedDirective directive, String argName) {
GraphQLAppliedDirectiveArgument argument = directive.getArgument(argName);
if (argument == null) {
return assertExpectedArgType(argName, "String");
}
String value = argument.getValue();
if (value == null) {
return assertExpectedArgType(argName, "String");
}
return value;
}
/**
* Returns an boolean argument from a directive (or its default) and throws an assertion of the argument is null
*
* @param directive the directive to check
* @param argName the argument name
* @return a non null value
*/
protected boolean getBoolArg(GraphQLAppliedDirective directive, String argName) {
GraphQLAppliedDirectiveArgument argument = directive.getArgument(argName);
if (argument == null) {
return assertExpectedArgType(argName, "Boolean");
}
Object value = argument.getValue();
if (value == null) {
return assertExpectedArgType(argName, "Boolean");
}
return Boolean.parseBoolean(String.valueOf(value));
}
/**
* Returns the "message : String" argument from a directive or makes up one
* called "graphql.validation.{name}.message"
*
* @param directive the directive to check
* @return a non null value
*/
protected String getMessageTemplate(GraphQLAppliedDirective directive) {
String msg = null;
GraphQLAppliedDirectiveArgument arg = directive.getArgument("message");
if (arg != null) {
msg = arg.getValue();
}
if (msg == null) {
msg = "graphql.validation." + getName() + ".message";
}
return msg;
}
/**
* Creates a map of named parameters for message interpolation
*
* @param validatedValue the value being validated
* @param validationEnvironment the validation environment
* @param args must be an key / value array with String keys as the even params and values as then odd params
* @return a map of message parameters
*/
protected Map<String, Object> mkMessageParams(Object validatedValue, ValidationEnvironment validationEnvironment, Object... args) {
Map<String, Object> params = new LinkedHashMap<>();
params.put("validatedValue", validatedValue);
params.put("constraint", getName());
params.put("path", validationEnvironment.getValidatedPath());
params.putAll(mkMap(args));
return params;
}
/**
* Creates a new {@link graphql.GraphQLError}
*
* @param validationEnvironment the current validation environment
* @param directive the directive being run
* @param msgParams the map of parameters
* @return a list of a single error
*/
protected List<GraphQLError> mkError(ValidationEnvironment validationEnvironment, GraphQLAppliedDirective directive, Map<String, Object> msgParams) {
String messageTemplate = getMessageTemplate(directive);
GraphQLError error = validationEnvironment.getInterpolator().interpolate(messageTemplate, msgParams, validationEnvironment);
return singletonList(error);
}
protected List<GraphQLError> mkError(ValidationEnvironment validationEnvironment, Object... messageParameters) {
GraphQLAppliedDirective directive = validationEnvironment.getContextObject(GraphQLAppliedDirective.class);
String messageTemplate = getMessageTemplate(directive);
Object validatedValue = validationEnvironment.getValidatedValue();
GraphQLError error = validationEnvironment.getInterpolator().interpolate(messageTemplate, mkMessageParams(validatedValue, validationEnvironment, messageParameters), validationEnvironment);
return singletonList(error);
}
/**
* Return true if the type is a String or ID or List type, regardless of non null ness
*
* @param inputType the type to check
* @return true if one of the above
*/
protected boolean isStringOrIDOrList(GraphQLInputType inputType) {
return isStringOrID(inputType) ||
isList(inputType);
}
/**
* Return true if the type is a String or ID or List type or {@link graphql.schema.GraphQLInputObjectType}, regardless of non null ness
*
* @param inputType the type to check
* @return true if one of the above
*/
protected boolean isStringOrIDOrListOrMap(GraphQLInputType inputType) {
return isStringOrID(inputType) ||
isList(inputType) ||
isMap(inputType);
}
protected boolean isMap(GraphQLInputType inputType) {
GraphQLInputType unwrappedType = Util.unwrapOneAndAllNonNull(inputType);
return (unwrappedType instanceof GraphQLInputObjectType);
}
/**
* Return true if the type is a String or ID
*
* @param inputType the type to check
* @return true if one of the above
*/
protected boolean isStringOrID(GraphQLInputType inputType) {
GraphQLInputType unwrappedType = Util.unwrapNonNull(inputType);
return Scalars.GraphQLString.equals(unwrappedType) || Scalars.GraphQLID.equals(unwrappedType);
}
/**
* Casts the object as a Map with an assertion of it is not one
*
* @param value the object to turn into a map
* @return a Map
*/
@SuppressWarnings("ConstantConditions")
protected Map asMap(Object value) {
Assert.assertTrue(value instanceof Map, () -> "The argument value MUST be a Map value");
return (Map) value;
}
/**
* Makes the object a BigDecimal with an assertion if we have no conversion of it
*
* @param value the object to turn into a BigDecimal
* @return a BigDecimal
*/
protected BigDecimal asBigDecimal(Object value) throws NumberFormatException {
if (value == null) {
return Assert.assertShouldNeverHappen("Validation cant handle null objects BigDecimals");
}
if (value instanceof BigDecimal) {
return (BigDecimal) value;
}
String bdStr = "";
if (value instanceof Number) {
bdStr = value.toString();
} else if (value instanceof String) {
bdStr = value.toString();
} else {
Assert.assertShouldNeverHappen("Validation cant handle objects of type '%s' as BigDecimals", value.getClass().getSimpleName());
}
return new BigDecimal(bdStr);
}
/**
* Makes the object a boolean with an assertion if we have no conversion of it
*
* @param value the boolean object
* @return a boolean
*/
protected boolean asBoolean(Object value) {
if (value == null) {
return Assert.assertShouldNeverHappen("Validation cant handle null objects Booleans");
}
if (value instanceof Boolean) {
return (Boolean) value;
} else {
return Assert.assertShouldNeverHappen("Validation cant handle objects of type '%s' as Booleans", value.getClass().getSimpleName());
}
}
/**
* Returns the length of a String, ID, size of a list or size of a Map
*
* @param inputType the input type
* @param value the value
* @return the length of a String or Map or List
*/
protected int getStringOrIDOrObjectOrMapLength(GraphQLInputType inputType, Object value) {
int valLen;
if (value == null) {
valLen = 0;
} else if (isStringOrID(inputType)) {
valLen = String.valueOf(value).length();
} else if (isList(inputType)) {
valLen = getListLength(value);
} else {
valLen = getObjectLen(value);
}
return valLen;
}
private int getObjectLen(Object value) {
if (value == null) {
return 0;
}
Map map = asMap(value);
return map.size();
}
private int getListLength(Object value) {
if (value instanceof Collection) {
return ((Collection) value).size();
} else if (value instanceof Iterable) {
int len = 0;
for (Object ignored : ((Iterable) value)) {
len++;
}
return len;
} else if (value != null && value.getClass().isArray()) {
return Array.getLength(value);
}
return 0;
}
private <T> T assertExpectedArgType(String argName, String typeName) {
return Assert.assertShouldNeverHappen("A validation directive MUST have a '%s' argument of type '%s' with a default value", argName, typeName);
}
}