vendredi 20 janvier 2017

Validation of user input, I need some design suggestions

I'm trying to make validation classes. At the moment I working with booleans, and I can return if input meets the configuration by a boolean. Now I would like to convert the booleans to objects containing a boolean result and String ErrorMessage. But I'm can't seem to get it right. This is the working code. Who can give some suggestions on how to approach this problem.

public class AttributeValidation {

     static boolean isFirstNameValid(String firstName) {
            if (firstName != null && firstName.length() <= 24 && Pattern.matches("^\\D*$", firstName)) {
                return true;
            }
            return false;
        }

        static boolean isPrefixValid(String prefix) {
            if (prefix == null) {
                return true;
            } else if (prefix.length() <= 16 && Pattern.matches("^\\D*$", prefix)) {
                return true;
            }
            return false;
        }

        static boolean isLastNameValid(String lastName) {
            if(lastName != null && lastName.length() <= 60 && Pattern.matches("^\\D*$", lastName)) {
                return true;
            }
            return false;
        }
}

In the future I want to use different valididation configurations

  public class PersonValidationResult{

        boolean isValidationResult = true;
        //  private List <ErrorMessage> errors = new ArrayList <> ();

        public void validate(Person person) {
                    AttributeValidation.isFirstNameValid(person.getFirstName()) == false |
                    AttributeValidation.isPrefixValid(person.getPrefix()) == false |
                    AttributeValidation.isLastNameValid(person.getLastName()) == false){
                this.isValidationResult = false;
            }

And my factory class

public class ValidationResultFactory {

   public static PersonValidationResult createPersonValidationResult(Person person){
       PersonValidationResult validationResult = new PersonValidationResult();
       validationResult.validate(person);
       return validationResult;
   }

}

Aucun commentaire:

Enregistrer un commentaire