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 = ...;
= new ValangParser("{ <key> : <expression> : <error_message> [ : <error_key> [ : <error_args> ] ]}");
ValangParser parser try {
Collection<ValidationRule> rules = parser.parseValidation();
if(CollectionUtils.isNotEmpty(rules))
{
Iterator<ValidationRule> iter = rules.iterator();
while(iter.hasNext()){
= iter.next();
ValidationRule rule .validate(target, errors);
rule}
}
} 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
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:
= new ValangValidator();
ValangValidator validator .setValang("");
validator@SuppressWarnings("unchecked")
Collection<ValidationRule> rules = validator.getRules();
@SuppressWarnings("unchecked")
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();
@SuppressWarnings("unchecked")
Collection args = rule.getErrorArgs();
if(CollectionUtils.isEmpty(args))
{
.reject(errorKey, message);
errors}
else
{
@SuppressWarnings("unchecked")
Object[] argArray = args.toArray(new Object[args.size()]);
.reject(errorKey, argArray, message);
errors}
}
};
}
});
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.
「为AI疯狂」星球上,扶墙老师正在和朋友们讨论有趣的AI话题,你要不要⼀起来呀?^-^
这里
- 不但有及时新鲜的AI资讯和深度探讨
- 还分享AI工具、产品方法和商业机会
- 更有体系化精品付费内容等着你,加入星球(https://t.zsxq.com/0dI3ZA0sL) 即可免费领取。(加入之后一定记得看置顶消息呀!)

存量的时代,省钱就是赚钱。
在增量的时代,省钱其实是亏钱。
避坑儿是省钱的一种形式,更是真正聪明人的选择!
弯路虽然也是路,但还是能少走就少走,背后都是高昂的试错成本。
订阅「福报」,少踩坑,少走弯路,多走一步,就是不一样的胜率!
