Skip to content

Commit f2dd5b7

Browse files
authored
Merge pull request #37 from aoudiamoncef/master
refactor: skip running validation on null value
2 parents 1499e2d + 65274bb commit f2dd5b7

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Extended Validation for graphql-java
22

33
[![Build Status](https://api.travis-ci.org/graphql-java/graphql-java-extended-validation.svg?branch=master)](https://api.travis-ci.org/graphql-java/graphql-java-extended-validation.svg?branch=master)  
4+
[![Download](https://api.bintray.com/packages/graphql-java/graphql-java/graphql-java-extended-validation/images/download.svg) ](https://bintray.com/graphql-java/graphql-java/graphql-java-extended-validation/_latestVersion)
45
[![MIT licensed](https://img.shields.io/badge/license-MIT-green)](https://github.com/graphql-java/graphql-java-extended-validation/blob/master/LICENSE)  
56

67

src/main/java/graphql/validation/constraints/AbstractDirectiveConstraint.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.math.BigDecimal;
2222
import java.util.ArrayList;
2323
import java.util.Collection;
24+
import java.util.Collections;
2425
import java.util.LinkedHashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -112,11 +113,14 @@ public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironm
112113
}
113114

114115
Object validatedValue = validationEnvironment.getValidatedValue();
115-
116116
//
117117
// all the directives validation code does NOT care for NULL ness since the graphql engine covers that.
118118
// eg a @NonNull validation directive makes no sense in graphql like it might in Java
119119
//
120+
if (validatedValue == null) {
121+
return Collections.emptyList();
122+
}
123+
120124
GraphQLInputType inputType = Util.unwrapNonNull(validationEnvironment.getValidatedType());
121125
validationEnvironment = validationEnvironment.transform(b -> b.validatedType(inputType));
122126

src/test/groovy/graphql/validation/constraints/standard/NoEmptyBlankConstraintTest.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class NoEmptyBlankConstraintTest extends BaseConstraintTestSupport {
2424
'field( arg : String @NotBlank ) : ID' | "\t\n\r " | 'NotBlank;path=/arg;val:\t\n\r ;\t'
2525
'field( arg : String @NotBlank ) : ID' | "" | 'NotBlank;path=/arg;val:;\t'
2626
'field( arg : String @NotBlank ) : ID' | "\t\n\r X" | ''
27-
'field( arg : String @NotBlank ) : ID' | null | 'NotBlank;path=/arg;val:null;\t'
27+
'field( arg : String @NotBlank ) : ID' | null | ''
2828

2929
// IDs
3030
'field( arg : ID @NotBlank ) : ID' | "\t\n\r " | 'NotBlank;path=/arg;val:\t\n\r ;\t'
3131
'field( arg : ID @NotBlank ) : ID' | "" | 'NotBlank;path=/arg;val:;\t'
3232
'field( arg : ID @NotBlank ) : ID' | "\t\n\r X" | ''
33-
'field( arg : ID @NotBlank ) : ID' | null | 'NotBlank;path=/arg;val:null;\t'
33+
'field( arg : ID @NotBlank ) : ID' | null | ''
3434
}
3535

3636
@Unroll
@@ -48,28 +48,28 @@ class NoEmptyBlankConstraintTest extends BaseConstraintTestSupport {
4848
fieldDeclaration | argVal | expectedMessage
4949
// strings
5050
'field( arg : String @NotEmpty ) : ID' | "" | 'NotEmpty;path=/arg;val:;\t'
51-
'field( arg : String @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
51+
'field( arg : String @NotEmpty ) : ID' | null | ''
5252
'field( arg : String @NotEmpty ) : ID' | "\t\n\r" | ''
5353
'field( arg : String @NotEmpty ) : ID' | "ABC" | ''
5454

5555
// IDs
5656
'field( arg : ID @NotEmpty ) : ID' | "" | 'NotEmpty;path=/arg;val:;\t'
57-
'field( arg : ID @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
57+
'field( arg : ID @NotEmpty ) : ID' | null | ''
5858
'field( arg : ID @NotEmpty ) : ID' | "\t\n\r" | ''
5959
'field( arg : ID @NotEmpty ) : ID' | "ABC" | ''
6060

6161

6262
// objects
6363
'field( arg : InputObject @NotEmpty ) : ID' | [:] | 'NotEmpty;path=/arg;val:[:];\t'
64-
'field( arg : InputObject @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
64+
'field( arg : InputObject @NotEmpty ) : ID' | null | ''
6565
'field( arg : InputObject @NotEmpty ) : ID' | [name: "x"] | ''
6666

6767
// lists
6868
'field( arg : [String] @NotEmpty ) : ID' | [] | 'NotEmpty;path=/arg;val:[];\t'
69-
'field( arg : [String] @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
69+
'field( arg : [String] @NotEmpty ) : ID' | null | ''
7070
'field( arg : [String] @NotEmpty ) : ID' | ["x"] | ''
7171
'field( arg : [ID] @NotEmpty ) : ID' | [] | 'NotEmpty;path=/arg;val:[];\t'
72-
'field( arg : [ID] @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
72+
'field( arg : [ID] @NotEmpty ) : ID' | null | ''
7373
'field( arg : [ID] @NotEmpty ) : ID' | ["x"] | ''
7474

7575
}

src/test/groovy/graphql/validation/constraints/standard/SizeConstraintTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class SizeConstraintTest extends BaseConstraintTestSupport {
2626
"field( arg : String @Size(max : 10, min : 5) ) : ID" | "123" | "Size;path=/arg;val:123;\t"
2727

2828
'field( arg : String @Size(min : 5, message : "custom") ) : ID' | "123" | "custom;path=/arg;val:123;\t"
29-
"field( arg : String @Size(min : 5) ) : ID" | null | "Size;path=/arg;val:null;\t"
29+
"field( arg : String @Size(min : 5) ) : ID" | null | ""
3030

3131
//IDs
3232
"field( arg : ID @Size(max : 10) ) : ID" | "1234567891011" | "Size;path=/arg;val:1234567891011;\t"
3333
"field( arg : ID @Size(max : 100) ) : ID" | "1234567891011" | ""
3434
"field( arg : ID @Size(max : 10, min : 5) ) : ID" | "123" | "Size;path=/arg;val:123;\t"
3535

3636
'field( arg : ID @Size(min : 5, message : "custom") ) : ID' | "123" | "custom;path=/arg;val:123;\t"
37-
"field( arg : ID @Size(min : 5) ) : ID" | null | "Size;path=/arg;val:null;\t"
37+
"field( arg : ID @Size(min : 5) ) : ID" | null | ""
3838
}
3939
}

0 commit comments

Comments
 (0)