Skip to content

Commit 6617ef8

Browse files
authored
Adding Pattern support for Lists (#20)
* Adding Pattern support for Lists * Casting to List instead of ArrayList
1 parent 6f3b293 commit 6617ef8

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This library provides extended validation of fields and field arguments for [gra
1212
# Status
1313

1414
This code is currently under construction. It is fairly complete in providing powerful validation
15-
but as it has NOT be consumed by a production like project then its API usefulness has not been tested
15+
but as it has NOT been consumed by a production like project then its API usefulness has not been tested
1616
and battle tested.
1717

1818
But the project welcomes all feedback and input on code design and validation requirements.
@@ -409,7 +409,7 @@ The String must match the specified regular expression, which follows the Java r
409409

410410
- Example : `updateDriver( licencePlate : String @Patttern(regex : "[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]") : DriverDetails`
411411

412-
- Applies to : `String`
412+
- Applies to : `String`, `Lists`
413413

414414
- SDL : `directive @Pattern(regexp : String! =".*", message : String = "graphql.validation.Pattern.message") on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION`
415415

src/main/java/graphql/validation/constraints/standard/PatternConstraint.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import graphql.validation.constraints.Documentation;
99
import graphql.validation.rules.ValidationEnvironment;
1010

11+
import java.util.Arrays;
1112
import java.util.HashMap;
1213
import java.util.List;
1314
import java.util.Map;
1415
import java.util.regex.Matcher;
1516
import java.util.regex.Pattern;
1617

18+
import static graphql.schema.GraphQLTypeUtil.isList;
1719
import static java.util.Collections.emptyList;
1820

1921
public class PatternConstraint extends AbstractDirectiveConstraint {
@@ -31,9 +33,9 @@ public Documentation getDocumentation() {
3133

3234
.description("The String must match the specified regular expression, which follows the Java regular expression conventions.")
3335

34-
.example("updateDriver( licencePlate : String @Patttern(regex : \"[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]\") : DriverDetails")
36+
.example("updateDriver( licencePlate : String @Pattern(regexp : \"[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]\") : DriverDetails")
3537

36-
.applicableTypeNames(Scalars.GraphQLString.getName())
38+
.applicableTypeNames(Scalars.GraphQLString.getName(), "Lists")
3739

3840
.directiveSDL("directive @Pattern(regexp : String! =\".*\", message : String = \"%s\") " +
3941
"on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION",
@@ -44,28 +46,39 @@ public Documentation getDocumentation() {
4446
@Override
4547
public boolean appliesToType(GraphQLInputType inputType) {
4648
return isOneOfTheseTypes(inputType,
47-
Scalars.GraphQLString
48-
);
49+
Scalars.GraphQLString) || isList(inputType);
4950
}
5051

5152
@Override
5253
protected List<GraphQLError> runConstraint(ValidationEnvironment validationEnvironment) {
5354
Object validatedValue = validationEnvironment.getValidatedValue();
55+
GraphQLInputType argumentType = validationEnvironment.getValidatedType();
56+
5457
if (validatedValue == null) {
5558
return emptyList();
5659
}
57-
String strValue = String.valueOf(validatedValue);
5860

59-
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
61+
List<Object> validatedValues;
62+
63+
if (isList(argumentType)) {
64+
validatedValues = (List)validatedValue;
65+
} else {
66+
validatedValues = Arrays.asList(validatedValue);
67+
}
68+
69+
for (Object value : validatedValues) {
70+
String strValue = String.valueOf(value);
71+
72+
GraphQLDirective directive = validationEnvironment.getContextObject(GraphQLDirective.class);
6073

61-
String patternArg = getStrArg(directive, "regexp");
62-
Pattern pattern = cachedPattern(patternArg);
74+
String patternArg = getStrArg(directive, "regexp");
75+
Pattern pattern = cachedPattern(patternArg);
6376

64-
Matcher matcher = pattern.matcher(strValue);
65-
if (!matcher.matches()) {
66-
return mkError(validationEnvironment, directive, mkMessageParams(validatedValue, validationEnvironment,
67-
"regexp", patternArg
68-
));
77+
Matcher matcher = pattern.matcher(strValue);
78+
if (!matcher.matches()) {
79+
return mkError(validationEnvironment, directive,
80+
mkMessageParams(validatedValue, validationEnvironment, "regexp", patternArg));
81+
}
6982
}
7083
return emptyList();
7184
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PatternConstraintTest extends BaseConstraintTestSupport {
2222
fieldDeclaration | argVal | expectedMessage
2323
'field( arg : String @Pattern(regexp:"[A-Z]*") ) : ID' | "ABCd" | 'Pattern;path=/arg;val:ABCd;\t'
2424
'field( arg : String @Pattern(regexp:"[A-Z]*") ) : ID' | "ABC" | ''
25+
'field( arg : [String] @Pattern(regexp:"[A-Z]*") ) : ID' | ["ABC"] | ''
26+
'field( arg : [String] @Pattern(regexp:"[A-Z]*") ) : ID' | ["ABC", "ABCd"] | 'Pattern;path=/arg;val:[ABC, ABCd];\t'
2527

2628
// nulls are valid
2729
'field( arg : String @Pattern(regexp:"[A-Z]*") ) : ID' | null | ''

0 commit comments

Comments
 (0)