[bv-dev] Distinguishing cross-parameter and generic constraints

Matt Benson mbenson at apache.org
Fri Jan 11 10:48:11 EST 2013


On Fri, Jan 11, 2013 at 9:47 AM, Matt Benson <mbenson at apache.org> wrote:

> Hi Gunnar,
>   I kind of like the idea of being able to use a single constraint
> definition type for either purpose, in that it avoids forcing users through
> semantic obstacles in expressing some basic concept.  I'm less enthralled
> with another "magic" annotation element name, but I don't have a better
> idea at the moment either.  I'm disagree with the idea that the default
> 'validationAppliesTo' value should be
>

s/I'm disagree/I disagree/ :P


> ANNOTATED_ELEMENT; I personally would rather see a default such as
> IMPLICIT whose presence requires that only one of @Constraint or
> @CrossParameterConstraint be present on the annotation type.
>
> Matt
>
>
> On Fri, Jan 11, 2013 at 9:22 AM, Gunnar Morling <gunnar at hibernate.org>wrote:
>
>> Anyone with thoughts/feedback on this?
>>
>> Thanks,
>>
>> --Gunnar
>>
>>
>> 2013/1/7 Gunnar Morling <gunnar at hibernate.org>
>>
>>> Hi all,
>>>
>>> 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.
>>>
>>> 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:
>>>
>>> @EqualPasswords
>>> public void register(String userName, String password, String
>>> confirmedPassword) {
>>> }
>>>
>>> @EqualPasswords
>>> public class ResetPasswordRequest {
>>>     String userName;
>>>     String password;
>>>     String confirmedPassword;
>>> }
>>>
>>> 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:
>>>
>>> @EqualPasswords(validationAppliesTo=PARAMETERS)
>>> public void register(String username, String password, String
>>> confirmPassword) {
>>> }
>>>
>>> @EqualPasswords(validationAppliesTo=ANNOTATED_ELEMENT)
>>> public class ResetPasswordRequest {
>>>     String password;
>>>     String confirmedPassword;
>>> }
>>>
>>> The following rules would apply:
>>>
>>> * If a constraint is annotated with @Constraint and
>>> @CrossParameterConstraint, it must define a member "validationAppliesTo".
>>> The default value should be ANNOTATED_ELEMENT.
>>> * If a constraint is annotated with only one
>>> of @Constraint/@CrossParameterConstraint, defining a "validationAppliesTo"
>>> member doesn't have any special effect
>>> * Specifying validationAppliesTo=PARAMETERS anywhere except a method
>>> causes ConstraintDeclarationException
>>>
>>> Any thoughts?
>>>
>>> --Gunnar
>>>
>>> [1] https://hibernate.onjira.com/browse/BVAL-340
>>>
>>>
>>
>> _______________________________________________
>> beanvalidation-dev mailing list
>> beanvalidation-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/beanvalidation-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/beanvalidation-dev/attachments/20130111/da8c6304/attachment.html 


More information about the beanvalidation-dev mailing list