22
33import graphql .Assert ;
44import graphql .GraphQLError ;
5+ import graphql .PublicSpi ;
56import graphql .Scalars ;
67import graphql .schema .GraphQLArgument ;
78import graphql .schema .GraphQLDirective ;
2324import static graphql .schema .GraphQLTypeUtil .isList ;
2425import static java .util .Collections .singletonList ;
2526
27+ @ PublicSpi
2628public abstract class AbstractDirectiveValidationRule implements DirectiveValidationRule {
2729
2830 private final String name ;
@@ -41,6 +43,14 @@ public boolean appliesToType(GraphQLArgument argument, GraphQLFieldDefinition fi
4143 return appliesToType (Util .unwrapNonNull (argument .getType ()));
4244 }
4345
46+ /**
47+ * Returns true of the input type is one of the specified scalar types, regardless of non null ness
48+ *
49+ * @param inputType the type to check
50+ * @param scalarTypes the array of scalar types
51+ *
52+ * @return true ifits oneof them
53+ */
4454 protected boolean isOneOfTheseTypes (GraphQLInputType inputType , GraphQLScalarType ... scalarTypes ) {
4555 GraphQLInputType unwrappedType = Util .unwrapNonNull (inputType );
4656 for (GraphQLScalarType scalarType : scalarTypes ) {
@@ -51,6 +61,14 @@ protected boolean isOneOfTheseTypes(GraphQLInputType inputType, GraphQLScalarTyp
5161 return false ;
5262 }
5363
64+ /**
65+ * Returns an integer argument from a directive (or its default) and throws an assertion of the argument is null
66+ *
67+ * @param directive the directive to check
68+ * @param argName the argument name
69+ *
70+ * @return a non null value
71+ */
5472 protected int getIntArg (GraphQLDirective directive , String argName ) {
5573 GraphQLArgument argument = directive .getArgument (argName );
5674 if (argument == null ) {
@@ -66,6 +84,14 @@ protected int getIntArg(GraphQLDirective directive, String argName) {
6684 return value .intValue ();
6785 }
6886
87+ /**
88+ * Returns an String argument from a directive (or its default) and throws an assertion of the argument is null
89+ *
90+ * @param directive the directive to check
91+ * @param argName the argument name
92+ *
93+ * @return a non null value
94+ */
6995 protected String getStrArg (GraphQLDirective directive , String argName ) {
7096 GraphQLArgument argument = directive .getArgument (argName );
7197 if (argument == null ) {
@@ -81,6 +107,14 @@ protected String getStrArg(GraphQLDirective directive, String argName) {
81107 return value ;
82108 }
83109
110+ /**
111+ * Returns an boolean argument from a directive (or its default) and throws an assertion of the argument is null
112+ *
113+ * @param directive the directive to check
114+ * @param argName the argument name
115+ *
116+ * @return a non null value
117+ */
84118 protected boolean getBoolArg (GraphQLDirective directive , String argName ) {
85119 GraphQLArgument argument = directive .getArgument (argName );
86120 if (argument == null ) {
@@ -96,6 +130,14 @@ protected boolean getBoolArg(GraphQLDirective directive, String argName) {
96130 return Boolean .parseBoolean (String .valueOf (value ));
97131 }
98132
133+ /**
134+ * Returns the "message : String" argument from a directive or makes up one
135+ * called "graphql.validation.{name}.message"
136+ *
137+ * @param directive the directive to check
138+ *
139+ * @return a non null value
140+ */
99141 protected String getMessageTemplate (GraphQLDirective directive ) {
100142 String msg = null ;
101143 GraphQLArgument arg = directive .getArgument ("message" );
@@ -111,6 +153,13 @@ protected String getMessageTemplate(GraphQLDirective directive) {
111153 return msg ;
112154 }
113155
156+ /**
157+ * Creates a map of named parameters for message interpolation
158+ *
159+ * @param args must be even with a String as even params and values as odd params
160+ *
161+ * @return a map of message parameters
162+ */
114163 protected Map <String , Object > mkMessageParams (Object ... args ) {
115164 Assert .assertTrue (args .length % 2 == 0 , "You MUST pass in an even number of arguments" );
116165 Map <String , Object > params = new LinkedHashMap <>();
@@ -124,25 +173,51 @@ protected Map<String, Object> mkMessageParams(Object... args) {
124173 return params ;
125174 }
126175
176+ /**
177+ * Creates a new {@link graphql.GraphQLError}
178+ *
179+ * @param ruleEnvironment the current rules environment
180+ * @param directive the directive being run
181+ * @param msgParams the map of parameters
182+ *
183+ * @return a list of a single error
184+ */
127185 protected List <GraphQLError > mkError (ValidationRuleEnvironment ruleEnvironment , GraphQLDirective directive , Map <String , Object > msgParams ) {
128186 String messageTemplate = getMessageTemplate (directive );
129187 GraphQLError error = ruleEnvironment .getInterpolator ().interpolate (messageTemplate , msgParams , ruleEnvironment );
130188 return singletonList (error );
131189 }
132190
133- protected boolean isStringOrListOrMap (GraphQLInputType argumentType ) {
134- GraphQLInputType unwrappedType = Util .unwrapOneAndAllNonNull (argumentType );
191+ /**
192+ * Return true if the type is a String or List type or {@link graphql.schema.GraphQLInputObjectType}, regardless of non null ness
193+ *
194+ * @param inputType the type to check
195+ *
196+ * @return true if one of the above
197+ */
198+ protected boolean isStringOrListOrMap (GraphQLInputType inputType ) {
199+ GraphQLInputType unwrappedType = Util .unwrapOneAndAllNonNull (inputType );
135200 return Scalars .GraphQLString .equals (unwrappedType ) ||
136- isList (argumentType ) ||
201+ isList (inputType ) ||
137202 (unwrappedType instanceof GraphQLInputObjectType );
138203 }
139204
205+ /**
206+ * Casts the object as a Map with an assertion of it is not one
207+ *
208+ * @return a Map
209+ */
140210 @ SuppressWarnings ("ConstantConditions" )
141211 protected Map asMap (Object value ) {
142212 Assert .assertTrue (value instanceof Map , "The argument value MUST be a Map value" );
143213 return (Map ) value ;
144214 }
145215
216+ /**
217+ * Makes the object a BigDecimal with an assertion if we have no conversion of it
218+ *
219+ * @return a BigDecimal
220+ */
146221 protected BigDecimal asBigDecimal (Object value ) throws NumberFormatException {
147222 if (value == null ) {
148223 return Assert .assertShouldNeverHappen ("Validation cant handle null objects BigDecimals" );
@@ -161,6 +236,11 @@ protected BigDecimal asBigDecimal(Object value) throws NumberFormatException {
161236 return new BigDecimal (bdStr );
162237 }
163238
239+ /**
240+ * Makes the object a boolean with an assertion if we have no conversion of it
241+ *
242+ * @return a boolean
243+ */
164244 protected boolean asBoolean (Object value ) {
165245 if (value == null ) {
166246 return Assert .assertShouldNeverHappen ("Validation cant handle null objects Booleans" );
@@ -172,16 +252,24 @@ protected boolean asBoolean(Object value) {
172252 }
173253 }
174254
175- protected int getStringOrObjectOrMapLength (GraphQLInputType inputType , Object argumentValue ) {
255+ /**
256+ * Returns the length of a String of the size of a list or size of a Map
257+ *
258+ * @param inputType the input type
259+ * @param value the value
260+ *
261+ * @return the length of a String or Map or List
262+ */
263+ protected int getStringOrObjectOrMapLength (GraphQLInputType inputType , Object value ) {
176264 int valLen ;
177- if (argumentValue == null ) {
265+ if (value == null ) {
178266 valLen = 0 ;
179267 } else if (Scalars .GraphQLString .equals (Util .unwrapNonNull (inputType ))) {
180- valLen = String .valueOf (argumentValue ).length ();
268+ valLen = String .valueOf (value ).length ();
181269 } else if (isList (inputType )) {
182- valLen = getListLength (argumentValue );
270+ valLen = getListLength (value );
183271 } else {
184- valLen = getObjectLen (argumentValue );
272+ valLen = getObjectLen (value );
185273 }
186274 return valLen ;
187275 }
0 commit comments