Reputation: 516
I am downloading a json file and contents are available as arrays of arrays.
There are various validation(notNull, isNumber etc) which I want to do for the elements of child array.
One was is when I am using Spring batch and enable ValidatingItemProcessor and Bean Validation works for me.
However, I want to write a standalone solution using already existing Validator frameworks like from Apache but do not want to validate as bean but directly on array.
What should be my approach to this problem.
I am using Spring framework, so anything around that would be helpful.
Upvotes: 0
Views: 309
Reputation: 2239
We used JSR validations for our spring batch application. We annotated our classes with validations such as @NotNull,@DecimalMin. We then created a CommonValidator such as
import javax.validation.Validator;
...
public class CommonValidator<T> implements ItemProcessor<T, T>{
@Autowired
private Validator validator;
public T process(T t) throws Exception{
Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
return constraintViolations.isEmpty()? t : null;
}
We then, added it in a CompositeItemProcessor as follows.
<bean id="validateProcessor" class="mypackage.CommonValidator" />
class="org.springframework..CompositeItemProcessor">
<property name="delegates">
<list>
<ref bean="validateProcessor"/>
<ref bean="otherProcessor"/>
And it worked. On the similar lines, you can write your own validator to validate your array. So, if the array values are valid, then the array is returned. null is returned for an invalid array.
Upvotes: 1