[bv-dev] Fwd: Limitations found in previous bean validation

Gunnar Morling gunnar at hibernate.org
Tue Feb 7 03:01:53 EST 2017

Hi Anders,

Thanks a lot for getting in touch and sharing your experiences and suggestions.

Some questions below inline.


2017-02-06 23:35 GMT+01:00 Anders Persson <thoroughsoft at gmail.com>:
> Hi all,
> So a couple of months back I had a bean structure that I wanted to validate.
> My selection was to use the validation framework.
> I could after a while see that the framework specification, and hence the
> implementations, fell short in several areas so my ideas below are based on
> a live project.
> 1) Sometimes the contents of one bean depends on the contents of another
> bean. The current specification does not in any way allow navigating the
> bean tree despite this information being available internally of the
> implementations. Also accessing the bean instances to get hold of the data
> is not supported. The closest to a solution here was the Hibernate
> implementation which did add a proprietary interface to extract the data
> using PropertyNode. There is however a somewhat strange limitation in the
> Hibernate implementation in that navigating the bean path does not allow you
> to access the root bean, only the first children under the bean. This means
> if the relevant data is located in another branch under the root node there
> is no way to gain access to the data.
> Solution: Allow full navigation up and down the bean structure and allow
> accessing the bean instance to get hold of the data. A framework should not
> impose limitations to what validation a user wants to do. This means make
> the getValue method of Hibernate PropertyNode part of the standard and add a
> getParent() to, for instance, Path.BeanNode to allow moving up the path.

Can you give an example/use case for 1)? Is it that validation of bean
A depends on the state of a bean B referenced by A? If so, couldn't
you put this validation logic to B instead of A? And how do you
interact with PathNode here? Discussing a specific example would help
to grasp the full picture.

> 2) Sometimes the bean structure can consist of lists with a large number of
> elements. The current solution with index number makes it very cumbersome to
> match a failed validation to whatever source of data you have (database, xml
> file, input from webservice etc). In many cases an element (bean) contains
> some data which can be considered as a "primary key" which makes it easy to
> identify in the input data. To handle this I created a StringId interface
> that the beans would implement and I then used this as output when parsing
> the validation result. This however requires me to implement a formatter for
> something that I think should be supported out of the box by the validation
> specification.
> Solution: Create an interface (of annotation, I have no strong feeling about
> the actual implementation) that can be used to identify elements in a list.

Interesting one. Can you elaborate a bit on how you envision this
interface to look like and how it would be used?

> 3) Sometimes the validation is situation dependent, for instance, the
> expected bean contents depend on where the data is coming from or at what
> point in time in the business process it is being validated. It would be
> desirable to easily pass in a settings object in the validation method.
> Solution: Extend ConstraintValidatorContext to allow passing user data or
> extend the validation method to take an arbitrary Object given by the user.

Do you want to apply different constraints in different "situations"?
If so, validation groups should be helpful.

If you want one constraint to behave situation-specific (not sure
whether it's a good idea), there is a solution if you work with a
container such as CDI or Spring: Inject the required context using the
correct scope (e.g. request-scoped) into ConstraintValidator

> I hope that I have been able to make my ideas clear and that there are not
> too many errors in what I have written. It has been a while since I wrote
> this code and I have since changed company so I can't check and verify that
> I have gotten all details right.
> Anders
> _______________________________________________
> beanvalidation-dev mailing list
> beanvalidation-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/beanvalidation-dev

More information about the beanvalidation-dev mailing list