support list of possible values

This commit is contained in:
Andreas Billmann
2015-12-17 23:41:20 +01:00
parent fc8627ff18
commit a67937a262
5 changed files with 45 additions and 24 deletions

View File

@@ -36,6 +36,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.validator.GenericValidator.*; import static org.apache.commons.validator.GenericValidator.*;
/** /**
@@ -94,6 +95,7 @@ public class Validator {
checkMinLength(columnConfig, value, error); checkMinLength(columnConfig, value, error);
checkInteger(columnConfig, value, error); checkInteger(columnConfig, value, error);
checkGroovy(column, columnConfig, value, error); checkGroovy(column, columnConfig, value, error);
checkValueOf(columnConfig, value, error);
} }
if (!error.isEmpty()) { if (!error.isEmpty()) {
@@ -146,6 +148,16 @@ public class Validator {
return groovyResult.equals(true) || groovyResult.toString().trim().toLowerCase().equals("true"); 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) { private void checkBlankOrNull(Config columnConfig, String value, ValidationError error) {
if (getBoolean(columnConfig, "not empty")) { if (getBoolean(columnConfig, "not empty")) {
if (isBlankOrNull(value)) { if (isBlankOrNull(value)) {
@@ -223,41 +235,42 @@ public class Validator {
return columnConfig.hasPath(path) ? columnConfig.getInt(path) : null; return columnConfig.hasPath(path) ? columnConfig.getInt(path) : null;
} }
private boolean getBoolean(Config columnConfig, String path) { private boolean getBoolean(Config columnConfig, String path) {
return columnConfig.hasPath(path) && columnConfig.getBoolean(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) { public ValidationError isHeaderValid(String[] headerNames) {
ValidationError result = null; ValidationError result = null;
if (validationConfig != null) { if (validationConfig != null) {
if (validationConfig.hasPath("headers")) { if (validationConfig.hasPath("headers")) {
Config headerSectionConfig = validationConfig.getConfig("headers"); Config headerSectionConfig = validationConfig.getConfig("headers");
if (headerSectionConfig.hasPath("list")) { List<String> headerConfig = getStringList(headerSectionConfig, "list");
List<String> headerConfig = headerSectionConfig.getStringList("list"); if (headerConfig != null) {
if (headerConfig != null) { if (headerNames.length != headerConfig.size()) {
if (headerNames.length != headerConfig.size()) { result = ValidationError.withoutLineNumber().add("validation.message.header.length",
result = ValidationError.withoutLineNumber().add("validation.message.header.length", Integer.toString(headerNames.length),
Integer.toString(headerNames.length), Integer.toString(headerConfig.size()));
Integer.toString(headerConfig.size())); return result;
return result; }
}
ValidationError error = ValidationError.withoutLineNumber(); ValidationError error = ValidationError.withoutLineNumber();
for(int i=0; i<headerConfig.size(); i++) { for(int i=0; i<headerConfig.size(); i++) {
String header = headerConfig.get(i); String header = headerConfig.get(i);
if (!header.equals(headerNames[i])) { if (!header.equals(headerNames[i])) {
error.add("validation.message.header.match", error.add("validation.message.header.match",
Integer.toString(i), Integer.toString(i),
header, header,
headerNames[i]); headerNames[i]);
}
}
if (!error.isEmpty()) {
result = error;
} }
} }
if (!error.isEmpty()) {
result = error;
}
} }
} }
} }

View File

@@ -21,4 +21,5 @@ validation.message.date.format = is not a date of format {0}
validation.message.regexp = does not match {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.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}

View File

@@ -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.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.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

View File

@@ -2,6 +2,8 @@ package ninja.javafx.smartcsv.validation;
import com.typesafe.config.Config; import com.typesafe.config.Config;
import java.util.List;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@@ -51,6 +53,8 @@ public class ConfigMock {
when(validatorConfig.getString(validation)).thenReturn((String) value); when(validatorConfig.getString(validation)).thenReturn((String) value);
} else if (value instanceof Integer) { } else if (value instanceof Integer) {
when(validatorConfig.getInt(validation)).thenReturn((Integer) value); when(validatorConfig.getInt(validation)).thenReturn((Integer) value);
} else if (value instanceof List) {
when(validatorConfig.getStringList(validation)).thenReturn((List<String>) value);
} }
return columnSectionConfig; return columnSectionConfig;

View File

@@ -101,6 +101,8 @@ public class ValidatorTest {
{ "column", "regexp", "[a-z]*", "column", "abcA", false, new ValidationMessage("validation.message.regexp", "[a-z]*") }, { "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", "abcdef", true, null },
{ "column", "groovy", "value.contains('a')? 'true' : 'no a inside'", "column", "bcdefg", false, new ValidationMessage("no a inside") }, { "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") },
}); });
} }