Class ValidationUtils


  • public final class ValidationUtils
    extends Object
    Utility classes to facilitate JRS-380 based validation
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T ensureValid​(T bean, String message)
      Ensures that a bean annotated with JSR-380 constraints is valid.
      static void failValidation​(Set<? extends javax.validation.ConstraintViolation<?>> constraintViolations, String messageTitle)
      Fails with the ConstraintViolationException that includes the violated constraints and the human-readable representation of them.
      static String formatViolations​(Set<? extends javax.validation.ConstraintViolation<?>> constraintViolations)
      Formats a set of ConstraintViolation to a human-readable string.
      static <T> Set<javax.validation.ConstraintViolation<T>> validate​(T bean)
      Validates a bean annotated with JSR-380 constraints using the default validation factory.
    • Method Detail

      • validate

        @Nonnull
        public static <T> Set<javax.validation.ConstraintViolation<T>> validate​(@Nonnull
                                                                                T bean)
        Validates a bean annotated with JSR-380 constraints using the default validation factory.
        Type Parameters:
        T - the type of the bean.
        Parameters:
        bean - the bean to validate
        Returns:
        the set of violated constrains, empty if the bean is valid.
      • ensureValid

        @Nonnull
        public static <T> T ensureValid​(@Nonnull
                                        T bean,
                                        @Nonnull
                                        String message)
                                 throws javax.validation.ConstraintViolationException
        Ensures that a bean annotated with JSR-380 constraints is valid. If validation with the default validation factory results in any violation throws the ConstraintViolationException.
        Type Parameters:
        T - the type of the bean.
        Parameters:
        bean - the bean to validate
        message - the message to use as the title of the exception message.
        Returns:
        the valid bean.
        Throws:
        javax.validation.ConstraintViolationException - if any constraints are violated.
      • failValidation

        public static void failValidation​(@Nonnull
                                          Set<? extends javax.validation.ConstraintViolation<?>> constraintViolations,
                                          @Nullable
                                          String messageTitle)
                                   throws javax.validation.ConstraintViolationException
        Fails with the ConstraintViolationException that includes the violated constraints and the human-readable representation of them.
        Parameters:
        constraintViolations - the violation to include in the exception.
        messageTitle - the title of the error message.
        Throws:
        javax.validation.ConstraintViolationException
      • formatViolations

        @Nonnull
        public static String formatViolations​(@Nonnull
                                              Set<? extends javax.validation.ConstraintViolation<?>> constraintViolations)
        Formats a set of ConstraintViolation to a human-readable string.
        Parameters:
        constraintViolations - the violations to include.
        Returns:
        the human-readable representaion of the violations.