users@jax-rs-spec.java.net

[jax-rs-spec users] Re: Bean Validation question

From: Santiago Pericas-Geertsen <santiago.pericasgeertsen_at_oracle.com>
Date: Mon, 30 Jul 2012 10:30:27 -0400

Hi Gunnar,

 Inlined ...

> How do you plan to trigger method validation in JAX-RS? Will it be
> triggered by default for any JAX-RS resource class or only for those
> specifically annotated with @MethodValidated:

 By default, if a class/method/field/property has a constraint annotation, then it will be validated.

>
> @Path("/")
> @NonEmptyNames
> @MethodValidated
> class MyResourceClass {
> ...
> @NotNull @FormParam("lastName")
> private String lastName;
> ...
> }
>
> In the first case, you might use @MethodValidated(validationMode=NONE)
> to disable method validation for single types, although I see that
> this is not particularly elegant. So something like @IgnoreValidation
> might be better here.

 Right, especially since it would be nice to have the same mechanism for the non-method cases.

>
> The idea behind @MethodValidated is to control which methods/types
> should be subject to method-level validation (that is, automatic
> validation of method parameters and return values upon method
> invocation). If you wanted to exclude single fields/properties from
> validation, you'd probably better off using validation groups.

 Validation groups are a more advanced concept. I suspect most JAX-RS users would likely just stick to the default group.

>
> What is the use case you have in mind, is it it temporarily disabling
> certain validations during development/debugging only, or also other
> use cases?

 Yes, exactly. Although I've seen production code with certain features disabled as well. The idea is to disable validation without removing the validation code. Of course, this could be achieved using comments, but is much less elegant. In addition, we may want to support disablement for the entire app (e.g, by annotating the JAX-RS Application subclass).

 Thanks.

-- Santiago

>
>> Has this requirement been proposed by any of the other technologies integrating with BV?
>
> Not that I'm aware off, but we haven't been discussing method
> validation with other EGs in really much detail yet.
>
> --Gunnar
>
>
> 2012/7/3 Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>:
>> Hi Gunnar,
>>
>> What we really need in JAX-RS is an annotation that can be used to
>> (temporarily) disable validation at any location: resource classes, fields,
>> properties and methods. Something like @IgnoreValidation that can be used as
>> follows:
>>
>> @Path("/")
>>
>> @NonEmptyNames @IgnoreValidation
>>
>> class MyResourceClass {
>>
>>
>> @NotNull @FormParam("firstName") @IgnoreValidation
>>
>> private String firstName;
>>
>>
>> @NotNull @FormParam("lastName")
>>
>> private String lastName;
>>
>>
>> private String email;
>>
>>
>> @FormParam("email")
>>
>> public void setEmail(String email) {
>>
>> this.email = email;
>>
>> }
>>
>> @Email @IgnoreValidation
>>
>> public String getEmail() {
>>
>> return email;
>>
>> }
>>
>> }
>>
>>
>> Of course, this would be equivalent to removing the BV annotations; but
>> it's often more convenient (debugging, etc.) to disable than to remove. This
>> annotation could potentially be used on an Application sub-class (i.e.,
>> class representing a JAX-RS app) to disable all annotations globally in
>> JAX-RS.
>>
>> Has this requirement been proposed by any of the other technologies
>> integrating with BV?
>>
>> Thanks.
>>
>> -- Santiago
>>
>> On Jun 27, 2012, at 6:19 PM, Gunnar Morling wrote:
>>
>> Hi Santiago,
>>
>> what we currently do have is an annotation, @MethodValidated [1],
>> which can be used to specify how method validation shall be applied
>> for a given type.
>>
>> Our current thinking is that Bean Validation itself wouldn't evaluate
>> this annotation and trigger method validation. Instead this would be
>> done by the integrating technology, here JAX-RS. So also this
>> integrating technology would be in charge of globally
>> enabling/disabling method validation.
>>
>> Based on that, I'm seeing two options now:
>>
>> * JAX-RS checks resources for the annotation and performs method
>> validation for those types/methods, where the annotation is found
>> * JAX-RS performs method validation by default for all resources, also
>> if the annotation is not given. The annotation could be used to
>> disable method validation for specific resources if needed
>> (@MethodValidated(validationMode=NONE))
>>
>> Would this work for you (I think you expressed doubts about
>> validationMode=NONE at one point, but I'm not sure)? If not, could you
>> describe the scenario you have in mind a bit more?
>>
>> Btw., since last week there is a first alpha release of the BV 1.1 RI,
>> which also implements the current method validation draft. Maybe you
>> could give it a try and see how it works for your use case? This would
>> be a great opportunity to see whether the new APIs fit.
>>
>> --Gunnar
>>
>> [1]
>> http://beanvalidation.org/1.1/spec/#validationapi-triggeringmethodvalidation
>> [2] http://in.relation.to/Bloggers/FirstAlphaReleaseOfHibernateValidator5
>>
>>
>> 2012/6/27 Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>:
>>
>> Hi Gunnar,
>>
>>
>> Thanks for this detailed email, Ron
>>
>>
>>
>> I must admit I cannot convince myself whether asking users to add the
>>
>> annotation to validate or asking users to add the annotation to disable is
>>
>> correct. That's why I had not replied yet. At the moment, I tend to agree
>>
>> with Santiago that this should be on by default.
>>
>>
>>
>> Yes, I think for the JAX-RS case that probably makes sense.
>>
>>
>> The question is whether that's something which must be defined by the
>>
>> spec or whether a user might simply enable validation on a global
>>
>> level using some sort of descriptor file (like validation.xml for BV
>>
>> or beans.xml for CDI). I think personally I'd prefer the latter option
>>
>> as it is a bit less “auto-magic“. A way to globally turn off method
>>
>> validation would probably needed in the case it's implicitly enabled
>>
>> by default.
>>
>>
>>
>> Have you given any more thought to this issue? I.e., the ability to
>>
>> globally turn validation on/off?
>>
>>
>>
>> I don't know though, whether there is such global descriptor for JAX-XS.
>>
>>
>>
>> We don't have one. If BV does not provide a mechanism for this, we'll
>>
>> likely need to define a new annotation in JAX-RS to disable validation.
>>
>>
>> Thanks.
>>
>>
>> -- Santiago
>>
>>
>>