<div dir="ltr">Hi all,<div><br></div><div>As per the latest spec draft, a constraint must be either a cross-parameter *or* a generic constraint, but not both at the same time, as otherwise it would be ambiguous whether a constraint on a method refers to the method parameters or return value.</div>
<div><br></div><div style>Most of the time this does not really pose a limitation, but some constraints actually might be both, cross-parameter *and* generic, depending on the specific context. Examples are @ScriptAssert in Hibernate Validator or generic constraints such as this:</div>
<br>@EqualPasswords<br>public void register(String userName, String password, String confirmedPassword) {<br>}<br><br>@EqualPasswords<br>public class ResetPasswordRequest {<div> String userName;<br> String password;<br>
String confirmedPassword;<br>}<div><br></div><div>Based on a recent BVAL issue [1], I'm proposing to introduce a special constraint annotation attribute, "validationAppliesTo", allowing to specify the required behavior at the usage site:</div>
<div><br></div><div>@EqualPasswords(validationAppliesTo=PARAMETERS)<br>public void register(String username, String password, String confirmPassword) {<br>}<br><div><br></div><div>@EqualPasswords(validationAppliesTo=ANNOTATED_ELEMENT)<br>
public class ResetPasswordRequest {<br> String password;<br> String confirmedPassword;<br>}<br></div><div><br></div>
<div style>The following rules would apply:</div><div style><br></div><div style>* If a constraint is annotated with @Constraint and @CrossParameterConstraint, it must define a member "validationAppliesTo". The default value should be ANNOTATED_ELEMENT.</div>
<div style>* If a constraint is annotated with only one of @Constraint/@CrossParameterConstraint, defining a "validationAppliesTo" member doesn't have any special effect</div><div style>* Specifying validationAppliesTo=PARAMETERS anywhere except a method causes ConstraintDeclarationException</div>
<div style><br></div><div style>Any thoughts?</div><div style><br></div><div style>--Gunnar</div>
<div><br></div><div>[1] <a href="https://hibernate.onjira.com/browse/BVAL-340" target="_blank">https://hibernate.onjira.com/browse/BVAL-340</a><br></div></div><div><br></div></div></div>