2015-11-28 23:06:14 +01:00
|
|
|
/*
|
|
|
|
|
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 groovy.lang.Binding;
|
|
|
|
|
import groovy.lang.GroovyShell;
|
|
|
|
|
import groovy.lang.Script;
|
|
|
|
|
import org.codehaus.groovy.control.CompilationFailedException;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2015-12-04 16:22:21 +01:00
|
|
|
import java.util.List;
|
2015-11-28 23:06:14 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
2015-12-17 23:41:20 +01:00
|
|
|
import static java.util.stream.Collectors.joining;
|
2015-11-28 23:06:14 +01:00
|
|
|
import static org.apache.commons.validator.GenericValidator.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class checks all the validations defined in the
|
|
|
|
|
* Config against a given value
|
|
|
|
|
*/
|
|
|
|
|
public class Validator {
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// member variables
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2016-02-02 03:20:14 +01:00
|
|
|
private ValidationConfiguration validationConfig;
|
2016-08-07 16:57:55 +02:00
|
|
|
private ColumnValidations columnValidations = new ColumnValidations();
|
2015-11-28 23:06:14 +01:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// constructors
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JSON configuration for this validator
|
|
|
|
|
* @param validationConfig
|
|
|
|
|
*/
|
2016-02-02 03:20:14 +01:00
|
|
|
public Validator(ValidationConfiguration validationConfig) {
|
2015-11-28 23:06:14 +01:00
|
|
|
this.validationConfig = validationConfig;
|
2016-08-07 16:57:55 +02:00
|
|
|
initColumnValidations();
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-07-22 20:45:26 +02:00
|
|
|
// public methods
|
2015-11-28 23:06:14 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* checks if the value is valid for the column configuration
|
|
|
|
|
* @param column the column name
|
|
|
|
|
* @param value the value to check
|
2015-12-07 22:41:59 +01:00
|
|
|
* @return ValidationError with information if valid and if not which getMessage happened
|
2015-11-28 23:06:14 +01:00
|
|
|
*/
|
2016-08-07 16:57:55 +02:00
|
|
|
public ValidationError isValid(Integer lineNumber, String column, String value) {
|
2015-12-07 22:41:59 +01:00
|
|
|
ValidationError result = null;
|
2016-07-22 20:45:26 +02:00
|
|
|
if (hasConfig()) {
|
2016-08-07 16:57:55 +02:00
|
|
|
ValidationError error = columnValidations.isValid(lineNumber, column, value);
|
2016-02-02 03:20:14 +01:00
|
|
|
if (!error.isEmpty()) {
|
|
|
|
|
result = error;
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
|
2016-07-22 20:45:26 +02:00
|
|
|
public boolean hasConfig() {
|
|
|
|
|
return validationConfig != null;
|
|
|
|
|
}
|
2015-11-28 23:06:14 +01:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// private methods
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
private void initColumnValidations() {
|
|
|
|
|
if (hasConfig()) {
|
|
|
|
|
String[] columns = validationConfig.headerNames();
|
|
|
|
|
for(String column: columns) {
|
|
|
|
|
Boolean alphaNumeric = validationConfig.getAlphanumericRuleFor(column);
|
|
|
|
|
if (alphaNumeric != null && alphaNumeric) {
|
|
|
|
|
columnValidations.add(column, new AlphaNumericValidation());
|
2016-08-05 23:09:22 +02:00
|
|
|
}
|
2015-11-28 23:06:14 +01:00
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
Boolean doubleRule = validationConfig.getDoubleRuleFor(column);
|
|
|
|
|
if (doubleRule != null && doubleRule) {
|
|
|
|
|
columnValidations.add(column, new DoubleValidation());
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
Boolean integerRule = validationConfig.getIntegerRuleFor(column);
|
|
|
|
|
if (integerRule != null && integerRule) {
|
|
|
|
|
columnValidations.add(column, new IntegerValidation());
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
Boolean notEmptyRule = validationConfig.getNotEmptyRuleFor(column);
|
|
|
|
|
if (notEmptyRule != null && notEmptyRule) {
|
|
|
|
|
columnValidations.add(column, new NotEmptyValidation());
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
Boolean uniqueRule = validationConfig.getUniqueRuleFor(column);
|
|
|
|
|
if (uniqueRule != null && uniqueRule) {
|
|
|
|
|
columnValidations.add(column, new UniqueValidation());
|
2015-12-17 23:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
String dateRule = validationConfig.getDateRuleFor(column);
|
|
|
|
|
if (dateRule != null && !dateRule.trim().isEmpty()) {
|
|
|
|
|
columnValidations.add(column, new DateValidation(dateRule));
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
Integer minLength = validationConfig.getMinLengthRuleFor(column);
|
|
|
|
|
if (minLength != null) {
|
|
|
|
|
columnValidations.add(column, new MinLengthValidation(minLength));
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
Integer maxLength = validationConfig.getMaxLengthRuleFor(column);
|
|
|
|
|
if (maxLength != null) {
|
|
|
|
|
columnValidations.add(column, new MaxLengthValidation(maxLength));
|
2015-12-17 23:55:08 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
String regexp = validationConfig.getRegexpRuleFor(column);
|
|
|
|
|
if (regexp != null && !regexp.trim().isEmpty()) {
|
|
|
|
|
columnValidations.add(column, new RegExpValidation(regexp));
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
2016-08-07 16:57:55 +02:00
|
|
|
String groovy = validationConfig.getGroovyRuleFor(column);
|
|
|
|
|
if (groovy != null && !groovy.trim().isEmpty()) {
|
|
|
|
|
columnValidations.add(column, new GroovyValidation(groovy));
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
2016-08-07 16:57:55 +02:00
|
|
|
List<String> valueOfRule = validationConfig.getValueOfRuleFor(column);
|
|
|
|
|
if (valueOfRule != null && !valueOfRule.isEmpty()) {
|
|
|
|
|
columnValidations.add(column, new ValueOfValidation(valueOfRule));
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-12-07 22:41:59 +01:00
|
|
|
public ValidationError isHeaderValid(String[] headerNames) {
|
|
|
|
|
ValidationError result = null;
|
2015-12-04 16:22:21 +01:00
|
|
|
if (validationConfig != null) {
|
2016-02-02 03:20:14 +01:00
|
|
|
String[] headerNamesConfig = validationConfig.headerNames();
|
|
|
|
|
if (headerNamesConfig != null) {
|
|
|
|
|
if (headerNames.length != headerNamesConfig.length) {
|
|
|
|
|
result = ValidationError.withoutLineNumber().add("validation.message.header.length",
|
|
|
|
|
Integer.toString(headerNames.length),
|
|
|
|
|
Integer.toString(headerNamesConfig.length));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-12-04 16:22:21 +01:00
|
|
|
|
2016-02-02 03:20:14 +01:00
|
|
|
ValidationError error = ValidationError.withoutLineNumber();
|
2015-12-07 22:41:59 +01:00
|
|
|
|
2016-02-02 03:20:14 +01:00
|
|
|
for(int i=0; i<headerNamesConfig.length; i++) {
|
|
|
|
|
if (!headerNamesConfig[i].equals(headerNames[i])) {
|
|
|
|
|
error.add("validation.message.header.match",
|
|
|
|
|
Integer.toString(i),
|
|
|
|
|
headerNamesConfig[i],
|
|
|
|
|
headerNames[i]);
|
2015-12-17 23:41:20 +01:00
|
|
|
}
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|
2016-02-02 03:20:14 +01:00
|
|
|
if (!error.isEmpty()) {
|
|
|
|
|
result = error;
|
|
|
|
|
}
|
2015-12-04 16:22:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2015-11-28 23:06:14 +01:00
|
|
|
}
|