1 package liquibase.exception; 2 3 import liquibase.database.Database; 4 import liquibase.changelog.ChangeSet; 5 6 import java.util.ArrayList; 7 import java.util.Collection; 8 import java.util.List; 9 10 public class ValidationErrors { 11 12 protected List<String> errorMessages = new ArrayList<String>(); 13 14 public boolean hasErrors() { 15 return errorMessages.size() > 0; 16 } 17 18 public void checkRequiredField(String requiredFieldName, Object value) { 19 if (value == null) { 20 addError(requiredFieldName + " is required"); 21 } else if (value instanceof Collection && ((Collection) value).size() == 0) { 22 addError(requiredFieldName + " is empty"); 23 } else if (value instanceof Object[] && ((Object[]) value).length == 0) { 24 addError(requiredFieldName + " is empty"); 25 } 26 } 27 28 public void checkDisallowedField(String disallowedFieldName, Object value, Database database, 29 Class<? extends Database>... disallowedDatabases) { 30 boolean isDisallowed = false; 31 if (disallowedDatabases == null || disallowedDatabases.length == 0) { 32 isDisallowed = true; 33 } else { 34 for (Class<? extends Database> databaseClass : disallowedDatabases) { 35 if (databaseClass.isAssignableFrom(database.getClass())) { 36 isDisallowed = true; 37 } 38 } 39 } 40 41 if (isDisallowed && value != null) { 42 addError(disallowedFieldName + " is not allowed on " + database.getTypeName()); 43 } 44 } 45 46 public void addError(String message) { 47 errorMessages.add(message); 48 } 49 50 public List<String> getErrorMessages() { 51 return errorMessages; 52 } 53 54 public void addAll(ValidationErrors validationErrors) { 55 this.errorMessages.addAll(validationErrors.getErrorMessages()); 56 } 57 58 public void addAll(ValidationErrors validationErrors, ChangeSet changeSet) { 59 for (String message : validationErrors.getErrorMessages()) { 60 this.errorMessages.add(message + ", " + changeSet); 61 } 62 } 63 }