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.
「福强私学」来一个?
「福强私学」, 一部沉淀了个人成长、技术与架构、组织与管理以及商业上的方法与心法的百科全书。
开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。