mirror of
https://github.com/frosch95/SmartCSV.fx.git
synced 2026-04-11 13:38:23 +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,18 +235,20 @@ 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");
|
||||
List<String> headerConfig = getStringList(headerSectionConfig, "list");
|
||||
if (headerConfig != null) {
|
||||
if (headerNames.length != headerConfig.size()) {
|
||||
result = ValidationError.withoutLineNumber().add("validation.message.header.length",
|
||||
@@ -260,7 +274,6 @@ public class Validator {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,3 +22,4 @@ 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.value.of = Value {0} is not part of this list {1}
|
||||
@@ -30,3 +30,4 @@ 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.value.of = Der Wert {0} ist nicht in der Liste {1} enthalten
|
||||
@@ -2,6 +2,8 @@ package ninja.javafx.smartcsv.validation;
|
||||
|
||||
import com.typesafe.config.Config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -51,6 +53,8 @@ public class ConfigMock {
|
||||
when(validatorConfig.getString(validation)).thenReturn((String) value);
|
||||
} else if (value instanceof Integer) {
|
||||
when(validatorConfig.getInt(validation)).thenReturn((Integer) value);
|
||||
} else if (value instanceof List) {
|
||||
when(validatorConfig.getStringList(validation)).thenReturn((List<String>) value);
|
||||
}
|
||||
|
||||
return columnSectionConfig;
|
||||
|
||||
@@ -101,6 +101,8 @@ public class ValidatorTest {
|
||||
{ "column", "regexp", "[a-z]*", "column", "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") },
|
||||
{ "column", "groovy", "value.contains('a')? 'true' : 'no a inside'", "column", "abcdef", true, null },
|
||||
{ "column", "groovy", "value.contains('a')? 'true' : 'no a inside'", "column", "bcdefg", false, new ValidationMessage("no a inside") },
|
||||
{ "column", "value of", asList("a","b","c","d","e"), "column", "c", true, null },
|
||||
{ "column", "value of", asList("a","b","c","d","e"), "column", "f", false, new ValidationMessage("validation.message.value.of", "f", "a, b, c, d, e") },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user