Interface Validator


public interface Validator
Validate plain Java objects that have been annotated with validation constraints.


   // create a validator
   final Validator validator = Validator.builder()
     .setDefaultLocale(Locale.CANADA)
     .addLocales(Locale.GERMAN)
     .build();

  // validate a pojo
  Customer customer = ...;
  validator.validate(customer);

 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Function to build a ValidationAdapter from a Validation Context
    static interface 
    Function to build a ValidationAdapter that needs Validator.
    static interface 
    Build the Validator instance adding ValidationAdapter, Factory or AdapterBuilder.
    static interface 
    Components register ValidationAdapters with the Validator.Builder
  • Method Summary

    Modifier and Type
    Method
    Description
    Return the Builder used to build the Validator.
    check(Object any, Class<?>... groups)
    Validate the object returning the constraint violations.
    check(Object any, Locale locale, Class<?>... groups)
    Validate the object returning the constraint violations.
    Return the validation context used to create adapters
    void
    validate(Object any, Class<?>... groups)
    Validate the object using the default locale throwing ConstraintViolationException when there are constraint violations.
    void
    validate(Object any, Locale locale, Class<?>... groups)
    Validate the object with a given locale throwing ConstraintViolationException when there are constraint violations.
  • Method Details

    • validate

      void validate(Object any, @Nullable Class<?>... groups) throws ConstraintViolationException
      Validate the object using the default locale throwing ConstraintViolationException when there are constraint violations.
      Parameters:
      any - The object to validate
      groups - The groups targeted for validation
      Throws:
      ConstraintViolationException - when there are constraint violations
    • validate

      void validate(Object any, @Nullable Locale locale, @Nullable Class<?>... groups) throws ConstraintViolationException
      Validate the object with a given locale throwing ConstraintViolationException when there are constraint violations.

      If the locale is not one of the supported locales then the default locale will be used.

      This is expected to be used when the Validator is configured to support multiple locales.

      Parameters:
      any - The object to validate
      locale - The locale to use for constraint messages
      groups - The groups targeted for validation
      Throws:
      ConstraintViolationException - when there are constraint violations
    • check

      Set<ConstraintViolation> check(Object any, @Nullable Class<?>... groups)
      Validate the object returning the constraint violations.
      Parameters:
      any - The object to validate
      groups - The groups targeted for validation
      Returns:
      The constraint violations
    • check

      Set<ConstraintViolation> check(Object any, @Nullable Locale locale, @Nullable Class<?>... groups)
      Validate the object returning the constraint violations.
      Parameters:
      any - The object to validate
      locale - The locale to use for constraint messages
      groups - The groups targeted for validation
      Returns:
      The constraint violations
    • context

      Return the validation context used to create adapters
    • builder

      static Validator.Builder builder()
      Return the Builder used to build the Validator.
      
      
         final Validator validator = Validator.builder()
           .setDefaultLocale(Locale.CANADA)
           .addLocales(Locale.GERMAN)
           .build();