Let’s say I have a class with two member variables:
import javax.validation.constraints.Min;
class Foo {
private int minimumAge;
private int maximumAge;
}
I know I can validate minimum / maximum values like this:
@Min(1)
private int minimumAge;
@Max(99)
private int maximumAge;
But what I really want to do, is to ensure that minimumAge is always less than or equal to maximumAge. So I want something like this:
@Min(1)
@Max(maximumAge)
private int minimumAge;
@Min(minumumAge)
@Max(99)
private int maximumAge;
But this does not seem possible with this validation framework, since it only can take constant expressions. Is there a way to do something like this?
Thanks!
As Zack said you could use a custom constraint.
With Hibernate Validator you could alternatively work with the @ScriptAssert constraint which allows to define constraints using any JSR 223 compatible scripting engine:
If you really need to declare constraints in a dynamic way you could use Hibernate Validator’s API for programmatic constraint definition: