Skip to content

Adding Lists support for @NotBlank validation directive #43

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
May 6, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ The String must contain at least one non-whitespace character, according to Java

- Example : `updateAccident( accidentNotes : String @NotBlank) : DriverDetails`

- Applies to : `String`, `ID`
- Applies to : `String`, `ID`, `Lists`

- SDL : `directive @NotBlank(message : String = "graphql.validation.NotBlank.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ protected List<GraphQLError> mkError(ValidationEnvironment validationEnvironment
return singletonList(error);
}

/**
* Return true if the type is a String or ID or List type, regardless of non null ness
*
* @param inputType the type to check
*
* @return true if one of the above
*/
protected boolean isStringOrIDOrList(GraphQLInputType inputType) {
return isStringOrID(inputType) ||
isList(inputType);
}

/**
* Return true if the type is a String or ID or List type or {@link graphql.schema.GraphQLInputObjectType}, regardless of non null ness
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package graphql.validation.constraints.standard;

import static graphql.schema.GraphQLTypeUtil.isList;

import graphql.GraphQLError;
import graphql.Scalars;
import graphql.schema.GraphQLDirective;
Expand All @@ -8,6 +10,7 @@
import graphql.validation.constraints.Documentation;
import graphql.validation.rules.ValidationEnvironment;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -26,7 +29,7 @@ public Documentation getDocumentation() {

.example("updateAccident( accidentNotes : String @NotBlank) : DriverDetails")

.applicableTypeNames(Scalars.GraphQLString.getName(), Scalars.GraphQLID.getName())
.applicableTypeNames(Scalars.GraphQLString.getName(), Scalars.GraphQLID.getName(), "Lists")

.directiveSDL("directive @NotBlank(message : String = \"%s\") " +
"on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION",
Expand All @@ -36,19 +39,38 @@ public Documentation getDocumentation() {

@Override
public boolean appliesToType(GraphQLInputType inputType) {
return isStringOrID(inputType);
return isStringOrIDOrList(inputType);
}

@Override
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
Object validatedValue = validationEnvironment.getValidatedValue();
GraphQLInputType argumentType = validationEnvironment.getValidatedType();

GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);

if (validatedValue == null || isBlank(validatedValue)) {
if (validatedValue == null) {
return mkError(validationEnvironment, directive, mkMessageParams(validatedValue, validationEnvironment));
}

List<Object> validatedValues;

if (isList(argumentType)) {
validatedValues = (List)validatedValue;
} else {
validatedValues = Arrays.asList(validatedValue);
}

if (validatedValues.size() <= 0) {
return mkError(validationEnvironment, directive, mkMessageParams(validatedValue, validationEnvironment));
}

for (Object value : validatedValues) {
if (isBlank(value)) {
return mkError(validationEnvironment, directive, mkMessageParams(validatedValue, validationEnvironment));
}
}

return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class NoEmptyBlankConstraintTest extends BaseConstraintTestSupport {
'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 | ''

// Lists
'field( arg : [String] @NotBlank ) : ID' | [] | 'NotBlank;path=/arg;val:[];\t'
'field( arg : [String] @NotBlank ) : ID' | null | ''
'field( arg : [String] @NotBlank ) : ID' | ["x"] | ''
'field( arg : [String] @NotBlank ) : ID' | ["x", "y"] | ''
'field( arg : [String] @NotBlank ) : ID' | ["x", " "] | 'NotBlank;path=/arg;val:[x, ];\t'
'field( arg : [ID] @NotBlank ) : ID' | [] | 'NotBlank;path=/arg;val:[];\t'
'field( arg : [ID] @NotBlank ) : ID' | null | ''
'field( arg : [ID] @NotBlank ) : ID' | ["x"] | ''
'field( arg : [ID] @NotBlank ) : ID' | ["x", "y"] | ''
'field( arg : [String] @NotBlank ) : ID' | ["x", " "] | 'NotBlank;path=/arg;val:[x, ];\t'
}

@Unroll
Expand Down