Want to reduce code from these validations, these validators' classes verify and return if inputs are valid or invalid, it's a reduction, I will validate some panels and almost 40 fields. Want to see if there is some pattern to simplify this, code is more than 300 lines which I believe to be a bad practice.
package Validation1;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MinimalReproducibleExampleValidation {
public static void main(String[] args) {
boolean saveToDatabase = true;
String name = "Richard";
String idCard = "123456789";
String address = "Main Street 454";
Entity entity = new Entity ();
/// Name Validation
if (saveToDatabase) {
ValidationEntity nameValidation = new
ValidationEntity(ValidationEntity.Regex.Alphabetic,
name, "ID Card", 0, 13);
saveToDatabase = nameValidation.isValid();
entity.setName(name);
}
/// ID Card Validation
if (saveToDatabase) {
ValidationEntity idCardValidator = new
ValidationEntity(ValidationEntity.Regex.Numerical,
idCard, "ID Card", 0, 13);
saveToDatabase = idCardValidator.isValid();
entity.setIdCard(idCard);
}
/// EMail Validation
if (saveToDatabase) {
ValidationEntity emailValidator = new
ValidationEntity(ValidationEntity.Regex.AlphaNumerical,
address, "Address", 1, 9);
saveToDatabase = emailValidator.isValid();
entity.setAddress(address);
}
// If every field is valid, save
if (saveToDatabase) {
new EntityDao().save(entity);
}
}
}
and:
class ValidationEntity {
private Regex regex;
private String input;
private String errorMessage;
private Integer minimum;
private Integer maximum;
public ValidationEntity(Regex regex, String input, String errorMessage, int minimum, int maximum) {
this.regex = regex;
this.input = input;
this.errorMessage = errorMessage;
this.minimum = minimum;
this.maximum = maximum;
}
public boolean isValid() {
Pattern pattern = Pattern.compile(getRegexFormat(), Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
return matcher.matches();
}
public String getRegexFormat() {
return "^" + regex.getRegex() + "{" + minimum + "," + maximum + "}";
}
and:
public enum Regex {
LowercaseAlphabetic("[a-z]"), UppercaseAlphabetic("[A-Z]"), Numerical("[0-9]"), Alphabetic("[a-zA-Z]"),
AlphaNumerical("^[A-Za-z0-9_ ]*$");
public String regexValue;
Regex(String regexValue) {
this.regexValue = regexValue;
}
}
}
and:
class EntityDao {
public void save(Entity entity) {
System.out.println("Saving the model!");
}
}
and:
class Entity {
private String name;
private String idCard;
private String address;
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public String getIdCard() {
return idCard;
}
public String getIdName() {
return name;
}
public String getAddress() {
return address;
}
}