What I would like to do is define a class with constraints which are all enforced by default, but some select constraints can be relaxed in certain circumstances. This seemed like a perfect application of groups.
So I define the following for example:
{ quote} { public interface Relaxed \{}; public interface Complete \{};
@GroupSequence(\{Complete.class, A.class}) class A \{ @NotNull public String a1;
@NotNull(groups=Complete.class) public String a2; } {quote } }
My understanding was that default validation should cause both constraints to be enforced. However with this set up the constraint on a1 is never enforced. If I remove the groups=Complete.class setting from the constraint on a2 then the constraint on a1 is enforced. Why does a2's constraint's groups affect evaluation of constraints on a1?
Even assuming the above behavior is incorrect, then I don't think there's any way to actually achieve what I want since there doesn't seem to be any way to invoke validation such that only the constraint on a1 is enforced. I thought I'd be able to invoke validate as:
{quote}A a = new A(); a.a1 = "not null"; a.a2 = null; violations = validator.validate(a, Relaxed.class, A.class}; {quote}
kind of like an inline GroupSequence but this throws an exception saying groups can only be interfaces.
I want the default to be such that all constraints are enforced so that consumers of this class only relax the constraints when they know they need to.
So questions are why
1. Why doesn't the @NotNull constraint on a1 ever get enforced? 2. Is there a way to have only constraints with empty group definition enforced?
Cheers,
Tim |
|