Skip to content

Commit 0a8faf7

Browse files
committed
Aligned the naming to be like Hibernate validator
1 parent 736f604 commit 0a8faf7

18 files changed

+385
-63
lines changed

TODO.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,31 @@ can we do a validation rule where we require `first`+`after` or `last` + `before
1010

1111
Currently the rules are single argument specific - we would need field specific rules
1212

13+
# naming
14+
15+
Since we take inspiration from javax validation perhaps we should use
16+
the name Constraint especially for the @directives rules
17+
18+
# i18n
19+
20+
We rally should look at using the Hibernate message interpolation code
21+
(by building a bridge between our own) and get I18n and EL in messages
22+
working for near free
23+
24+
https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#preface
25+
26+
27+
https://github.com/hibernate/hibernate-validator
28+
29+
# Allowing Java EL
30+
31+
Can we allow Java EL so that we can have expressions in rules
32+
33+
```
34+
${validatedValue.length()} > 50
35+
```
36+
37+
Does this makes sense? What can we do to make this more powerful?
38+
39+
Hibernate Validator uses a form of this to do message interpolation
40+

build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ repositories {
4040
dependencies {
4141
compile "com.graphql-java:graphql-java:12.0"
4242
compile "javax.validation:validation-api:2.0.1.Final"
43-
compile "org.hibernate.validator:hibernate-validator:6.0.10.Final"
43+
//compile "org.hibernate.validator:hibernate-validator:6.0.17.Final"
44+
compile "org.hibernate.validator:hibernate-validator:6.1.0.Alpha5"
4445
compile "javax.el:javax.el-api:3.0.0"
45-
compile "org.glassfish.web:javax.el:2.2.6"
46-
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
46+
compile "org.glassfish:javax.el:3.0.0"
47+
48+
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
4749
testCompile 'org.codehaus.groovy:groovy-all:2.5.7'
4850
}
4951

src/main/java/graphql/validation/directives/DirectiveValidationRules.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import graphql.validation.directives.standardrules.PatternRule;
3030
import graphql.validation.directives.standardrules.PositiveOrZeroRule;
3131
import graphql.validation.directives.standardrules.PositiveRule;
32+
import graphql.validation.directives.standardrules.RangeRule;
3233
import graphql.validation.directives.standardrules.SizeRule;
3334
import graphql.validation.rules.ValidationRule;
3435
import graphql.validation.rules.ValidationRuleEnvironment;
@@ -63,6 +64,7 @@ public class DirectiveValidationRules implements ValidationRule {
6364
new PatternRule(),
6465
new PositiveOrZeroRule(),
6566
new PositiveRule(),
67+
new RangeRule(),
6668
new SizeRule()
6769
);
6870

@@ -114,7 +116,7 @@ private void assertDirectiveOnTheRightType(DirectiveValidationRule directiveRule
114116
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
115117

116118
GraphQLArgument argument = ruleEnvironment.getArgument();
117-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
119+
Object validatedValue = ruleEnvironment.getValidatedValue();
118120
List<GraphQLDirective> directives = argument.getDirectives();
119121
if (directives.isEmpty()) {
120122
return Collections.emptyList();
@@ -127,11 +129,11 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
127129
GraphQLInputType inputType = Util.unwrapNonNull(ruleEnvironment.getFieldOrArgumentType());
128130
ruleEnvironment = ruleEnvironment.transform(b -> b.fieldOrArgumentType(inputType));
129131

130-
return runValidationImpl(ruleEnvironment, inputType, argumentValue, directives);
132+
return runValidationImpl(ruleEnvironment, inputType, validatedValue, directives);
131133
}
132134

133135
@SuppressWarnings("unchecked")
134-
private List<GraphQLError> runValidationImpl(ValidationRuleEnvironment ruleEnvironment, GraphQLInputType inputType, Object argumentValue, List<GraphQLDirective> directives) {
136+
private List<GraphQLError> runValidationImpl(ValidationRuleEnvironment ruleEnvironment, GraphQLInputType inputType, Object validatedValue, List<GraphQLDirective> directives) {
135137
List<GraphQLError> errors = new ArrayList<>();
136138
for (GraphQLDirective directive : directives) {
137139
DirectiveValidationRule validationRule = directiveRules.get(directive.getName());
@@ -150,21 +152,21 @@ private List<GraphQLError> runValidationImpl(ValidationRuleEnvironment ruleEnvir
150152
errors.addAll(ruleErrors);
151153
}
152154

153-
if (argumentValue == null) {
155+
if (validatedValue == null) {
154156
return errors;
155157
}
156158

157159
inputType = (GraphQLInputType) GraphQLTypeUtil.unwrapNonNull(inputType);
158160

159161
if (GraphQLTypeUtil.isList(inputType)) {
160-
List<Object> values = new ArrayList<>(FpKit.toCollection(argumentValue));
162+
List<Object> values = new ArrayList<>(FpKit.toCollection(validatedValue));
161163
List<GraphQLError> ruleErrors = walkListArg(ruleEnvironment, (GraphQLList) inputType, values);
162164
errors.addAll(ruleErrors);
163165
}
164166

165167
if (inputType instanceof GraphQLInputObjectType) {
166-
if (argumentValue instanceof Map) {
167-
Map<String, Object> objectValue = (Map<String, Object>) argumentValue;
168+
if (validatedValue instanceof Map) {
169+
Map<String, Object> objectValue = (Map<String, Object>) validatedValue;
168170
List<GraphQLError> ruleErrors = walkObjectArg(ruleEnvironment, (GraphQLInputObjectType) inputType, objectValue);
169171
errors.addAll(ruleErrors);
170172
} else {
@@ -181,20 +183,20 @@ private List<GraphQLError> walkObjectArg(ValidationRuleEnvironment ruleEnvironme
181183

182184
GraphQLInputType fieldType = inputField.getType();
183185
List<GraphQLDirective> directives = inputField.getDirectives();
184-
Object argumentValue = objectMap.getOrDefault(inputField.getName(), inputField.getDefaultValue());
185-
if (argumentValue == null) {
186+
Object validatedValue = objectMap.getOrDefault(inputField.getName(), inputField.getDefaultValue());
187+
if (validatedValue == null) {
186188
continue;
187189
}
188190

189191
ExecutionPath fieldOrArgPath = ruleEnvironment.getFieldOrArgumentPath().segment(inputField.getName());
190192

191193
ValidationRuleEnvironment newRuleEnvironment = ruleEnvironment.transform(builder -> builder
192194
.fieldOrArgumentPath(fieldOrArgPath)
193-
.fieldOrArgumentValue(argumentValue)
195+
.validatedValue(validatedValue)
194196
.fieldOrArgumentType(fieldType)
195197
);
196198

197-
List<GraphQLError> ruleErrors = runValidationImpl(newRuleEnvironment, fieldType, argumentValue, directives);
199+
List<GraphQLError> ruleErrors = runValidationImpl(newRuleEnvironment, fieldType, validatedValue, directives);
198200
errors.addAll(ruleErrors);
199201
}
200202
return errors;
@@ -217,7 +219,7 @@ private List<GraphQLError> walkListArg(ValidationRuleEnvironment ruleEnvironment
217219

218220
ValidationRuleEnvironment newRuleEnvironment = ruleEnvironment.transform(builder -> builder
219221
.fieldOrArgumentPath(fieldOrArgPath)
220-
.fieldOrArgumentValue(value)
222+
.validatedValue(value)
221223
.fieldOrArgumentType(listItemType)
222224
);
223225

src/main/java/graphql/validation/directives/standardrules/AbstractAssertRule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ public List<String> getApplicableTypeNames() {
3636

3737
@Override
3838
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
39-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
39+
Object validatedValue = ruleEnvironment.getValidatedValue();
4040
//null values are valid
41-
if (argumentValue == null) {
41+
if (validatedValue == null) {
4242
return Collections.emptyList();
4343
}
4444

4545
GraphQLDirective directive = ruleEnvironment.getContextObject(GraphQLDirective.class);
4646

47-
boolean isTrue = asBoolean(argumentValue);
47+
boolean isTrue = asBoolean(validatedValue);
4848
if (!isOK(isTrue)) {
4949
return mkError(ruleEnvironment, directive, mkMessageParams(
50-
"fieldOrArgumentValue", argumentValue));
50+
"validatedValue", validatedValue));
5151

5252
}
5353
return Collections.emptyList();

src/main/java/graphql/validation/directives/standardrules/AbstractDecimalMinMaxRule.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public List<String> getApplicableTypeNames() {
5050

5151
@Override
5252
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
53-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
53+
Object validatedValue = ruleEnvironment.getValidatedValue();
5454
//null values are valid
55-
if (argumentValue == null) {
55+
if (validatedValue == null) {
5656
return Collections.emptyList();
5757
}
5858

@@ -63,7 +63,7 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
6363
boolean isOK;
6464
try {
6565
BigDecimal directiveBD = new BigDecimal(value);
66-
BigDecimal argBD = asBigDecimal(argumentValue);
66+
BigDecimal argBD = asBigDecimal(validatedValue);
6767
int comparisonResult = argBD.compareTo(directiveBD);
6868
isOK = isOK(inclusive, comparisonResult);
6969

@@ -73,9 +73,9 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
7373

7474
if (!isOK) {
7575
return mkError(ruleEnvironment, directive, mkMessageParams(
76-
"value", argumentValue,
76+
"value", validatedValue,
7777
"inclusive", inclusive,
78-
"fieldOrArgumentValue", argumentValue));
78+
"validatedValue", validatedValue));
7979

8080
}
8181
return Collections.emptyList();

src/main/java/graphql/validation/directives/standardrules/AbstractMinMaxRule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public List<String> getApplicableTypeNames() {
5050

5151
@Override
5252
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
53-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
53+
Object validatedValue = ruleEnvironment.getValidatedValue();
5454
//null values are valid
55-
if (argumentValue == null) {
55+
if (validatedValue == null) {
5656
return Collections.emptyList();
5757
}
5858

@@ -62,7 +62,7 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
6262
boolean isOK;
6363
try {
6464
BigDecimal directiveBD = new BigDecimal(value);
65-
BigDecimal argBD = asBigDecimal(argumentValue);
65+
BigDecimal argBD = asBigDecimal(validatedValue);
6666
int comparisonResult = argBD.compareTo(directiveBD);
6767
isOK = isOK(comparisonResult);
6868

@@ -74,7 +74,7 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
7474
if (!isOK) {
7575
return mkError(ruleEnvironment, directive, mkMessageParams(
7676
"value", value,
77-
"fieldOrArgumentValue", argumentValue));
77+
"validatedValue", validatedValue));
7878

7979
}
8080
return Collections.emptyList();

src/main/java/graphql/validation/directives/standardrules/AbstractPositiveNegativeRule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ public List<String> getApplicableTypeNames() {
5050

5151
@Override
5252
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
53-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
53+
Object validatedValue = ruleEnvironment.getValidatedValue();
5454
//null values are valid
55-
if (argumentValue == null) {
55+
if (validatedValue == null) {
5656
return Collections.emptyList();
5757
}
5858

5959
GraphQLDirective directive = ruleEnvironment.getContextObject(GraphQLDirective.class);
6060

6161
boolean isOK;
6262
try {
63-
BigDecimal bigDecimal = asBigDecimal(argumentValue);
63+
BigDecimal bigDecimal = asBigDecimal(validatedValue);
6464
isOK = isOK(bigDecimal);
6565
} catch (NumberFormatException nfe) {
6666
isOK = false;
6767
}
6868

6969
if (!isOK) {
7070
return mkError(ruleEnvironment, directive, mkMessageParams(
71-
"fieldOrArgumentValue", argumentValue));
71+
"validatedValue", validatedValue));
7272

7373
}
7474
return Collections.emptyList();

src/main/java/graphql/validation/directives/standardrules/DigitsRule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public String getDescription() {
6363

6464
@Override
6565
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
66-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
67-
if (argumentValue == null) {
66+
Object validatedValue = ruleEnvironment.getValidatedValue();
67+
if (validatedValue == null) {
6868
return Collections.emptyList();
6969
}
7070

@@ -74,7 +74,7 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
7474

7575
boolean isOk;
7676
try {
77-
BigDecimal bigNum = asBigDecimal(argumentValue);
77+
BigDecimal bigNum = asBigDecimal(validatedValue);
7878
isOk = isOk(bigNum, maxIntegerLength, maxFractionLength);
7979
} catch (NumberFormatException e) {
8080
isOk = false;
@@ -84,7 +84,7 @@ public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironmen
8484
return mkError(ruleEnvironment, directive, mkMessageParams(
8585
"integer", maxIntegerLength,
8686
"fraction", maxFractionLength,
87-
"fieldOrArgumentValue", argumentValue));
87+
"validatedValue", validatedValue));
8888
}
8989
return Collections.emptyList();
9090
}

src/main/java/graphql/validation/directives/standardrules/NotBlankRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ public List<String> getApplicableTypeNames() {
4848

4949
@Override
5050
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
51-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
51+
Object validatedValue = ruleEnvironment.getValidatedValue();
5252

5353
GraphQLDirective directive = ruleEnvironment.getContextObject(GraphQLDirective.class);
5454

55-
if (argumentValue == null || isBlank(argumentValue)) {
55+
if (validatedValue == null || isBlank(validatedValue)) {
5656
return mkError(ruleEnvironment, directive, mkMessageParams(
57-
"fieldOrArgumentValue", argumentValue));
57+
"validatedValue", validatedValue));
5858

5959
}
6060
return Collections.emptyList();

src/main/java/graphql/validation/directives/standardrules/NotEmptyRule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ public List<String> getApplicableTypeNames() {
4242

4343
@Override
4444
public List<GraphQLError> runValidation(ValidationRuleEnvironment ruleEnvironment) {
45-
Object argumentValue = ruleEnvironment.getFieldOrArgumentValue();
45+
Object validatedValue = ruleEnvironment.getValidatedValue();
4646
GraphQLInputType argumentType = ruleEnvironment.getFieldOrArgumentType();
4747

4848
GraphQLDirective directive = ruleEnvironment.getContextObject(GraphQLDirective.class);
49-
int size = getStringOrObjectOrMapLength(argumentType, argumentValue);
49+
int size = getStringOrObjectOrMapLength(argumentType, validatedValue);
5050

5151
if (size <= 0) {
5252
return mkError(ruleEnvironment, directive, mkMessageParams(
5353
"size", size,
54-
"fieldOrArgumentValue", argumentValue));
54+
"validatedValue", validatedValue));
5555
}
5656
return Collections.emptyList();
5757
}

0 commit comments

Comments
 (0)