Valang Validator under the hood

Valang Validator under the hood

How to Convert Valang syntax Expression into ValidationRule Object model?

org.springmodules.validation.valang.parser.ValangParser is the key class that will help on this task.

If you are able to construct a valid valang-syntax expression from some other sources, you can use ValangParser to parse these kinds of expressions into Valang’s Object model. for example:

Errors errors = ...;
Object target = ...;
ValangParser parser = new ValangParser("{ <key> : <expression> : <error_message> [ : <error_key> [ : <error_args> ] ]}");
try {
    Collection<ValidationRule> rules = parser.parseValidation();
        Iterator<ValidationRule> iter = rules.iterator();
            ValidationRule rule =;
            rule.validate(target, errors);
} catch (ParseException e) {
    // handle exception here.

with sample code above, I think you can figure out how the ValangValidator class do its work.

Since you can “setValang(String valang)”, you can “setCustomFunctions(..)”, in the “validate(Object target, Errors errors)” method, the ValangValidator only need use ValangParser to parse the expression set via “setValang(String)” method. After a collection of ValidationRule is available, the left things is almost the same like code above.

Of course, since ValangValidator use ValangParser to do the parsing things, you can use ValangValidator or its super class, that’s, org.springmodules.validation.valang.parser.SimpleValangBased , to do the same thing. I mean, to parse the valang expression.

Custom ValangValidator or ValidationRule

when I want to add GlobalError support for ROMA framework, I found that as if Valang doesn’t support such GlobalError expression things, so I have to find another way.

In a valang-syntax expression, the first token is the , it’s mandatory. But for a global error, it’s meaningless. so even we provide a dummy value, and make the expression-parsing pass, when we invoke the “#validate(target, errors)” method of ValidationRule, an exception will still be raised, because, the ValidationRule can’t find a property on the target object. In order to fix this, we have to break down the “#validate(target, errors)” method’s logic. Here is what I will do.

If we inspect the type of the ValidationRule returned from “parser.parseValidation()”, we will find that it’s type is org.springmodules.validation.valang.predicates.BasicValidationRule . This is the default value object that hold every part of the parsed Valang expression. Since we can get everything with it, we then can filter the returned collection of ValidationRule. The code seems like:

ValangValidator validator = new ValangValidator();
        Collection<ValidationRule> rules = validator.getRules();
        Collection<ValidationRule> globalErrorRules = CollectionUtils.transformedCollection(rules, new Transformer() {
            public Object transform(Object arg) {
                final BasicValidationRule rule = (BasicValidationRule)arg;
                return new ValidationRule() {
                    public void validate(Object target, org.springframework.validation.Errors errors) {
                        String errorKey = rule.getErrorKey();
                        String message = rule.getErrorMessage();
                        Collection args = rule.getErrorArgs();
                            errors.reject(errorKey, message);
                            Object[] argArray = args.toArray(new Object[args.size()]);
                            errors.reject(errorKey, argArray, message);

since FiledError is added with “#rejectValue(..)”, we use “#reject(..)” to fill GlobalError to Errors . After these rules are applied to the target object, the corresponding global errors will be available. You can pull them in you view via spring’s RequestContext or other way you resort to.

>>>>>> 更多阅读 <<<<<<


跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。

  • 一个ChatGPT触达不到的地方
  • 一个带你超越AI/人工智能的地方
  • 一个与你一起成长的地方