Class AttributeValidator


  • public class AttributeValidator
    extends Object
    Used to validate the value fo one attribute against the attribute definition
    Author:
    Luca Frosini (ISTI - CNR)
    • 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 - true to stop on the first error.
    • Method Detail

      • isStopOnError

        public boolean isStopOnError()
        Checks if the validator stops on the first error.
        Returns:
        true if 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:
        true if the number is valid, false otherwise.
      • 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:
        true if 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:
        true if 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:
        true if 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:
        true if 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:
        true if 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:
        true if the value is valid.
      • validate

        public boolean validate()
        Validates the attribute's value against its definition.
        Returns:
        true if the value is valid, false otherwise.