Package graphql.validation.constraints
Class AbstractDirectiveConstraint
- java.lang.Object
-
- graphql.validation.constraints.AbstractDirectiveConstraint
-
- All Implemented Interfaces:
DirectiveConstraint,ValidationRule
- Direct Known Subclasses:
AbstractNotEmptyRule,AbstractSizeConstraint,AssertFalseConstraint,AssertTrueConstraint,DecimalMaxConstraint,DecimalMinConstraint,DigitsConstraint,ExpressionConstraint,MaxConstraint,MinConstraint,NegativeConstraint,NegativeOrZeroConstraint,NotBlankRule,PatternConstraint,PositiveConstraint,PositiveOrZeroConstraint,RangeConstraint
@PublicSpi public abstract class AbstractDirectiveConstraint extends java.lang.Object implements DirectiveConstraint
-
-
Constructor Summary
Constructors Constructor Description AbstractDirectiveConstraint(java.lang.String name)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanappliesTo(graphql.schema.GraphQLArgument argument, graphql.schema.GraphQLFieldDefinition fieldDefinition, graphql.schema.GraphQLFieldsContainer fieldsContainer)This is called to work out if this rule applies to the argument of a specified fieldbooleanappliesTo(graphql.schema.GraphQLFieldDefinition fieldDefinition, graphql.schema.GraphQLFieldsContainer fieldsContainer)This is called to work out if this rule applies to a specified fieldprotected abstract booleanappliesToListElements()protected abstract booleanappliesToType(graphql.schema.GraphQLInputType inputType)A derived class will be called to indicate whether this input type applies to the constraintprotected java.math.BigDecimalasBigDecimal(java.lang.Object value)Makes the object a BigDecimal with an assertion if we have no conversion of itprotected booleanasBoolean(java.lang.Object value)Makes the object a boolean with an assertion if we have no conversion of itprotected java.util.MapasMap(java.lang.Object value)Casts the object as a Map with an assertion of it is not oneprotected booleangetBoolArg(graphql.schema.GraphQLAppliedDirective directive, java.lang.String argName)Returns an boolean argument from a directive (or its default) and throws an assertion of the argument is nullprotected intgetIntArg(graphql.schema.GraphQLAppliedDirective directive, java.lang.String argName)Returns an integer argument from a directive (or its default) and throws an assertion of the argument is nullprotected java.lang.StringgetMessageTemplate()protected java.lang.StringgetMessageTemplate(graphql.schema.GraphQLAppliedDirective directive)Returns the "message : String" argument from a directive or makes up one called "graphql.validation.{name}.message"java.lang.StringgetName()protected java.lang.StringgetStrArg(graphql.schema.GraphQLAppliedDirective directive, java.lang.String argName)Returns an String argument from a directive (or its default) and throws an assertion of the argument is nullprotected intgetStringOrIDOrObjectOrMapLength(graphql.schema.GraphQLInputType inputType, java.lang.Object value)Returns the length of a String, ID, size of a list or size of a Mapprotected booleanisMap(graphql.schema.GraphQLInputType inputType)protected booleanisOneOfTheseTypes(graphql.schema.GraphQLInputType inputType, graphql.schema.GraphQLScalarType... scalarTypes)protected booleanisOneOfTheseTypes(graphql.schema.GraphQLInputType inputType, java.util.Collection<graphql.schema.GraphQLScalarType> scalarTypes)Returns true of the input type is one of the specified scalar types, regardless of non null nessprotected booleanisStringOrID(graphql.schema.GraphQLInputType inputType)Return true if the type is a String or IDprotected booleanisStringOrIDOrList(graphql.schema.GraphQLInputType inputType)Return true if the type is a String or ID or List type, regardless of non null nessprotected booleanisStringOrIDOrListOrMap(graphql.schema.GraphQLInputType inputType)Return true if the type is a String or ID or List type orGraphQLInputObjectType, regardless of non null nessprotected java.util.List<graphql.GraphQLError>mkError(ValidationEnvironment validationEnvironment, graphql.schema.GraphQLAppliedDirective directive, java.util.Map<java.lang.String,java.lang.Object> msgParams)Creates a newGraphQLErrorprotected java.util.List<graphql.GraphQLError>mkError(ValidationEnvironment validationEnvironment, java.lang.Object... messageParameters)protected java.util.Map<java.lang.String,java.lang.Object>mkMessageParams(java.lang.Object validatedValue, ValidationEnvironment validationEnvironment, java.lang.Object... args)Creates a map of named parameters for message interpolationprotected abstract java.util.List<graphql.GraphQLError>runConstraint(ValidationEnvironment validationEnvironment)This is called to perform the constraint validationjava.util.List<graphql.GraphQLError>runValidation(ValidationEnvironment validationEnvironment)This is called to runs the rule.java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface graphql.validation.constraints.DirectiveConstraint
getDocumentation
-
-
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getName
public java.lang.String getName()
- Specified by:
getNamein interfaceDirectiveConstraint- Returns:
- the name of the constraint
-
getMessageTemplate
protected java.lang.String getMessageTemplate()
-
appliesTo
public boolean appliesTo(graphql.schema.GraphQLFieldDefinition fieldDefinition, graphql.schema.GraphQLFieldsContainer fieldsContainer)Description copied from interface:ValidationRuleThis is called to work out if this rule applies to a specified field- Specified by:
appliesToin interfaceValidationRule- Parameters:
fieldDefinition- the field to checkfieldsContainer- the field container- Returns:
- true if this rule applies to the field
-
appliesTo
public boolean appliesTo(graphql.schema.GraphQLArgument argument, graphql.schema.GraphQLFieldDefinition fieldDefinition, graphql.schema.GraphQLFieldsContainer fieldsContainer)Description copied from interface:ValidationRuleThis is called to work out if this rule applies to the argument of a specified field- Specified by:
appliesToin interfaceValidationRule- Parameters:
argument- the argument to checkfieldDefinition- the field to checkfieldsContainer- the field container- Returns:
- true if this rule applies to the argument of the field field
-
appliesToType
protected abstract boolean appliesToType(graphql.schema.GraphQLInputType inputType)
A derived class will be called to indicate whether this input type applies to the constraint- Parameters:
inputType- the input type- Returns:
- true if the constraint can handle that type
-
runConstraint
protected abstract java.util.List<graphql.GraphQLError> runConstraint(ValidationEnvironment validationEnvironment)
This is called to perform the constraint validation- Parameters:
validationEnvironment- the validation environment- Returns:
- a list of errors or an empty one if there are no errors
-
runValidation
public java.util.List<graphql.GraphQLError> runValidation(ValidationEnvironment validationEnvironment)
Description copied from interface:ValidationRuleThis is called to runs the rule. A rule maybe invoked MULTIPLE times per field. It will be invoked once for the original field, then invoked for each of the arguments on a field and then if the input types are complex ones such asGraphQLInputObjectTypethen it will be invoked for each attribute of that type.A rule should consult
ValidationEnvironment.getValidatedElement()to check what element is being validated. If the rule does not handle that type of element, simply return an empty list of errors.- Specified by:
runValidationin interfaceValidationRule- Parameters:
validationEnvironment- the validation environment- Returns:
- a non null list of errors where emptyList() means its valid
-
appliesToListElements
protected abstract boolean appliesToListElements()
-
isOneOfTheseTypes
protected boolean isOneOfTheseTypes(graphql.schema.GraphQLInputType inputType, graphql.schema.GraphQLScalarType... scalarTypes)
-
isOneOfTheseTypes
protected boolean isOneOfTheseTypes(graphql.schema.GraphQLInputType inputType, java.util.Collection<graphql.schema.GraphQLScalarType> scalarTypes)Returns true of the input type is one of the specified scalar types, regardless of non null ness- Parameters:
inputType- the type to checkscalarTypes- the array of scalar types- Returns:
- true if its one of them
-
getIntArg
protected int getIntArg(graphql.schema.GraphQLAppliedDirective directive, java.lang.String argName)Returns an integer argument from a directive (or its default) and throws an assertion of the argument is null- Parameters:
directive- the directive to checkargName- the argument name- Returns:
- a non null value
-
getStrArg
protected java.lang.String getStrArg(graphql.schema.GraphQLAppliedDirective directive, java.lang.String argName)Returns an String argument from a directive (or its default) and throws an assertion of the argument is null- Parameters:
directive- the directive to checkargName- the argument name- Returns:
- a non null value
-
getBoolArg
protected boolean getBoolArg(graphql.schema.GraphQLAppliedDirective directive, java.lang.String argName)Returns an boolean argument from a directive (or its default) and throws an assertion of the argument is null- Parameters:
directive- the directive to checkargName- the argument name- Returns:
- a non null value
-
getMessageTemplate
protected java.lang.String getMessageTemplate(graphql.schema.GraphQLAppliedDirective directive)
Returns the "message : String" argument from a directive or makes up one called "graphql.validation.{name}.message"- Parameters:
directive- the directive to check- Returns:
- a non null value
-
mkMessageParams
protected java.util.Map<java.lang.String,java.lang.Object> mkMessageParams(java.lang.Object validatedValue, ValidationEnvironment validationEnvironment, java.lang.Object... args)Creates a map of named parameters for message interpolation- Parameters:
validatedValue- the value being validatedvalidationEnvironment- the validation environmentargs- must be an key / value array with String keys as the even params and values as then odd params- Returns:
- a map of message parameters
-
mkError
protected java.util.List<graphql.GraphQLError> mkError(ValidationEnvironment validationEnvironment, graphql.schema.GraphQLAppliedDirective directive, java.util.Map<java.lang.String,java.lang.Object> msgParams)
Creates a newGraphQLError- Parameters:
validationEnvironment- the current validation environmentdirective- the directive being runmsgParams- the map of parameters- Returns:
- a list of a single error
-
mkError
protected java.util.List<graphql.GraphQLError> mkError(ValidationEnvironment validationEnvironment, java.lang.Object... messageParameters)
-
isStringOrIDOrList
protected boolean isStringOrIDOrList(graphql.schema.GraphQLInputType inputType)
Return true if the type is a String or ID or List type, regardless of non null ness- Parameters:
inputType- the type to check- Returns:
- true if one of the above
-
isStringOrIDOrListOrMap
protected boolean isStringOrIDOrListOrMap(graphql.schema.GraphQLInputType inputType)
Return true if the type is a String or ID or List type orGraphQLInputObjectType, regardless of non null ness- Parameters:
inputType- the type to check- Returns:
- true if one of the above
-
isMap
protected boolean isMap(graphql.schema.GraphQLInputType inputType)
-
isStringOrID
protected boolean isStringOrID(graphql.schema.GraphQLInputType inputType)
Return true if the type is a String or ID- Parameters:
inputType- the type to check- Returns:
- true if one of the above
-
asMap
protected java.util.Map asMap(java.lang.Object value)
Casts the object as a Map with an assertion of it is not one- Parameters:
value- the object to turn into a map- Returns:
- a Map
-
asBigDecimal
protected java.math.BigDecimal asBigDecimal(java.lang.Object value) throws java.lang.NumberFormatExceptionMakes the object a BigDecimal with an assertion if we have no conversion of it- Parameters:
value- the object to turn into a BigDecimal- Returns:
- a BigDecimal
- Throws:
java.lang.NumberFormatException
-
asBoolean
protected boolean asBoolean(java.lang.Object value)
Makes the object a boolean with an assertion if we have no conversion of it- Parameters:
value- the boolean object- Returns:
- a boolean
-
getStringOrIDOrObjectOrMapLength
protected int getStringOrIDOrObjectOrMapLength(graphql.schema.GraphQLInputType inputType, java.lang.Object value)Returns the length of a String, ID, size of a list or size of a Map- Parameters:
inputType- the input typevalue- the value- Returns:
- the length of a String or Map or List
-
-