Class Constraint


  • public abstract class Constraint
    extends Object
    Represents a validation constraint.
    Author:
    Alex Bogdanovski [alex@erudika.com]
    • Method Detail

      • isValid

        public abstract boolean isValid​(Object actualValue)
        Validates the given value against the this constraint.
        Parameters:
        actualValue - a value / property of an object
        Returns:
        true if the value satisfies the constraint
      • getName

        public String getName()
        The constraint name.
        Returns:
        a name
      • setName

        public void setName​(String name)
        Sets the name of the constraint.
        Parameters:
        name - a name
      • getPayload

        public Map<String,​Object> getPayload()
        The payload (a map).
        Returns:
        a map
      • setPayload

        public void setPayload​(Map<String,​Object> payload)
        Sets the payload.
        Parameters:
        payload - a map
      • matches

        public static boolean matches​(Class<? extends Annotation> anno,
                                      String consName)
        Verifies that the given annotation type corresponds to a known constraint.
        Parameters:
        anno - annotation type
        consName - constraint name
        Returns:
        true if known
      • fromAnnotation

        public static Constraint fromAnnotation​(Annotation anno)
        Builds a new constraint from the annotation data.
        Parameters:
        anno - JSR-303 annotation instance
        Returns:
        a new constraint
      • isValidConstraintName

        public static boolean isValidConstraintName​(String name)
        Returns true if that validator is the list of known validators.
        Parameters:
        name - a name
        Returns:
        true if validator is known
      • isValidConstraintType

        public static boolean isValidConstraintType​(Class<? extends Annotation> type)
        Returns true if that validator is the list of known validators.
        Parameters:
        type - annotation class type
        Returns:
        true if validator is known
      • required

        public static Constraint required()
        The 'required' constraint - marks a field as required.
        Returns:
        constraint
      • min

        public static Constraint min​(Number min)
        The 'min' constraint - field must contain a number larger than or equal to min.
        Parameters:
        min - the minimum value
        Returns:
        constraint
      • max

        public static Constraint max​(Number max)
        The 'max' constraint - field must contain a number smaller than or equal to max.
        Parameters:
        max - the maximum value
        Returns:
        constraint
      • size

        public static Constraint size​(Number min,
                                      Number max)
        The 'size' constraint - field must be a String, Map, Collection or array with a given minimum and maximum length.
        Parameters:
        min - the minimum length
        max - the maximum length
        Returns:
        constraint
      • digits

        public static Constraint digits​(Number integer,
                                        Number fraction)
        The 'digits' constraint - field must be a Number or String containing digits where the number of digits in the integral part is limited by 'integer', and the number of digits for the fractional part is limited by 'fraction'.
        Parameters:
        integer - the max number of digits for the integral part
        fraction - the max number of digits for the fractional part
        Returns:
        constraint
      • pattern

        public static Constraint pattern​(Object regex)
        The 'pattern' constraint - field must contain a value matching a regular expression.
        Parameters:
        regex - a regular expression
        Returns:
        constraint
      • email

        public static Constraint email()
        The 'email' constraint - field must contain a valid email.
        Returns:
        constraint
      • falsy

        public static Constraint falsy()
        The 'falsy' constraint - field value must not be equal to 'true'.
        Returns:
        constraint
      • truthy

        public static Constraint truthy()
        The 'truthy' constraint - field value must be equal to 'true'.
        Returns:
        constraint
      • future

        public static Constraint future()
        The 'future' constraint - field value must be a Date or a timestamp in the future.
        Returns:
        constraint
      • past

        public static Constraint past()
        The 'past' constraint - field value must be a Date or a timestamp in the past.
        Returns:
        constraint
      • url

        public static Constraint url()
        The 'url' constraint - field value must be a valid URL.
        Returns:
        constraint
      • build

        public static Constraint build​(String cname,
                                       Map<String,​Object> payload)
        Builds a new constraint from a given name and payload.
        Parameters:
        cname - the constraint name
        payload - the payload
        Returns:
        constraint