The basic idea is to have a simple SPI:
Code:
public interface CompositeValidator<T> {
void validate(T value, CompositeValidatorContext context);
}
When HV comes across a @Valid annotation it looks up a CompositeValidator<T> by the type annotated with @Valid and calls the validate() method. Validation across Optional<T> would be implemented as:
Code:
public class OptionalValidator implements CompositeValidator<Optional<?>> {
// This is an internal HV factory
private final CompositeValidatorProvider provider;
// This is an annotations/type decriptor
// For bean property / method parameter / list item / map entry / optional value etc
private final Element element;
public OptionalValidator(CompositeValidatorProvider provider, Element element) {
this.provider = provider;
this.element = element;
}
@Override
public void validate(Optional<?> value, CompositeValidatorContext context) {
if (value.isPresent()) {
Element presentElement = element.getElementArgument(0);
provider.get(presentElement).validate(value.get(), context);
}
}
}
As I said already, I think the above could also be used for Collection<T>, Map<K,V>, but I think it could be generalised for beans, method parameters as well.
Hope the above make sense.
regards,
George