1- package graphql .validation .directives ;
1+ package graphql .validation .constraints ;
22
33import graphql .Assert ;
44import graphql .GraphQLError ;
1212import graphql .schema .GraphQLInputType ;
1313import graphql .schema .GraphQLScalarType ;
1414import graphql .schema .GraphQLTypeUtil ;
15- import graphql .validation .rules .ValidationRuleEnvironment ;
15+ import graphql .validation .rules .ValidationEnvironment ;
1616import graphql .validation .util .Util ;
1717
1818import java .lang .reflect .Array ;
2626import static java .util .Collections .singletonList ;
2727
2828@ PublicSpi
29- public abstract class AbstractDirectiveValidationRule implements DirectiveValidationRule {
29+ public abstract class AbstractDirectiveConstraint implements DirectiveConstraint {
3030
3131 private final String name ;
3232
33- public AbstractDirectiveValidationRule (String name ) {
33+ public AbstractDirectiveConstraint (String name ) {
3434 this .name = name ;
3535 }
3636
37+
38+ @ Override
39+ public String toString () {
40+ return "@" + name ;
41+ }
42+
3743 @ Override
3844 public String getName () {
3945 return name ;
4046 }
4147
48+
49+ @ Override
50+ public String getMessageTemplate () {
51+ return "graphql.validation." + getName () + ".message" ;
52+ }
53+
4254 @ Override
4355 public boolean appliesTo (GraphQLFieldDefinition fieldDefinition , GraphQLFieldsContainer fieldsContainer ) {
4456 return false ;
@@ -179,35 +191,39 @@ protected String getMessageTemplate(GraphQLDirective directive) {
179191 /**
180192 * Creates a map of named parameters for message interpolation
181193 *
194+ * @param validatedValue the value being validated
195+ * @param ruleEnvironment the rule environment
182196 * @param args must be even with a String as even params and values as odd params
183197 *
184198 * @return a map of message parameters
185199 */
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 ) {
188201 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" );
189206 for (int ix = 0 ; ix < args .length ; ix = ix + 2 ) {
190207 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" );
192209 Object val = args [ix + 1 ];
193210 params .put (String .valueOf (key ), val );
194211 }
195-
196212 return params ;
197213 }
198214
199215 /**
200216 * Creates a new {@link graphql.GraphQLError}
201217 *
202- * @param ruleEnvironment the current rules environment
218+ * @param validationEnvironment the current validation environment
203219 * @param directive the directive being run
204220 * @param msgParams the map of parameters
205221 *
206222 * @return a list of a single error
207223 */
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 ) {
209225 String messageTemplate = getMessageTemplate (directive );
210- GraphQLError error = ruleEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , ruleEnvironment );
226+ GraphQLError error = validationEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , validationEnvironment );
211227 return singletonList (error );
212228 }
213229
@@ -228,6 +244,8 @@ protected boolean isStringOrListOrMap(GraphQLInputType inputType) {
228244 /**
229245 * Casts the object as a Map with an assertion of it is not one
230246 *
247+ * @param value the object to turn into a map
248+ *
231249 * @return a Map
232250 */
233251 @ SuppressWarnings ("ConstantConditions" )
@@ -239,6 +257,8 @@ protected Map asMap(Object value) {
239257 /**
240258 * Makes the object a BigDecimal with an assertion if we have no conversion of it
241259 *
260+ * @param value the object to turn into a BigDecimal
261+ *
242262 * @return a BigDecimal
243263 */
244264 protected BigDecimal asBigDecimal (Object value ) throws NumberFormatException {
@@ -262,6 +282,8 @@ protected BigDecimal asBigDecimal(Object value) throws NumberFormatException {
262282 /**
263283 * Makes the object a boolean with an assertion if we have no conversion of it
264284 *
285+ * @param value the boolean object
286+ *
265287 * @return a boolean
266288 */
267289 protected boolean asBoolean (Object value ) {
0 commit comments