Skip to content

refactor: skip running validation on null value #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Extended Validation for graphql-java

[![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)  
[![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)
[![MIT licensed](https://img.shields.io/badge/license-MIT-green)](https://github.com/graphql-java/graphql-java-extended-validation/blob/master/LICENSE)  


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -112,11 +113,14 @@ public List<GraphQLError> runValidation(ValidationEnvironment validationEnvironm
}

Object validatedValue = validationEnvironment.getValidatedValue();

//
// all the directives validation code does NOT care for NULL ness since the graphql engine covers that.
// eg a @NonNull validation directive makes no sense in graphql like it might in Java
//
if (validatedValue == null) {
return Collections.emptyList();
}

GraphQLInputType inputType = Util.unwrapNonNull(validationEnvironment.getValidatedType());
validationEnvironment = validationEnvironment.transform(b -> b.validatedType(inputType));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class NoEmptyBlankConstraintTest extends BaseConstraintTestSupport {
'field( arg : String @NotBlank ) : ID' | "\t\n\r " | 'NotBlank;path=/arg;val:\t\n\r ;\t'
'field( arg : String @NotBlank ) : ID' | "" | 'NotBlank;path=/arg;val:;\t'
'field( arg : String @NotBlank ) : ID' | "\t\n\r X" | ''
'field( arg : String @NotBlank ) : ID' | null | 'NotBlank;path=/arg;val:null;\t'
'field( arg : String @NotBlank ) : ID' | null | ''

// IDs
'field( arg : ID @NotBlank ) : ID' | "\t\n\r " | 'NotBlank;path=/arg;val:\t\n\r ;\t'
'field( arg : ID @NotBlank ) : ID' | "" | 'NotBlank;path=/arg;val:;\t'
'field( arg : ID @NotBlank ) : ID' | "\t\n\r X" | ''
'field( arg : ID @NotBlank ) : ID' | null | 'NotBlank;path=/arg;val:null;\t'
'field( arg : ID @NotBlank ) : ID' | null | ''
}

@Unroll
Expand All @@ -48,28 +48,28 @@ class NoEmptyBlankConstraintTest extends BaseConstraintTestSupport {
fieldDeclaration | argVal | expectedMessage
// strings
'field( arg : String @NotEmpty ) : ID' | "" | 'NotEmpty;path=/arg;val:;\t'
'field( arg : String @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
'field( arg : String @NotEmpty ) : ID' | null | ''
'field( arg : String @NotEmpty ) : ID' | "\t\n\r" | ''
'field( arg : String @NotEmpty ) : ID' | "ABC" | ''

// IDs
'field( arg : ID @NotEmpty ) : ID' | "" | 'NotEmpty;path=/arg;val:;\t'
'field( arg : ID @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
'field( arg : ID @NotEmpty ) : ID' | null | ''
'field( arg : ID @NotEmpty ) : ID' | "\t\n\r" | ''
'field( arg : ID @NotEmpty ) : ID' | "ABC" | ''


// objects
'field( arg : InputObject @NotEmpty ) : ID' | [:] | 'NotEmpty;path=/arg;val:[:];\t'
'field( arg : InputObject @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
'field( arg : InputObject @NotEmpty ) : ID' | null | ''
'field( arg : InputObject @NotEmpty ) : ID' | [name: "x"] | ''

// lists
'field( arg : [String] @NotEmpty ) : ID' | [] | 'NotEmpty;path=/arg;val:[];\t'
'field( arg : [String] @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
'field( arg : [String] @NotEmpty ) : ID' | null | ''
'field( arg : [String] @NotEmpty ) : ID' | ["x"] | ''
'field( arg : [ID] @NotEmpty ) : ID' | [] | 'NotEmpty;path=/arg;val:[];\t'
'field( arg : [ID] @NotEmpty ) : ID' | null | 'NotEmpty;path=/arg;val:null;\t'
'field( arg : [ID] @NotEmpty ) : ID' | null | ''
'field( arg : [ID] @NotEmpty ) : ID' | ["x"] | ''

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class SizeConstraintTest extends BaseConstraintTestSupport {
"field( arg : String @Size(max : 10, min : 5) ) : ID" | "123" | "Size;path=/arg;val:123;\t"

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

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

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