Class Validators


  • public final class Validators
    extends Object

    Collection of utility methods for the Bean Validation API in general.

    Usage

    Here are some examples:

     // Check if Bean Validation is available.
     boolean available = Validators.isBeanValidationAvailable();
     
     // Check if Bean Validation is available.
     boolean available = Validators.isBeanValidationAvailable();
     
     // Validate whole bean with Bean Validation.
     Set<ConstraintViolation<?>> violations = Validators.validateBean(bean);
     
     // Validate only a bean property with Bean Validation.
     Set<ConstraintViolation<?>> violations = Validators.validateBeanProperty(bean, "propertyName", propertyValue);
     

    For a full list, check the method summary.

    Since:
    3.8
    Author:
    Bauke Scholtz, Andre Wachsmuth
    • Method Detail

      • isBeanValidationAvailable

        public static boolean isBeanValidationAvailable()
        Returns true if Bean Validation is available. This is remembered in the application scope.
        Returns:
        true if Bean Validation is available.
      • getBeanValidatorFactory

        public static ValidatorFactory getBeanValidatorFactory()
        Returns the default bean validator factory. This is remembered in the application scope.
        Returns:
        The default bean validator factory.
      • getBeanValidator

        public static Validator getBeanValidator()
        Returns the bean validator which is aware of the Faces locale.
        Returns:
        The bean validator which is aware of the Faces locale.
        See Also:
        Faces.getLocale()
      • validateBean

        public static Set<ConstraintViolation<?>> validateBean​(Object bean,
                                                               Class<?>... groups)
        Validate given bean on given group classes.
        Parameters:
        bean - Bean to be validated.
        groups - Bean validation groups, if any.
        Returns:
        Constraint violations.
      • validateBeanProperty

        public static Set<ConstraintViolation<?>> validateBeanProperty​(Class<?> beanType,
                                                                       String propertyName,
                                                                       Object value,
                                                                       Class<?>... groups)
        Validate given value as if it were a property of the given bean type.
        Parameters:
        beanType - Type of target bean.
        propertyName - Name of property on target bean.
        value - Value to be validated.
        groups - Bean validation groups, if any.
        Returns:
        Constraint violations.
      • resolveViolatedBase

        public static Object resolveViolatedBase​(Object bean,
                                                 ConstraintViolation<?> violation)
        Resolve violated base from given bean based on given violation.
        Parameters:
        bean - Bean to resolve violated base on.
        violation - Constraint violation to resolve violated base from.
        Returns:
        Violated base from given bean based on given violation.
      • resolveViolatedBasesAndProperties

        public static List<Map.Entry<Object,​String>> resolveViolatedBasesAndProperties​(Object bean,
                                                                                             ConstraintViolation<?> violation)
        Resolve violated bases and properties from given bean based on given violation in reverse order.
        Parameters:
        bean - Bean to resolve violated base on.
        violation - Constraint violation to resolve violated base from.
        Returns:
        Violated bases and properties from given bean based on given violation in reverse order.
        Since:
        3.14.2
      • resolveViolatedProperty

        public static String resolveViolatedProperty​(ConstraintViolation<?> violation)
        Resolve violated property from given violation.
        Parameters:
        violation - Constraint violation to resolve violated property from.
        Returns:
        Violated property from given violation.
      • getPropertyNodes

        public static List<Path.Node> getPropertyNodes​(ConstraintViolation<?> violation)
        Returns a list of property path nodes from the given constraint violation.
        Parameters:
        violation - The constraint violation to return a list of property path nodes from.
        Returns:
        A list of property path nodes from the given constraint violation.