title: Use of fluent interfaces to validate objects using java 8
body: <p>Im currently doing a side project to validate objects using java 8.</p>
  
  <p>Currently I have this:</p>
  
  <ul>
  <li>An interface that is essencially a rewrite of the java 8 predicate interface:</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/HQ348.png" alt="enter image description here"></p>
  
  <p>Then, I created the implementation of that same interface:</p>
  
  <p><img src="https://i.stack.imgur.com/GkUuK.png" alt="enter image description here"></p>
  
  <p>And then this class is the result of my validation</p>
  
  <p><img src="https://i.stack.imgur.com/wy6NH.png" alt="enter image description here"></p>
  
  <p>The concrete object validation can be found here:</p>
  
  <pre><code>public class ConcreteValidator implements EmployeeValidator {
    @Override
    public void validate(Employee employee) throws EmployeeException {
  
      ValidatorUtil.notNullString.and(ValidatorUtil.notEmptyString)
          .and(ValidatorUtil.stringBetween(1, 100)).test(employee.getFirstName())
          .getFieldNameIfInvalid(" Please specify valid firstname ").orElse("");
  
      ValidatorUtil.notNullString.and(ValidatorUtil.notEmptyString)
          .and(ValidatorUtil.stringBetween(1, 100)).test(employee.getLastName())
          .getFieldNameIfInvalid(" Please specify valid lastname ").orElse("");
  
      ValidatorUtil.notNullString.and(ValidatorUtil.notEmptyString)
          .and(ValidatorUtil.stringBetween(3, 100)).test(employee.getEmail())
          .getFieldNameIfInvalid(" Please specify valid email ").orElse("");
  
      ValidatorUtil.notNullInteger.and(ValidatorUtil.greaterThanZero)
          .and(ValidatorUtil.integerBetween(18, 60)).test(employee.getAge())
          .getFieldNameIfInvalid(" Please specify valid age ").orElse("");
    }
  }
  </code></pre>
  
  <p>This works fine but what I want to do now is to limit the user to use the notNull verification first, and only after that validation all the methods like notEmpty or greaterThanZero will be available.</p>
  
  <p>I searched for fluent interfaces but don't know if it is the correct way to do it (want to do something like this: <a href="https://code-held.com/2019/04/29/robust-builder-pattern/" rel="nofollow noreferrer">https://code-held.com/2019/04/29/robust-builder-pattern/</a>)</p>
  
  <p>To summarise, I want to force the developer to validate if the object is null first and all the other methods go next, something like the chaining of the Stream API in java-8.Here is my customValidations.</p>
  
  <p><img src="https://i.stack.imgur.com/9wqGR.png" alt="enter image description here"></p>