2
2
3
3
import graphql .Assert ;
4
4
import graphql .GraphQLError ;
5
+ import graphql .PublicSpi ;
5
6
import graphql .Scalars ;
6
7
import graphql .schema .GraphQLArgument ;
7
8
import graphql .schema .GraphQLDirective ;
23
24
import static graphql .schema .GraphQLTypeUtil .isList ;
24
25
import static java .util .Collections .singletonList ;
25
26
27
+ @ PublicSpi
26
28
public abstract class AbstractDirectiveValidationRule implements DirectiveValidationRule {
27
29
28
30
private final String name ;
@@ -41,6 +43,14 @@ public boolean appliesToType(GraphQLArgument argument, GraphQLFieldDefinition fi
41
43
return appliesToType (Util .unwrapNonNull (argument .getType ()));
42
44
}
43
45
46
+ /**
47
+ * Returns true of the input type is one of the specified scalar types, regardless of non null ness
48
+ *
49
+ * @param inputType the type to check
50
+ * @param scalarTypes the array of scalar types
51
+ *
52
+ * @return true ifits oneof them
53
+ */
44
54
protected boolean isOneOfTheseTypes (GraphQLInputType inputType , GraphQLScalarType ... scalarTypes ) {
45
55
GraphQLInputType unwrappedType = Util .unwrapNonNull (inputType );
46
56
for (GraphQLScalarType scalarType : scalarTypes ) {
@@ -51,6 +61,14 @@ protected boolean isOneOfTheseTypes(GraphQLInputType inputType, GraphQLScalarTyp
51
61
return false ;
52
62
}
53
63
64
+ /**
65
+ * Returns an integer argument from a directive (or its default) and throws an assertion of the argument is null
66
+ *
67
+ * @param directive the directive to check
68
+ * @param argName the argument name
69
+ *
70
+ * @return a non null value
71
+ */
54
72
protected int getIntArg (GraphQLDirective directive , String argName ) {
55
73
GraphQLArgument argument = directive .getArgument (argName );
56
74
if (argument == null ) {
@@ -66,6 +84,14 @@ protected int getIntArg(GraphQLDirective directive, String argName) {
66
84
return value .intValue ();
67
85
}
68
86
87
+ /**
88
+ * Returns an String argument from a directive (or its default) and throws an assertion of the argument is null
89
+ *
90
+ * @param directive the directive to check
91
+ * @param argName the argument name
92
+ *
93
+ * @return a non null value
94
+ */
69
95
protected String getStrArg (GraphQLDirective directive , String argName ) {
70
96
GraphQLArgument argument = directive .getArgument (argName );
71
97
if (argument == null ) {
@@ -81,6 +107,14 @@ protected String getStrArg(GraphQLDirective directive, String argName) {
81
107
return value ;
82
108
}
83
109
110
+ /**
111
+ * Returns an boolean argument from a directive (or its default) and throws an assertion of the argument is null
112
+ *
113
+ * @param directive the directive to check
114
+ * @param argName the argument name
115
+ *
116
+ * @return a non null value
117
+ */
84
118
protected boolean getBoolArg (GraphQLDirective directive , String argName ) {
85
119
GraphQLArgument argument = directive .getArgument (argName );
86
120
if (argument == null ) {
@@ -96,6 +130,14 @@ protected boolean getBoolArg(GraphQLDirective directive, String argName) {
96
130
return Boolean .parseBoolean (String .valueOf (value ));
97
131
}
98
132
133
+ /**
134
+ * Returns the "message : String" argument from a directive or makes up one
135
+ * called "graphql.validation.{name}.message"
136
+ *
137
+ * @param directive the directive to check
138
+ *
139
+ * @return a non null value
140
+ */
99
141
protected String getMessageTemplate (GraphQLDirective directive ) {
100
142
String msg = null ;
101
143
GraphQLArgument arg = directive .getArgument ("message" );
@@ -111,6 +153,13 @@ protected String getMessageTemplate(GraphQLDirective directive) {
111
153
return msg ;
112
154
}
113
155
156
+ /**
157
+ * Creates a map of named parameters for message interpolation
158
+ *
159
+ * @param args must be even with a String as even params and values as odd params
160
+ *
161
+ * @return a map of message parameters
162
+ */
114
163
protected Map <String , Object > mkMessageParams (Object ... args ) {
115
164
Assert .assertTrue (args .length % 2 == 0 , "You MUST pass in an even number of arguments" );
116
165
Map <String , Object > params = new LinkedHashMap <>();
@@ -124,25 +173,51 @@ protected Map<String, Object> mkMessageParams(Object... args) {
124
173
return params ;
125
174
}
126
175
176
+ /**
177
+ * Creates a new {@link graphql.GraphQLError}
178
+ *
179
+ * @param ruleEnvironment the current rules environment
180
+ * @param directive the directive being run
181
+ * @param msgParams the map of parameters
182
+ *
183
+ * @return a list of a single error
184
+ */
127
185
protected List <GraphQLError > mkError (ValidationRuleEnvironment ruleEnvironment , GraphQLDirective directive , Map <String , Object > msgParams ) {
128
186
String messageTemplate = getMessageTemplate (directive );
129
187
GraphQLError error = ruleEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , ruleEnvironment );
130
188
return singletonList (error );
131
189
}
132
190
133
- protected boolean isStringOrListOrMap (GraphQLInputType argumentType ) {
134
- GraphQLInputType unwrappedType = Util .unwrapOneAndAllNonNull (argumentType );
191
+ /**
192
+ * Return true if the type is a String or List type or {@link graphql.schema.GraphQLInputObjectType}, regardless of non null ness
193
+ *
194
+ * @param inputType the type to check
195
+ *
196
+ * @return true if one of the above
197
+ */
198
+ protected boolean isStringOrListOrMap (GraphQLInputType inputType ) {
199
+ GraphQLInputType unwrappedType = Util .unwrapOneAndAllNonNull (inputType );
135
200
return Scalars .GraphQLString .equals (unwrappedType ) ||
136
- isList (argumentType ) ||
201
+ isList (inputType ) ||
137
202
(unwrappedType instanceof GraphQLInputObjectType );
138
203
}
139
204
205
+ /**
206
+ * Casts the object as a Map with an assertion of it is not one
207
+ *
208
+ * @return a Map
209
+ */
140
210
@ SuppressWarnings ("ConstantConditions" )
141
211
protected Map asMap (Object value ) {
142
212
Assert .assertTrue (value instanceof Map , "The argument value MUST be a Map value" );
143
213
return (Map ) value ;
144
214
}
145
215
216
+ /**
217
+ * Makes the object a BigDecimal with an assertion if we have no conversion of it
218
+ *
219
+ * @return a BigDecimal
220
+ */
146
221
protected BigDecimal asBigDecimal (Object value ) throws NumberFormatException {
147
222
if (value == null ) {
148
223
return Assert .assertShouldNeverHappen ("Validation cant handle null objects BigDecimals" );
@@ -161,6 +236,11 @@ protected BigDecimal asBigDecimal(Object value) throws NumberFormatException {
161
236
return new BigDecimal (bdStr );
162
237
}
163
238
239
+ /**
240
+ * Makes the object a boolean with an assertion if we have no conversion of it
241
+ *
242
+ * @return a boolean
243
+ */
164
244
protected boolean asBoolean (Object value ) {
165
245
if (value == null ) {
166
246
return Assert .assertShouldNeverHappen ("Validation cant handle null objects Booleans" );
@@ -172,16 +252,24 @@ protected boolean asBoolean(Object value) {
172
252
}
173
253
}
174
254
175
- protected int getStringOrObjectOrMapLength (GraphQLInputType inputType , Object argumentValue ) {
255
+ /**
256
+ * Returns the length of a String of the size of a list or size of a Map
257
+ *
258
+ * @param inputType the input type
259
+ * @param value the value
260
+ *
261
+ * @return the length of a String or Map or List
262
+ */
263
+ protected int getStringOrObjectOrMapLength (GraphQLInputType inputType , Object value ) {
176
264
int valLen ;
177
- if (argumentValue == null ) {
265
+ if (value == null ) {
178
266
valLen = 0 ;
179
267
} else if (Scalars .GraphQLString .equals (Util .unwrapNonNull (inputType ))) {
180
- valLen = String .valueOf (argumentValue ).length ();
268
+ valLen = String .valueOf (value ).length ();
181
269
} else if (isList (inputType )) {
182
- valLen = getListLength (argumentValue );
270
+ valLen = getListLength (value );
183
271
} else {
184
- valLen = getObjectLen (argumentValue );
272
+ valLen = getObjectLen (value );
185
273
}
186
274
return valLen ;
187
275
}
0 commit comments