Class AttributeValidator
- java.lang.Object
-
- org.gcube.informationsystem.types.impl.validator.AttributeValidator
-
public class AttributeValidator extends Object
Used to validate the value fo one attribute against the attribute definition- Author:
- Luca Frosini (ISTI - CNR)
-
-
Field Summary
Fields Modifier and Type Field Description protected AttributeDefinitionattributeDefinitionThe definition of the attribute to validate against.protected AttributeValidatorReportattributeValidatorReportThe report containing validation results.protected static org.slf4j.LoggerloggerThe logger for this class.protected booleanstopOnErrorWhether to stop validation on the first error.protected org.gcube.com.fasterxml.jackson.databind.JsonNodevalueThe JSON value of the attribute to validate.
-
Constructor Summary
Constructors Constructor Description AttributeValidator(AttributeDefinition attributeDefinition, org.gcube.com.fasterxml.jackson.databind.JsonNode value)Constructs a new validator.AttributeValidator(AttributeDefinition attributeDefinition, org.gcube.com.fasterxml.jackson.databind.JsonNode value, boolean stopOnError)Constructs a new validator with the option to stop on the first error.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddError(StringBuffer sb)Adds an error message to the report and logs it.protected booleancheckPropertyTypeCompliance(org.gcube.com.fasterxml.jackson.databind.JsonNode value, String expectedPropertyType, StringBuffer sb, boolean fromMultipleInstaces)Checks the property type compliance.AttributeDefinitiongetAttributeDefinition()Returns the attribute definition.AttributeValidatorReportgetAttributeValidatorReport()Returns the attribute validator report.protected StringBuffergetNewStringBuffer(StringBuffer sb)Returns a new string buffer for logging.protected StringBuffergetPropertyStringBuffer(StringBuffer sb, String expectedPropertyType, boolean fromMultipleInstaces)Returns a string buffer for a property.org.gcube.com.fasterxml.jackson.databind.JsonNodegetValue()Returns the value of the attribute.booleanisStopOnError()Checks if the validator stops on the first error.protected booleantypeNotCompliant(org.gcube.com.fasterxml.jackson.databind.JsonNode value, String expectedType, StringBuffer sb)Checks if the type is compliant.booleanvalidate()Validates the attribute's value against its definition.protected booleanvalidate(String expectedType, org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a value against an expected type.protected booleanvalidateByte(org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a byte.protected booleanvalidateDate(org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a date.protected booleanvalidateNumber(Number number, StringBuffer sb)Validates a number against the attribute's min/max constraints.protected booleanvalidateString(org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a string.
-
-
-
Field Detail
-
logger
protected static org.slf4j.Logger logger
The logger for this class.
-
stopOnError
protected final boolean stopOnError
Whether to stop validation on the first error.
-
attributeDefinition
protected final AttributeDefinition attributeDefinition
The definition of the attribute to validate against.
-
value
protected final org.gcube.com.fasterxml.jackson.databind.JsonNode value
The JSON value of the attribute to validate.
-
attributeValidatorReport
protected final AttributeValidatorReport attributeValidatorReport
The report containing validation results.
-
-
Constructor Detail
-
AttributeValidator
public AttributeValidator(AttributeDefinition attributeDefinition, org.gcube.com.fasterxml.jackson.databind.JsonNode value)
Constructs a new validator.- Parameters:
attributeDefinition- The attribute definition.value- The JSON value.
-
AttributeValidator
public AttributeValidator(AttributeDefinition attributeDefinition, org.gcube.com.fasterxml.jackson.databind.JsonNode value, boolean stopOnError)
Constructs a new validator with the option to stop on the first error.- Parameters:
attributeDefinition- The attribute definition.value- The JSON value.stopOnError-trueto stop on the first error.
-
-
Method Detail
-
isStopOnError
public boolean isStopOnError()
Checks if the validator stops on the first error.- Returns:
trueif the validator stops on the first error.
-
getAttributeDefinition
public AttributeDefinition getAttributeDefinition()
Returns the attribute definition.- Returns:
- The attribute definition.
-
getValue
public org.gcube.com.fasterxml.jackson.databind.JsonNode getValue()
Returns the value of the attribute.- Returns:
- The value of the attribute.
-
getAttributeValidatorReport
public AttributeValidatorReport getAttributeValidatorReport()
Returns the attribute validator report.- Returns:
- The attribute validator report.
-
addError
protected void addError(StringBuffer sb)
Adds an error message to the report and logs it.- Parameters:
sb- The string buffer containing the error message.
-
getNewStringBuffer
protected StringBuffer getNewStringBuffer(StringBuffer sb)
Returns a new string buffer for logging.- Parameters:
sb- The string buffer.- Returns:
- The new string buffer.
-
validateNumber
protected boolean validateNumber(Number number, StringBuffer sb)
Validates a number against the attribute's min/max constraints.- Parameters:
number- The number to validate.sb- The string buffer for error messages.- Returns:
trueif the number is valid,falseotherwise.
-
typeNotCompliant
protected boolean typeNotCompliant(org.gcube.com.fasterxml.jackson.databind.JsonNode value, String expectedType, StringBuffer sb)Checks if the type is compliant.- Parameters:
value- The value.expectedType- The expected type.sb- The string buffer.- Returns:
trueif the type is compliant.
-
validateString
protected boolean validateString(org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a string.- Parameters:
value- The value.sb- The string buffer.- Returns:
trueif the string is valid.
-
validateDate
protected boolean validateDate(org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a date.- Parameters:
value- The value.sb- The string buffer.- Returns:
trueif the date is valid.
-
validateByte
protected boolean validateByte(org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)Validates a byte.- Parameters:
value- The value.sb- The string buffer.- Returns:
trueif the byte is valid.
-
getPropertyStringBuffer
protected StringBuffer getPropertyStringBuffer(StringBuffer sb, String expectedPropertyType, boolean fromMultipleInstaces)
Returns a string buffer for a property.- Parameters:
sb- The string buffer.expectedPropertyType- The expected property type.fromMultipleInstaces- Whether there are multiple instances.- Returns:
- The string buffer.
-
checkPropertyTypeCompliance
protected boolean checkPropertyTypeCompliance(org.gcube.com.fasterxml.jackson.databind.JsonNode value, String expectedPropertyType, StringBuffer sb, boolean fromMultipleInstaces)Checks the property type compliance.- Parameters:
value- The value.expectedPropertyType- The expected property type.sb- The string buffer.fromMultipleInstaces- Whether there are multiple instances.- Returns:
trueif the property type is compliant.
-
validate
protected boolean validate(String expectedType, org.gcube.com.fasterxml.jackson.databind.JsonNode value, StringBuffer sb)
Validates a value against an expected type.- Parameters:
expectedType- The expected type.value- The value.sb- The string buffer.- Returns:
trueif the value is valid.
-
validate
public boolean validate()
Validates the attribute's value against its definition.- Returns:
trueif the value is valid,falseotherwise.
-
-