1
- package graphql .validation .directives ;
1
+ package graphql .validation .constraints ;
2
2
3
3
import graphql .Assert ;
4
4
import graphql .GraphQLError ;
12
12
import graphql .schema .GraphQLInputType ;
13
13
import graphql .schema .GraphQLScalarType ;
14
14
import graphql .schema .GraphQLTypeUtil ;
15
- import graphql .validation .rules .ValidationRuleEnvironment ;
15
+ import graphql .validation .rules .ValidationEnvironment ;
16
16
import graphql .validation .util .Util ;
17
17
18
18
import java .lang .reflect .Array ;
26
26
import static java .util .Collections .singletonList ;
27
27
28
28
@ PublicSpi
29
- public abstract class AbstractDirectiveValidationRule implements DirectiveValidationRule {
29
+ public abstract class AbstractDirectiveConstraint implements DirectiveConstraint {
30
30
31
31
private final String name ;
32
32
33
- public AbstractDirectiveValidationRule (String name ) {
33
+ public AbstractDirectiveConstraint (String name ) {
34
34
this .name = name ;
35
35
}
36
36
37
+
38
+ @ Override
39
+ public String toString () {
40
+ return "@" + name ;
41
+ }
42
+
37
43
@ Override
38
44
public String getName () {
39
45
return name ;
40
46
}
41
47
48
+
49
+ @ Override
50
+ public String getMessageTemplate () {
51
+ return "graphql.validation." + getName () + ".message" ;
52
+ }
53
+
42
54
@ Override
43
55
public boolean appliesTo (GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
44
56
return false ;
@@ -179,35 +191,39 @@ protected String getMessageTemplate(GraphQLDirective directive) {
179
191
/**
180
192
* Creates a map of named parameters for message interpolation
181
193
*
194
+ * @param validatedValue the value being validated
195
+ * @param ruleEnvironment the rule environment
182
196
* @param args must be even with a String as even params and values as odd params
183
197
*
184
198
* @return a map of message parameters
185
199
*/
186
- protected Map <String , Object > mkMessageParams (Object ... args ) {
187
- Assert .assertTrue (args .length % 2 == 0 , "You MUST pass in an even number of arguments" );
200
+ protected Map <String , Object > mkMessageParams (Object validatedValue , ValidationEnvironment ruleEnvironment , Object ... args ) {
188
201
Map <String , Object > params = new LinkedHashMap <>();
202
+ params .put ("validatedValue" , validatedValue );
203
+ params .put ("constraint" , getName ());
204
+
205
+ Assert .assertTrue (args .length % 2 == 0 , "You MUST pass in an even number of arguments" );
189
206
for (int ix = 0 ; ix < args .length ; ix = ix + 2 ) {
190
207
Object key = args [ix ];
191
- Assert .assertTrue (key instanceof String , "You MUST pass in a string key" );
208
+ Assert .assertTrue (key instanceof String , "You MUST pass in a message param string key" );
192
209
Object val = args [ix + 1 ];
193
210
params .put (String .valueOf (key ), val );
194
211
}
195
-
196
212
return params ;
197
213
}
198
214
199
215
/**
200
216
* Creates a new {@link graphql.GraphQLError}
201
217
*
202
- * @param ruleEnvironment the current rules environment
218
+ * @param validationEnvironment the current validation environment
203
219
* @param directive the directive being run
204
220
* @param msgParams the map of parameters
205
221
*
206
222
* @return a list of a single error
207
223
*/
208
- protected List <GraphQLError > mkError (ValidationRuleEnvironment ruleEnvironment , GraphQLDirective directive , Map <String , Object > msgParams ) {
224
+ protected List <GraphQLError > mkError (ValidationEnvironment validationEnvironment , GraphQLDirective directive , Map <String , Object > msgParams ) {
209
225
String messageTemplate = getMessageTemplate (directive );
210
- GraphQLError error = ruleEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , ruleEnvironment );
226
+ GraphQLError error = validationEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , validationEnvironment );
211
227
return singletonList (error );
212
228
}
213
229
@@ -228,6 +244,8 @@ protected boolean isStringOrListOrMap(GraphQLInputType inputType) {
228
244
/**
229
245
* Casts the object as a Map with an assertion of it is not one
230
246
*
247
+ * @param value the object to turn into a map
248
+ *
231
249
* @return a Map
232
250
*/
233
251
@ SuppressWarnings ("ConstantConditions" )
@@ -239,6 +257,8 @@ protected Map asMap(Object value) {
239
257
/**
240
258
* Makes the object a BigDecimal with an assertion if we have no conversion of it
241
259
*
260
+ * @param value the object to turn into a BigDecimal
261
+ *
242
262
* @return a BigDecimal
243
263
*/
244
264
protected BigDecimal asBigDecimal (Object value ) throws NumberFormatException {
@@ -262,6 +282,8 @@ protected BigDecimal asBigDecimal(Object value) throws NumberFormatException {
262
282
/**
263
283
* Makes the object a boolean with an assertion if we have no conversion of it
264
284
*
285
+ * @param value the boolean object
286
+ *
265
287
* @return a boolean
266
288
*/
267
289
protected boolean asBoolean (Object value ) {
0 commit comments