8
8
import graphql .validation .constraints .Documentation ;
9
9
import graphql .validation .rules .ValidationEnvironment ;
10
10
11
+ import java .util .Arrays ;
11
12
import java .util .HashMap ;
12
13
import java .util .List ;
13
14
import java .util .Map ;
14
15
import java .util .regex .Matcher ;
15
16
import java .util .regex .Pattern ;
16
17
18
+ import static graphql .schema .GraphQLTypeUtil .isList ;
17
19
import static java .util .Collections .emptyList ;
18
20
19
21
public class PatternConstraint extends AbstractDirectiveConstraint {
@@ -31,9 +33,9 @@ public Documentation getDocumentation() {
31
33
32
34
.description ("The String must match the specified regular expression, which follows the Java regular expression conventions." )
33
35
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" )
35
37
36
- .applicableTypeNames (Scalars .GraphQLString .getName ())
38
+ .applicableTypeNames (Scalars .GraphQLString .getName (), "Lists" )
37
39
38
40
.directiveSDL ("directive @Pattern(regexp : String! =\" .*\" , message : String = \" %s\" ) " +
39
41
"on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION" ,
@@ -44,28 +46,39 @@ public Documentation getDocumentation() {
44
46
@ Override
45
47
public boolean appliesToType (GraphQLInputType inputType ) {
46
48
return isOneOfTheseTypes (inputType ,
47
- Scalars .GraphQLString
48
- );
49
+ Scalars .GraphQLString ) || isList (inputType );
49
50
}
50
51
51
52
@ Override
52
53
protected List <GraphQLError > runConstraint (ValidationEnvironment validationEnvironment ) {
53
54
Object validatedValue = validationEnvironment .getValidatedValue ();
55
+ GraphQLInputType argumentType = validationEnvironment .getValidatedType ();
56
+
54
57
if (validatedValue == null ) {
55
58
return emptyList ();
56
59
}
57
- String strValue = String .valueOf (validatedValue );
58
60
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 );
60
73
61
- String patternArg = getStrArg (directive , "regexp" );
62
- Pattern pattern = cachedPattern (patternArg );
74
+ String patternArg = getStrArg (directive , "regexp" );
75
+ Pattern pattern = cachedPattern (patternArg );
63
76
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
+ }
69
82
}
70
83
return emptyList ();
71
84
}
0 commit comments