英文:
How to create Validation utility for variable validation in spring boot or java
问题
I have some variable, for them validation rule may change in run time (getting validation rules from Datbase) like:
String name;
Rules: - {Max:10, Min:3, SpacialSymbol:false, AlphaNumeric:true,...etc}
String age;
Rules: - {max:100, min:19, numeric:true, alphaNumeric:false}
So according to the variable rules may change during runtime...
I want ValidationUtil.isValid(Object variableValue, Object rules)
in Validation utility.
static boolean isValid() {//rule implementation}
How can I do it? in a generic way for validating all rules. I do not want to use if-else statements.
英文:
I have some variable, for them validation rule may change in run time(getting validation rules from Datbase) like
String name ;
Rules: - {Max:10, Min:3, SpacialSymbol:false, AlphaNumeric:ture,...etc}
String age;
Rules: - {max:100,min:19,numeric:true,alphaNumeric:false}
So according to the variable rules may change during runtime...
I want ValidationUtl.isValid(Object variableValue,Object rules)
In Validation utility
static boolean isValid(){//rule implementation}
How can I do it ? in a generic way for validating all rules. I do not want to use if else statement.
答案1
得分: 1
你声明了一个接口
interface Rule<T> {
boolean isValid(T obj);
}
然后,你可以在 Rule
实现类中封装各种检查
class MinLength implements Rule<String> {
private final int min;
MinLength(int min) {
this.min = min;
}
@Override
public boolean isValid(String obj) {
return obj.length() >= min;
}
}
class IsAlphaNumeric implements Rule<String> {
@Override
public boolean isValid(String obj) {
return true; //TODO: 实现字母数字检查
}
}
// 其他规则
最后,你需要动态从数据库中获取规则并与 Rule
实现类关联。
public class StringValidator {
public boolean validate(String string, List<Rule<String>> rules) {
boolean isValid = true;
for (Rule<String> rule : rules) {
if (rule.isValid(string)) {
isValid = false;
break;
}
}
return isValid;
}
public static void main(String[] args) {
List<Rule<String>> rules = Arrays.asList(new MinLength(10), new IsAlphaNumeric()); //从数据库获取
boolean isValid = new StringValidator().validate("blablabla", rules);
}
}
英文:
You declare an interface
interface Rule<T> {
boolean isValid(T obj);
}
and then you can encapsulate various checks inside Rule
implementations
class MinLength implements Rule<String> {
private final int min;
MinLength(int min) {
this.min = min;
}
@Override
public boolean isValid(String obj) {
return obj.length() >= min;
}
}
class IsAlphaNumeric implements Rule<String> {
@Override
public boolean isValid(String obj) {
return true; //TODO: implement the alpha-numeric check
}
}
// other rules
The last thing you will need is to dynamically obtain rules from your Database and link them with Rule
implementations.
public class StringValidator {
public boolean validate(String string, List<Rule<String>> rules) {
boolean isValid = true;
for (Rule<String> rule : rules) {
if (rule.isValid(string)) {
isValid = false;
break;
}
}
return isValid;
}
public static void main(String[] args) {
List<Rule<String>> rules = Arrays.asList(new MinLength(10), new IsAlphaNumeric()); //obtain from Database
boolean isValid = new StringValidator().validate("blablabla", rules);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论