mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 21:48:22 +02:00
support list of possible values
This commit is contained in:
@@ -36,6 +36,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static org.apache.commons.validator.GenericValidator.*;
|
||||
|
||||
/**
|
||||
@@ -94,6 +95,7 @@ public class Validator {
|
||||
checkMinLength(columnConfig, value, error);
|
||||
checkInteger(columnConfig, value, error);
|
||||
checkGroovy(column, columnConfig, value, error);
|
||||
checkValueOf(columnConfig, value, error);
|
||||
}
|
||||
|
||||
if (!error.isEmpty()) {
|
||||
@@ -146,6 +148,16 @@ public class Validator {
|
||||
return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true");
|
||||
}
|
||||
|
||||
private void checkValueOf(Config columnConfig, String value, ValidationError error) {
|
||||
List<String> stringList = getStringList(columnConfig, "value of");
|
||||
if (stringList != null) {
|
||||
if (!stringList.contains(value)) {
|
||||
String commaSeparated = stringList.stream().collect(joining(", "));
|
||||
error.add("validation.message.value.of", value, commaSeparated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkBlankOrNull(Config columnConfig, String value, ValidationError error) {
|
||||
if (getBoolean(columnConfig, "not empty")) {
|
||||
if (isBlankOrNull(value)) {
|
||||
@@ -223,41 +235,42 @@ public class Validator {
|
||||
return columnConfig.hasPath(path) ? columnConfig.getInt(path) : null;
|
||||
}
|
||||
|
||||
|
||||
private boolean getBoolean(Config columnConfig, String path) {
|
||||
return columnConfig.hasPath(path) && columnConfig.getBoolean(path);
|
||||
}
|
||||
|
||||
private List<String> getStringList(Config columnConfig, String path) {
|
||||
return columnConfig.hasPath(path) ? columnConfig.getStringList(path) : null;
|
||||
}
|
||||
|
||||
public ValidationError isHeaderValid(String[] headerNames) {
|
||||
ValidationError result = null;
|
||||
if (validationConfig != null) {
|
||||
if (validationConfig.hasPath("headers")) {
|
||||
Config headerSectionConfig = validationConfig.getConfig("headers");
|
||||
if (headerSectionConfig.hasPath("list")) {
|
||||
List<String> headerConfig = headerSectionConfig.getStringList("list");
|
||||
if (headerConfig != null) {
|
||||
if (headerNames.length != headerConfig.size()) {
|
||||
result = ValidationError.withoutLineNumber().add("validation.message.header.length",
|
||||
Integer.toString(headerNames.length),
|
||||
Integer.toString(headerConfig.size()));
|
||||
return result;
|
||||
}
|
||||
List<String> headerConfig = getStringList(headerSectionConfig, "list");
|
||||
if (headerConfig != null) {
|
||||
if (headerNames.length != headerConfig.size()) {
|
||||
result = ValidationError.withoutLineNumber().add("validation.message.header.length",
|
||||
Integer.toString(headerNames.length),
|
||||
Integer.toString(headerConfig.size()));
|
||||
return result;
|
||||
}
|
||||
|
||||
ValidationError error = ValidationError.withoutLineNumber();
|
||||
ValidationError error = ValidationError.withoutLineNumber();
|
||||
|
||||
for(int i=0; i<headerConfig.size(); i++) {
|
||||
String header = headerConfig.get(i);
|
||||
if (!header.equals(headerNames[i])) {
|
||||
error.add("validation.message.header.match",
|
||||
Integer.toString(i),
|
||||
header,
|
||||
headerNames[i]);
|
||||
}
|
||||
}
|
||||
if (!error.isEmpty()) {
|
||||
result = error;
|
||||
for(int i=0; i<headerConfig.size(); i++) {
|
||||
String header = headerConfig.get(i);
|
||||
if (!header.equals(headerNames[i])) {
|
||||
error.add("validation.message.header.match",
|
||||
Integer.toString(i),
|
||||
header,
|
||||
headerNames[i]);
|
||||
}
|
||||
}
|
||||
if (!error.isEmpty()) {
|
||||
result = error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,5 @@ validation.message.date.format = is not a date of format {0}
|
||||
validation.message.regexp = does not match {0}
|
||||
|
||||
validation.message.header.length = number of headers is not correct! there are {0} but there should be {1}
|
||||
validation.message.header.match = header number {0} does not match "{1}" should be "{3}"
|
||||
validation.message.header.match = header number {0} does not match "{1}" should be "{3}"
|
||||
validation.message.value.of = Value {0} is not part of this list {1}
|
||||
@@ -29,4 +29,5 @@ validation.message.date.format = Das Datumsformat entspricht nicht {0}
|
||||
validation.message.regexp = entspricht nicht dem regul\u00e4ren Ausdruck {0}
|
||||
|
||||
validation.message.header.length = Anzahl der \u00dcberschriften ist nicht korrekt! Es sind {0} aber es sollten {1} sein
|
||||
validation.message.header.match = \u00dcberschrift in Spalte {0} stimmt nicht. "{1}" sollte "{3}" sein
|
||||
validation.message.header.match = \u00dcberschrift in Spalte {0} stimmt nicht. "{1}" sollte "{3}" sein
|
||||
validation.message.value.of = Der Wert {0} ist nicht in der Liste {1} enthalten
|
||||
Reference in New Issue
Block a user