如何在Spring Boot或Java中创建用于变量验证的验证工具。

huangapple go评论99阅读模式
英文:

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);
    }
}

huangapple
  • 本文由 发表于 2020年7月22日 15:20:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63028882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定