Skip to content

Commit e30f265

Browse files
committed
Renamed most things - directive rules are not Constraints
1 parent 0e75b75 commit e30f265

File tree

56 files changed

+976
-558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+976
-558
lines changed

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ repositories {
3939

4040
dependencies {
4141
compile "com.graphql-java:graphql-java:13.0"
42+
compile 'org.slf4j:slf4j-api:1.7.25'
4243
compile "javax.validation:validation-api:2.0.1.Final"
43-
//compile "org.hibernate.validator:hibernate-validator:6.0.17.Final"
44-
compile "org.hibernate.validator:hibernate-validator:6.1.0.Alpha5"
44+
compile "org.hibernate.validator:hibernate-validator:6.0.17.Final"
45+
//compile "org.hibernate.validator:hibernate-validator:6.1.0.Alpha5"
4546
compile "javax.el:javax.el-api:3.0.0"
4647
compile "org.glassfish:javax.el:3.0.0"
4748

49+
testCompile 'org.slf4j:slf4j-simple:1.7.25'
4850
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
4951
testCompile 'org.codehaus.groovy:groovy-all:2.5.7'
5052
}

src/main/java/graphql/validation/directives/AbstractDirectiveValidationRule.java renamed to src/main/java/graphql/validation/constraints/AbstractDirectiveConstraint.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graphql.validation.directives;
1+
package graphql.validation.constraints;
22

33
import graphql.Assert;
44
import graphql.GraphQLError;
@@ -12,7 +12,7 @@
1212
import graphql.schema.GraphQLInputType;
1313
import graphql.schema.GraphQLScalarType;
1414
import graphql.schema.GraphQLTypeUtil;
15-
import graphql.validation.rules.ValidationRuleEnvironment;
15+
import graphql.validation.rules.ValidationEnvironment;
1616
import graphql.validation.util.Util;
1717

1818
import java.lang.reflect.Array;
@@ -26,19 +26,31 @@
2626
import 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) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package graphql.validation.constraints;
2+
3+
import graphql.PublicSpi;
4+
import graphql.validation.rules.ValidationRule;
5+
6+
import java.util.List;
7+
8+
/**
9+
* A DirectiveConstraint is a specialised form of validation rule
10+
* that assumes it is backed by a SDL directive on fields, field arguments
11+
* or input type fields.
12+
*/
13+
@PublicSpi
14+
public interface DirectiveConstraint extends ValidationRule {
15+
16+
String getName();
17+
18+
String getDescription();
19+
20+
String getDirectiveDeclarationSDL();
21+
22+
String getMessageTemplate();
23+
24+
List<String> getApplicableTypeNames();
25+
26+
}

0 commit comments

Comments
 (0)