Class Validator

  • All Implemented Interfaces:
    ValidationSummarizer

    public class Validator
    extends Object
    implements ValidationSummarizer
    The Validator helps to group, chain and execute ValidationStatements.
    Example:
    
     public ValidationResult validate(){
         return new Validator()
              .add(this::validateName)
              .add(this::validateAge)
              .validateAll();
     }
    
     ValidationInfo validateName(){
         String name = unbindName();
         if(name.isBlank()){
            return ValidationInfo.invalid("Name is empty");
         }
         return ValidationInfo.valid();
     }
    
     ValidationInfo validateAge(){
         int age = unbindAge();
         if(age <= 0){
             return ValdationInfo.invalid("Age must be positive value")
         }
         return ValidationInfo.valid();
     }
     

    Logging: Depending on the ValidationRunner you choose you will get detailed information about the validation process if you set the log level for this package to debug. The used logging framework ist org.apache.logging.log4j.Logger
    Author:
    mlo
    • Constructor Detail

      • Validator

        public Validator()
    • Method Detail

      • add

        @NotNull
        public @NotNull Validator add​(@Nullable
                                      @Nullable ValidationStatement statement)
        Adds a new ValidationStatement to the list of existent statements. The added statement will be appended at the end of the list. The order of the statements is maintained.
        Execute the added statements which validate()
        Example:
        
         public ValidationResult validate(Person person){
             return new Validator()
              .add(() -> validateName(person.getName())
              .add(() -> validateAge(person.getAge())
              .add(() -> validateAddress(person.getAddress())
              .validate();
         }
         

        For more complex stuff like grouping and nesting you can use the other add function which accepts whole valdiators and results.
        Parameters:
        statement - A new statement to add to the end of the list of statements. Null values are ignored
        Returns:
        An instance of this Validator so you can chain 'add' calls
      • add

        @NotNull
        public @NotNull Validator add​(@Nullable
                                      @Nullable ValidationSummarizer validationSummarizer)
        Adds a ValidationSummarizer. A summarizer can execute multiple parts of the validation process and can aggregate the result of these.
        This function allows you to nest different validators with their own runner.
        Example: Check that the given person is not null and if this is true, the persons fields will be validated all
        
         public ValidationResult validatePerson(Person person){
             return new Validator()
              .add(new Validator().add(() -> validatePersonNotNull(person))
              .add(new Validator()
                  .add(() -> validateName(person.getName())
                  .add(() -> validateAge(person.getAge()))
              .validateStopOnFirstFail()
         }
         
        Parameters:
        validationSummarizer - A ValidationSummarizer which can be a Validator or ValidationResult for example. If the parameter is null it will be ignored.
        Returns:
        The instance of this validator
      • validateStopOnFirstFail

        @NotNull
        public @NotNull ValidationResult validateStopOnFirstFail()
        Shortcut for
        
         validator.setValidateStopOnFirstFail().validate();
         
        Executes the added ValidationStatements in the order they have been added until the first ValidationStatement fail. If a single statement fails, the ValidationResult.isValid() function will return false. Only if all statement passes the test the result will be valid (ValidationResult.isValid() will be true).
        The validation result also contains the result of every single statements and can aggregate all failure messages: ValidationResult.getMessage().
        Returns:
        The result of the validation process. The result will contain zero or only one information which indicates that the validation failed.
      • setValidationRunner

        @NotNull
        public @NotNull Validator setValidationRunner​(@NotNull
                                                      @NotNull ValidationRunner validationRunner)
        Sets a custom ValidationRunner. A runner has to execute statements and has to aggregate the results of them.
        Parameters:
        validationRunner - A custom validation runner
        Returns:
        The instance of this validator