prepare the usage of dynamic validation messages

This commit is contained in:
Andreas Billmann
2015-12-17 21:15:37 +01:00
parent a67f793670
commit 5121b998c5
6 changed files with 124 additions and 49 deletions

View File

@@ -33,14 +33,14 @@ import java.util.List;
*/
public class ValidationError {
private List<String> messages;
private List<ValidationMessage> messages;
private Integer lineNumber;
public ValidationError(List<String> messages) {
public ValidationError(List<ValidationMessage> messages) {
this(messages, -1);
}
public ValidationError(List<String> messages, Integer lineNumber) {
public ValidationError(List<ValidationMessage> messages, Integer lineNumber) {
this.messages = messages;
this.lineNumber = lineNumber;
}
@@ -49,7 +49,7 @@ public class ValidationError {
return lineNumber;
}
public List<String> getMessages() {
public List<ValidationMessage> getMessages() {
return messages;
}

View File

@@ -0,0 +1,71 @@
/*
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package ninja.javafx.smartcsv.validation;
import java.util.Arrays;
/**
* TODO: DESCRIPTION!!!
*/
public class ValidationMessage {
private String key;
private String[] parameters;
public ValidationMessage(String key, String... parameters) {
this.key = key;
this.parameters = parameters;
}
public String getKey() {
return key;
}
public String[] getParameters() {
return parameters;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ValidationMessage that = (ValidationMessage) o;
if (key != null ? !key.equals(that.key) : that.key != null) return false;
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(parameters, that.parameters);
}
@Override
public int hashCode() {
int result = key != null ? key.hashCode() : 0;
result = 31 * result + Arrays.hashCode(parameters);
return result;
}
}

View File

@@ -86,7 +86,7 @@ public class Validator {
Config columnConfig = getColumnConfig(columnSectionConfig, column);
if (columnConfig != null) {
List<String> errorMessages = new ArrayList<>();
List<ValidationMessage> errorMessages = new ArrayList<>();
checkBlankOrNull(columnConfig, value, errorMessages);
if (value != null) {
checkRegularExpression(columnConfig, value, errorMessages);
@@ -112,7 +112,7 @@ public class Validator {
// private methods
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void checkGroovy(String column, Config columnConfig, String value, List<String> result) {
private void checkGroovy(String column, Config columnConfig, String value, List<ValidationMessage> result) {
String groovyScript = getString(columnConfig, "groovy");
if (groovyScript != null) {
@@ -130,15 +130,15 @@ public class Validator {
try {
groovyResult = script.run();
} catch (CompilationFailedException e) {
result.add("groovy script '"+groovyScript+"' throws exception: "+e.getMessage());
result.add(new ValidationMessage("groovy script '"+groovyScript+"' throws exception: "+e.getMessage()));
e.printStackTrace();
}
if (groovyResult == null) {
result.add("groovy script '"+groovyScript+"' returns null");
result.add(new ValidationMessage("groovy script '"+groovyScript+"' returns null"));
}
if (!isScriptResultTrue(groovyResult)) {
result.add(groovyResult.toString());
result.add(new ValidationMessage(groovyResult.toString()));
}
}
@@ -148,62 +148,62 @@ public class Validator {
return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true");
}
private void checkBlankOrNull(Config columnConfig, String value, List<String> result) {
private void checkBlankOrNull(Config columnConfig, String value, List<ValidationMessage> result) {
if (getBoolean(columnConfig, "not empty")) {
if (isBlankOrNull(value)) {
result.add("validation.message.not.empty");
result.add(new ValidationMessage("validation.message.not.empty"));
}
}
}
private void checkInteger(Config columnConfig, String value, List<String> result) {
private void checkInteger(Config columnConfig, String value, List<ValidationMessage> result) {
if (getBoolean(columnConfig, "integer")) {
if (!isInt(value)) {
result.add("validation.message.integer");
result.add(new ValidationMessage("validation.message.integer"));
}
}
}
private void checkMinLength(Config columnConfig, String value, List<String> result) {
private void checkMinLength(Config columnConfig, String value, List<ValidationMessage> result) {
Integer minLength = getInteger(columnConfig, "minlength");
if (minLength != null) {
if (!minLength(value, minLength)) {
result.add("has not min length of " + minLength);
result.add(new ValidationMessage("has not min length of " + minLength));
}
}
}
private void checkMaxLength(Config columnConfig, String value, List<String> result) {
private void checkMaxLength(Config columnConfig, String value, List<ValidationMessage> result) {
Integer maxLength = getInteger(columnConfig, "maxlength");
if (maxLength != null) {
if (!maxLength(value, maxLength)) {
result.add("has not max length of " + maxLength);
result.add(new ValidationMessage("has not max length of " + maxLength));
}
}
}
private void checkDate(Config columnConfig, String value, List<String> result) {
private void checkDate(Config columnConfig, String value, List<ValidationMessage> result) {
String dateformat = getString(columnConfig, "date");
if (dateformat != null && !dateformat.trim().isEmpty()) {
if (!isDate(value, dateformat, true)) {
result.add("is not a date of format " + dateformat);
result.add(new ValidationMessage("is not a date of format " + dateformat));
}
}
}
private void checkAlphaNumeric(Config columnConfig, String value, List<String> result) {
private void checkAlphaNumeric(Config columnConfig, String value, List<ValidationMessage> result) {
if (getBoolean(columnConfig, "alphanumeric")) {
if (!matchRegexp(value, "[0-9a-zA-Z]*")) {
result.add("validation.message.alphanumeric");
result.add(new ValidationMessage("validation.message.alphanumeric"));
}
}
}
private void checkRegularExpression(Config columnConfig, String value, List<String> result) {
private void checkRegularExpression(Config columnConfig, String value, List<ValidationMessage> result) {
String regexp = getString(columnConfig, "regexp");
if (regexp != null && !regexp.trim().isEmpty()) {
if (!matchRegexp(value, regexp)) {
result.add("does not match " + regexp);
result.add(new ValidationMessage("does not match " + regexp));
}
}
}
@@ -239,25 +239,25 @@ public class Validator {
List<String> headerConfig = headerSectionConfig.getStringList("list");
if (headerConfig != null) {
if (headerNames.length != headerConfig.size()) {
result = new ValidationError(singletonList("number of headers is not correct! there are " +
result = new ValidationError(singletonList(new ValidationMessage("number of headers is not correct! there are " +
headerNames.length +
" but there should be " +
headerConfig.size()));
headerConfig.size())));
return result;
}
List<String> errorMessages = new ArrayList<>();
List<ValidationMessage> errorMessages = new ArrayList<>();
for(int i=0; i<headerConfig.size(); i++) {
String header = headerConfig.get(i);
if (!header.equals(headerNames[i])) {
errorMessages.add("header number " +
errorMessages.add(new ValidationMessage("header number " +
i +
" does not match \"" +
header +
"\" should be \"" +
headerNames[i] +
"\"");
"\""));
}
}
if (!errorMessages.isEmpty()) {