Hi All
I have spent a fair amount of time today trying to implement a new implementation of an existing validation annotation @Size. My use case means that I am validating a set of classes which wrap a string object in in an outer class:
Code:
public class SimpleElement {
private String innerString = null;
....
}
My aim was to declare an annotation such as this
@Size(min=1)
private SimpleElement name = null;
and process it using code like this
Code:
public class SizeValidatorForSimpleElementType implements ConstraintValidator<Size, SimpleElementType>{
private SizeValidatorForString sizeValidatorForString = null;
@Override
public void initialize(Size size) {
this.sizeValidatorForString = new SizeValidatorForString();
this.sizeValidatorForString.initialize(size);
}
@Override
public boolean isValid(SimpleElementType value, ConstraintValidatorContext context) {
return sizeValidatorForString.isValid(value == null ? null : value.getBodyText() , context);
}
}
I was expecting to have to indicate to the factory in some way that it should supplement its standard mappings with an additional map of entries.
I have looked quite deeply and failed to find a methodology of doing this, the factory seems to use the org.hibernate.validator.metadata.ConstraintHelper class to map the entries. The factory also sees to implement a single instance of this class which it manages in a private manner so that it is not exposed in order that the inner mappings can be extended.
I am more than aware that the factory must have a method of mapping the interface to the implementations, I am though surprised that the factory does not allow the existing mappings to be extended or overridden.
Am I missing something, is there a deeply veiled way of doing this that I am missing, the alternative is that I implement my own identical version of the Size attribute and map it using the @Constraint annotation as per the examples, but this just seems daft to me.
If anyone can help and point me at a method of doing this I would appreciate it, I am also genuinely interested in the reasoning behind the code being non extensible in this way is there something in jsr303 that I have missed that prohibits this?
I appreciate any answers to this even if it is just to point me at at a thread I have failed to find in my numerous searches of the user groups.
Thanks In advance
Chris