|
|
@ -1,7 +1,9 @@ |
|
|
|
package io.sc.engine.rule.core; |
|
|
|
|
|
|
|
import io.sc.platform.util.StringUtil; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
@ -12,34 +14,50 @@ import java.util.regex.Pattern; |
|
|
|
* |
|
|
|
*/ |
|
|
|
public class FieldValidator { |
|
|
|
private static final Pattern EMAIL_PATTERN = Pattern.compile("[a-zA-Z0-9_\\\\-\\\\.]+@[a-zA-Z0-9]+(\\\\.(com))"); |
|
|
|
private static final Pattern EMAIL_PATTERN = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); |
|
|
|
|
|
|
|
public static boolean empty(String fieldCode,String fieldName,Object obj,ValidateResult result) { |
|
|
|
public static boolean empty(String fieldCode,String fieldName,Object obj,String tip,ValidateResult result) { |
|
|
|
if(obj!=null) { |
|
|
|
result.error(fieldCode,fieldName, "only null or empty string value can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode, fieldName, "only null or empty string value can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean notEmpty(String fieldCode,String fieldName,Object obj,ValidateResult result) { |
|
|
|
public static boolean notEmpty(String fieldCode,String fieldName,Object obj,String tip,ValidateResult result) { |
|
|
|
if(obj==null) { |
|
|
|
result.error(fieldCode,fieldName, "not null value can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode, fieldName, "not null value can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
if(obj instanceof String) { |
|
|
|
if(obj.toString().isEmpty()) { |
|
|
|
result.error(fieldCode,fieldName, "not empty string value can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode, fieldName, "not empty string value can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean trueValue(String fieldCode,String fieldName,Object obj,ValidateResult result) { |
|
|
|
public static boolean trueValue(String fieldCode,String fieldName,Object obj,String tip,ValidateResult result) { |
|
|
|
if(obj instanceof Boolean) { |
|
|
|
if(!((Boolean)obj)) { |
|
|
|
result.error(fieldCode,fieldName, "true value can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode, fieldName, "true value can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
return true; |
|
|
@ -49,10 +67,14 @@ public class FieldValidator { |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean falseValue(String fieldCode,String fieldName,Object obj,ValidateResult result) { |
|
|
|
public static boolean falseValue(String fieldCode,String fieldName,Object obj,String tip,ValidateResult result) { |
|
|
|
if(obj instanceof Boolean) { |
|
|
|
if(((Boolean)obj)) { |
|
|
|
result.error(fieldCode,fieldName, "false value can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode, fieldName, "false value can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
return true; |
|
|
@ -61,139 +83,231 @@ public class FieldValidator { |
|
|
|
result.error(fieldCode,fieldName, "only boolean value can been accepted"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean integerRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Integer min,Integer max,Boolean maxInclude,ValidateResult result) { |
|
|
|
if(obj instanceof Integer) { |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
Integer value =(Integer)obj; |
|
|
|
if((min!=null && value<min) || (max!=null && value>max)) { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("integer ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
|
|
|
|
public static boolean numberRange(String fieldCode,String fieldName,Number obj,Boolean minInclude,Number min,Number max,Boolean maxInclude,String tip,ValidateResult result){ |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
double value =obj.doubleValue(); |
|
|
|
double minValue =min.doubleValue(); |
|
|
|
double maxValue =max.doubleValue(); |
|
|
|
if((min!=null && value<minValue) || (max!=null && value>maxValue)) { |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
if((minInclude==null || !minInclude) && value==min){ |
|
|
|
result.error(fieldCode, fieldName, StringUtil.format("integer ${0} can been accepted", errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
if((minInclude==null || !minInclude) && value==minValue){ |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("integer ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
if((maxInclude!=null || !maxInclude) && value==max){ |
|
|
|
return false; |
|
|
|
} |
|
|
|
if((maxInclude==null || !maxInclude) && value==maxValue){ |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("integer ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
result.error(fieldCode,fieldName, "only integer value can been accepted"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean decimalRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Double min,Double max,Boolean maxInclude,ValidateResult result) { |
|
|
|
if(obj instanceof Double) { |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
Double value =(Double)obj; |
|
|
|
if((min!=null && value<min) || (max!=null && value>max)) { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("decimal ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
if((minInclude==null || !minInclude) && value==min){ |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("decimal ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
if((maxInclude!=null || !maxInclude) && value==max){ |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("decimal ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
result.error(fieldCode,fieldName, "only double value can been accepted"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// public static boolean integerRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Long min,Long max,Boolean maxInclude,String tip,ValidateResult result) {
|
|
|
|
// if(obj instanceof Integer || obj instanceof Long) {
|
|
|
|
// if(min==null && max==null) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude);
|
|
|
|
// Long value =(Long)obj;
|
|
|
|
// if((min!=null && value<min) || (max!=null && value>max)) {
|
|
|
|
// if(StringUtils.hasText(tip)){
|
|
|
|
// result.error(fieldCode, fieldName, tip);
|
|
|
|
// }else {
|
|
|
|
// result.error(fieldCode, fieldName, StringUtil.format("integer ${0} can been accepted", errorMessage));
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }else {
|
|
|
|
// if((minInclude==null || !minInclude) && value==min){
|
|
|
|
// if(StringUtils.hasText(tip)){
|
|
|
|
// result.error(fieldCode, fieldName, tip);
|
|
|
|
// }else {
|
|
|
|
// result.error(fieldCode,fieldName, StringUtil.format("integer ${0} can been accepted",errorMessage));
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// if((maxInclude!=null || !maxInclude) && value==max){
|
|
|
|
// if(StringUtils.hasText(tip)){
|
|
|
|
// result.error(fieldCode, fieldName, tip);
|
|
|
|
// }else {
|
|
|
|
// result.error(fieldCode,fieldName, StringUtil.format("integer ${0} can been accepted",errorMessage));
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// result.error(fieldCode,fieldName, "only integer value can been accepted");
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// public static boolean decimalRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Double min,Double max,Boolean maxInclude,String tip,ValidateResult result) {
|
|
|
|
// if(obj instanceof Double || obj instanceof BigDecimal) {
|
|
|
|
// if(min==null && max==null) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude);
|
|
|
|
// Double value =(Double)obj;
|
|
|
|
// if((min!=null && value<min) || (max!=null && value>max)) {
|
|
|
|
// if(StringUtils.hasText(tip)){
|
|
|
|
// result.error(fieldCode, fieldName, tip);
|
|
|
|
// }else {
|
|
|
|
// result.error(fieldCode,fieldName, StringUtil.format("decimal ${0} can been accepted",errorMessage));
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }else {
|
|
|
|
// if((minInclude==null || !minInclude) && value==min){
|
|
|
|
// if(StringUtils.hasText(tip)){
|
|
|
|
// result.error(fieldCode, fieldName, tip);
|
|
|
|
// }else {
|
|
|
|
// result.error(fieldCode,fieldName, StringUtil.format("decimal ${0} can been accepted",errorMessage));
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// if((maxInclude!=null || !maxInclude) && value==max){
|
|
|
|
// if(StringUtils.hasText(tip)){
|
|
|
|
// result.error(fieldCode, fieldName, tip);
|
|
|
|
// }else {
|
|
|
|
// result.error(fieldCode,fieldName, StringUtil.format("decimal ${0} can been accepted",errorMessage));
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// result.error(fieldCode,fieldName, "only double value can been accepted");
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
|
|
|
|
public static boolean lengthRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Integer min,Integer max,Boolean maxInclude,ValidateResult result) { |
|
|
|
public static boolean lengthRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Integer min,Integer max,Boolean maxInclude,String tip,ValidateResult result) { |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
if(obj instanceof String) { |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
String value =(String)obj; |
|
|
|
if((min!=null && value.length()<min) || (max!=null && value.length()>max)) { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
if((minInclude==null || !minInclude) && value.length()==min){ |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
if((maxInclude!=null || !maxInclude) && value.length()==max){ |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
if((maxInclude==null || !maxInclude) && value.length()==max){ |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
result.error(fieldCode,fieldName, "only string value can been accepted"); |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("length ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean dateRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Date min,Date max,Boolean maxInclude,ValidateResult result) { |
|
|
|
|
|
|
|
public static boolean dateRange(String fieldCode,String fieldName,Object obj,Boolean minInclude,Date min,Date max,Boolean maxInclude,String tip,ValidateResult result) { |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
if(obj instanceof Date) { |
|
|
|
if(min==null && max==null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
String errorMessage =getRangeRestrictionMessage(minInclude,min,max,maxInclude); |
|
|
|
Date value =(Date)obj; |
|
|
|
if((min!=null && value.getTime()<min.getTime()) || (max!=null && value.getTime()>max.getTime())) { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
if((minInclude==null || !minInclude) && value.getTime()==min.getTime()){ |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
if((maxInclude!=null || !maxInclude) && value.getTime()==max.getTime()){ |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
if((maxInclude==null || !maxInclude) && value.getTime()==max.getTime()){ |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
result.error(fieldCode,fieldName, "only date value can been accepted"); |
|
|
|
result.error(fieldCode,fieldName, StringUtil.format("date ${0} can been accepted",errorMessage)); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean mail(String fieldCode,String fieldName,Object obj,ValidateResult result) { |
|
|
|
public static boolean mail(String fieldCode,String fieldName,Object obj,String tip,ValidateResult result) { |
|
|
|
if(obj instanceof String) { |
|
|
|
String value =(String)obj; |
|
|
|
Matcher m = EMAIL_PATTERN.matcher(value); |
|
|
|
if(!m.matches()) { |
|
|
|
result.error(fieldCode,fieldName, "only email address can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, "only email address can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
result.error(fieldCode,fieldName, "only string value can been accepted"); |
|
|
|
result.error(fieldCode,fieldName, "only email address can been accepted"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean pattern(String fieldCode,String fieldName,Object obj,String pattern,ValidateResult result) { |
|
|
|
public static boolean pattern(String fieldCode,String fieldName,Object obj,String pattern,String tip,ValidateResult result) { |
|
|
|
if(obj instanceof String) { |
|
|
|
String value =(String)obj; |
|
|
|
Pattern p = Pattern.compile(pattern); |
|
|
|
Matcher m = p.matcher(value); |
|
|
|
if(!m.matches()) { |
|
|
|
result.error(fieldCode,fieldName, "only string value matched " + pattern + " pattern can been accepted"); |
|
|
|
if(StringUtils.hasText(tip)){ |
|
|
|
result.error(fieldCode, fieldName, tip); |
|
|
|
}else { |
|
|
|
result.error(fieldCode,fieldName, "only string value matched " + pattern + " pattern can been accepted"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else { |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
result.error(fieldCode,fieldName, "only string value can been accepted"); |
|
|
|
result.error(fieldCode,fieldName, "only string value matched " + pattern + " pattern can been accepted"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|