You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@bval.apache.org by Rudy De Busscher <rd...@gmail.com> on 2011/07/18 21:17:02 UTC

Question on non default groups value

hi  All,

The situation is a bit difficult to describe both I have a use case where I
would like to create a custom validation annotation where I need to define a
non empty groups attribute

something like this
*@Target(value = { ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { EqualsValidator.class })
public @interface Equals
{
    String message() default "{duplicated_content_required}";

    Class<?>[] groups() default {CrossValidationGroup.class};

    Class<? extends Payload>[] payload() default {};

    String[] value();
}
*

But as expected, I get a error when I try to use the annotation:
Default value for groups() must be an empty array

But I want to avoid that the user needs to define it always when he uses the
annotation.
  *  @Equals(value = "caseSensitiveTarget", groups =
CrossValidationGroup.class)
    private String caseSensitiveSource;*


Any idea or direction I need to look into are apreciated.

Regards

-- 
Rudy De Busscher
http://www.c4j.be

Re: Question on non default groups value

Posted by Rudy De Busscher <rd...@gmail.com>.
@All

I tried to make a kind of transformer.  And although it works, I'm not
completely satisfied.
This is was I did.

In the buildForClass method of the MetaBeanBuilder, I call all the
registered transformer and let them do their thing. Something like this:

*org.apache.bval.MetaBeanBuilder#buildForClass

        for (MetaBeanTransformer transformer :
TransformerFactory.getTransformers()) {
            transformer.transform(meta);
        }
        return meta;
*

A possible transform could add a group when the annotation has a kind of
marker:
*
    public void transform(MetaBean metaBean)
    {
        for (MetaProperty metaProperty : metaBean.getProperties())
        {
            for (Validation validation : metaProperty.getValidations())
            {
                ConstraintValidation constraint = (ConstraintValidation)
validation;
                if
(checkHasCrossValidationMarker(constraint.getAnnotation()))
                {
                    constraint.getGroups().remove(Default.class);
                    constraint.getGroups().add(CrossValidationGroup.class);
                }
            }
        }
    }
*
The reason why I'm not satisfied?
- Validation is an interface that I need to cast to ConstraintValidation
- ConstraintValidation is immutable but adding to the groups is possible
through getGroups().add()
- Adding a group like this, doesn't set the attribute (see setGroups;
this.attributes.put(Jsr303MetaBeanFactory.ANNOTATION_GROUPS, ....)

Probably there is a solution with a more complex setup, but I searched and
found a solution that removes the requirement of adding the group which
works in Apache BVal and Hibernate validator with a small performance
penalty.

regards
Rudy

On 22 July 2011 10:51, Gerhard Petracek <ge...@gmail.com> wrote:

> hi rudy,
>
> maybe we should think about both. providing a (different) default group for
> a constraint sounds useful (maybe even for bv 1.1) if you have a constraint
> which is (by definition) only useful for a specific group (in most or all
> cases).
>
> regards,
> gerhard
>
> http://www.irian.at
>
> Your JSF powerhouse -
> JSF Consulting, Development and
> Courses in English and German
>
> Professional Support for Apache MyFaces
>
>
>
> 2011/7/22 Rudy De Busscher <rd...@gmail.com>
>
>> Hi Gerhard,
>>
>> Disabling the standard requirements for just an add on of ExtVal seems a
>> bit too drastic, but a transformer seems a good idea.  I'll try to make
>> something and post it.
>>
>> Regards
>> Rudy.
>>
>>
>> On 18 July 2011 21:58, Gerhard Petracek <ge...@gmail.com>wrote:
>>
>>> hi rudy,
>>>
>>> we could discuss a bval feature which relaxes such rules (it would be
>>> deactivated by default).
>>> or something like transformer for constraints.
>>>
>>> regards,
>>> gerhard
>>>
>>> http://www.irian.at
>>>
>>> Your JSF powerhouse -
>>> JSF Consulting, Development and
>>> Courses in English and German
>>>
>>> Professional Support for Apache MyFaces
>>>
>>>
>>>
>>> 2011/7/18 Rudy De Busscher <rd...@gmail.com>
>>>
>>>> hi  All,
>>>>
>>>> The situation is a bit difficult to describe both I have a use case
>>>> where I would like to create a custom validation annotation where I need to
>>>> define a non empty groups attribute
>>>>
>>>> something like this
>>>> *@Target(value = { ElementType.FIELD})
>>>> @Retention(value = RetentionPolicy.RUNTIME)
>>>> @Constraint(validatedBy = { EqualsValidator.class })
>>>> public @interface Equals
>>>> {
>>>>     String message() default "{duplicated_content_required}";
>>>>
>>>>     Class<?>[] groups() default {CrossValidationGroup.class};
>>>>
>>>>     Class<? extends Payload>[] payload() default {};
>>>>
>>>>     String[] value();
>>>> }
>>>> *
>>>>
>>>> But as expected, I get a error when I try to use the annotation:
>>>> Default value for groups() must be an empty array
>>>>
>>>> But I want to avoid that the user needs to define it always when he uses
>>>> the annotation.
>>>>   *  @Equals(value = "caseSensitiveTarget", groups =
>>>> CrossValidationGroup.class)
>>>>     private String caseSensitiveSource;*
>>>>
>>>>
>>>> Any idea or direction I need to look into are apreciated.
>>>>
>>>> Regards
>>>>
>>>> --
>>>> Rudy De Busscher
>>>> http://www.c4j.be
>>>>
>>>>
>>>
>>
>>
>> --
>> Rudy De Busscher
>> http://www.c4j.be
>>
>>
>


-- 
Rudy De Busscher
http://www.c4j.be

Re: Question on non default groups value

Posted by Gerhard Petracek <ge...@gmail.com>.
hi rudy,

maybe we should think about both. providing a (different) default group for
a constraint sounds useful (maybe even for bv 1.1) if you have a constraint
which is (by definition) only useful for a specific group (in most or all
cases).

regards,
gerhard

http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces



2011/7/22 Rudy De Busscher <rd...@gmail.com>

> Hi Gerhard,
>
> Disabling the standard requirements for just an add on of ExtVal seems a
> bit too drastic, but a transformer seems a good idea.  I'll try to make
> something and post it.
>
> Regards
> Rudy.
>
>
> On 18 July 2011 21:58, Gerhard Petracek <ge...@gmail.com>wrote:
>
>> hi rudy,
>>
>> we could discuss a bval feature which relaxes such rules (it would be
>> deactivated by default).
>> or something like transformer for constraints.
>>
>> regards,
>> gerhard
>>
>> http://www.irian.at
>>
>> Your JSF powerhouse -
>> JSF Consulting, Development and
>> Courses in English and German
>>
>> Professional Support for Apache MyFaces
>>
>>
>>
>> 2011/7/18 Rudy De Busscher <rd...@gmail.com>
>>
>>> hi  All,
>>>
>>> The situation is a bit difficult to describe both I have a use case where
>>> I would like to create a custom validation annotation where I need to define
>>> a non empty groups attribute
>>>
>>> something like this
>>> *@Target(value = { ElementType.FIELD})
>>> @Retention(value = RetentionPolicy.RUNTIME)
>>> @Constraint(validatedBy = { EqualsValidator.class })
>>> public @interface Equals
>>> {
>>>     String message() default "{duplicated_content_required}";
>>>
>>>     Class<?>[] groups() default {CrossValidationGroup.class};
>>>
>>>     Class<? extends Payload>[] payload() default {};
>>>
>>>     String[] value();
>>> }
>>> *
>>>
>>> But as expected, I get a error when I try to use the annotation:
>>> Default value for groups() must be an empty array
>>>
>>> But I want to avoid that the user needs to define it always when he uses
>>> the annotation.
>>>   *  @Equals(value = "caseSensitiveTarget", groups =
>>> CrossValidationGroup.class)
>>>     private String caseSensitiveSource;*
>>>
>>>
>>> Any idea or direction I need to look into are apreciated.
>>>
>>> Regards
>>>
>>> --
>>> Rudy De Busscher
>>> http://www.c4j.be
>>>
>>>
>>
>
>
> --
> Rudy De Busscher
> http://www.c4j.be
>
>

Re: Question on non default groups value

Posted by Rudy De Busscher <rd...@gmail.com>.
Hi Gerhard,

Disabling the standard requirements for just an add on of ExtVal seems a bit
too drastic, but a transformer seems a good idea.  I'll try to make
something and post it.

Regards
Rudy.

On 18 July 2011 21:58, Gerhard Petracek <ge...@gmail.com> wrote:

> hi rudy,
>
> we could discuss a bval feature which relaxes such rules (it would be
> deactivated by default).
> or something like transformer for constraints.
>
> regards,
> gerhard
>
> http://www.irian.at
>
> Your JSF powerhouse -
> JSF Consulting, Development and
> Courses in English and German
>
> Professional Support for Apache MyFaces
>
>
>
> 2011/7/18 Rudy De Busscher <rd...@gmail.com>
>
>> hi  All,
>>
>> The situation is a bit difficult to describe both I have a use case where
>> I would like to create a custom validation annotation where I need to define
>> a non empty groups attribute
>>
>> something like this
>> *@Target(value = { ElementType.FIELD})
>> @Retention(value = RetentionPolicy.RUNTIME)
>> @Constraint(validatedBy = { EqualsValidator.class })
>> public @interface Equals
>> {
>>     String message() default "{duplicated_content_required}";
>>
>>     Class<?>[] groups() default {CrossValidationGroup.class};
>>
>>     Class<? extends Payload>[] payload() default {};
>>
>>     String[] value();
>> }
>> *
>>
>> But as expected, I get a error when I try to use the annotation:
>> Default value for groups() must be an empty array
>>
>> But I want to avoid that the user needs to define it always when he uses
>> the annotation.
>>   *  @Equals(value = "caseSensitiveTarget", groups =
>> CrossValidationGroup.class)
>>     private String caseSensitiveSource;*
>>
>>
>> Any idea or direction I need to look into are apreciated.
>>
>> Regards
>>
>> --
>> Rudy De Busscher
>> http://www.c4j.be
>>
>>
>


-- 
Rudy De Busscher
http://www.c4j.be

Re: Question on non default groups value

Posted by Gerhard Petracek <ge...@gmail.com>.
hi rudy,

we could discuss a bval feature which relaxes such rules (it would be
deactivated by default).
or something like transformer for constraints.

regards,
gerhard

http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces



2011/7/18 Rudy De Busscher <rd...@gmail.com>

> hi  All,
>
> The situation is a bit difficult to describe both I have a use case where I
> would like to create a custom validation annotation where I need to define a
> non empty groups attribute
>
> something like this
> *@Target(value = { ElementType.FIELD})
> @Retention(value = RetentionPolicy.RUNTIME)
> @Constraint(validatedBy = { EqualsValidator.class })
> public @interface Equals
> {
>     String message() default "{duplicated_content_required}";
>
>     Class<?>[] groups() default {CrossValidationGroup.class};
>
>     Class<? extends Payload>[] payload() default {};
>
>     String[] value();
> }
> *
>
> But as expected, I get a error when I try to use the annotation:
> Default value for groups() must be an empty array
>
> But I want to avoid that the user needs to define it always when he uses
> the annotation.
>   *  @Equals(value = "caseSensitiveTarget", groups =
> CrossValidationGroup.class)
>     private String caseSensitiveSource;*
>
>
> Any idea or direction I need to look into are apreciated.
>
> Regards
>
> --
> Rudy De Busscher
> http://www.c4j.be
>
>