I am using Hibernate Validator (HV) as my JSR 303 validator. However, both in JSR 303 and in HV’s extensions, there does not seem to be any annotation to specify constraints like a key must exist in a Map or only validate the Map value corresponding to a key.
My use case is that some map keys are required to have valid values only if some other property of the bean is set to true. Currently, I used a fake getter and a @AssertTrue annotation, but I’d really like to make this more annotation based.
This can be solved by implementing a custom constraint:
And a validator like this:
For this requirement you could implement a class-level constraint, which allows to access the complete bean with all its attributes in the validator implementation.