-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Validation of List of primitive types
PostPosted: Wed Oct 28, 2009 11:20 am 
Newbie

Joined: Sat Jan 22, 2005 9:39 am
Posts: 10
Hi,

I have a list of String in my bean :
These strings are email and i want to validate them.

So, i did in my bean :
Code:
@NotEmpty
@Email
List<String> emails ;


At execution, i've got the error :
Code:
Exception in thread "main" javax.validation.UnexpectedTypeException: No validator could be found for type: java.util.List<java.lang.String>
   at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:236)
   at org.hibernate.validator.engine.ConstraintTree.findMatchingValidatorClass(ConstraintTree.java:219)
   at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:167)
   at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:113)
   at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
   at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:334)
   at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:278)
   at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:260)
   at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:213)
   at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119)
   at com.adeuza.movalys.validation.hibernate.TestMain.main(TestMain.java:75)


I don't know if it is a bug of hibernate validator. I have looked at the JSR303 and i havenot seen anything about List of primitives.
You can validate per example List<Person> with @Valid and it works well.
But with list of primitives, i don't understand how to do that with JSR303.

I have used a little the Oval framework and with it, you can tell if the check applies to the container, or the values inside, or the keys(for map).

Thanks for helping.


Top
 Profile  
 
 Post subject: Re: Validation of List of primitive types
PostPosted: Fri Jan 29, 2010 2:02 am 
Newbie

Joined: Fri Jan 29, 2010 2:00 am
Posts: 1
I also believe this to be a limitation of JSR303.

Any chance of a reply from one of the development team??


Top
 Profile  
 
 Post subject: Re: Validation of List of primitive types
PostPosted: Fri Feb 05, 2010 8:10 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

@EMail is only allowed at String, but not at List<String>.

You could solve this by creating a dedicated class representing e-mail addresses (probably a good idea anyways):
Code:
public class EMailAddress {

   @NotNull
   @EMail
   private String address;
   //...
}

Lists of e-mail addresses could then be validated as follows:

Code:
@NotEmpty
@Valid
List<EMailAddress> addresses;

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validation of List of primitive types
PostPosted: Tue Feb 01, 2011 6:14 am 
Newbie

Joined: Tue Feb 01, 2011 6:04 am
Posts: 3
Hello all,

I think this is definitely a limitation of the jsr-303, that there is nothing in the spec for validation of a list of primitive & string. This is a rather simple, and very common scenario (i've come to this topic because like many java developpers out there, i too need to validate a list of string for email format checking)
If anyone of the Validator developpement can give us feedback about this concern, that would be great

Thanks

HR


Top
 Profile  
 
 Post subject: Re: Validation of List of primitive types
PostPosted: Tue Feb 01, 2011 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi,

right, currently there is no simple solution for that requirement. We've discussed this a while ago and didn't come to a good solution as all of the approaches have some disadvantages. If you have a good idea for this, you might want to add a comment at HV's JIRA ticket for this. Hardy also wrote a blog post on this topic which might be of interest for you.

The only really elegant solution to that problem will be JSR 308 which will allow annotations to be given at more locations than today. Using JSR 308 this constraint could be expressed like this:

Code:
List<@Email String> addresses;

Unfortunately this won't be available before Java 8.

With respect to your example of a list of e-mail addresses I still recommend to create a dedicated EMail class as I wrote above. EMail is a value object which deserves is own type and shouldn't be represented by plain strings. To this type you could add the constraints you need (besides @Email maybe @Size etc.), which then would apply whereever you use the EMail type and not only at this single list and you could also add related business logic. Lists could be validated using @Valid then:

Code:
@Valid
List<Email> addresses;

Hth, Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Validation of List of primitive types
PostPosted: Thu Feb 10, 2011 9:48 am 
Newbie

Joined: Thu Feb 10, 2011 8:31 am
Posts: 4
I am trying to implement an Iterable<String> validation using the suggestion suggested here: http://in.relation.to/Bloggers/AValidationStickler?cid=803743. The author mentions

Quote:
One way to solve the problem within the current framework is to implement a ConstraintValidator<Email, Iterable<String>>.

However, being new to JSR303 I am trying to figure out how to make this work:
1) How would one apply a built-in constraint to a list item within isValid?
2) If that is not possible, how would a manual validation be used within the confines of JSR-303?

Please see my comments in the example below

Thanks for any comments

Code:
public class user {
  //....
    @ValidEmailList( regexp="myemailPattern@xyz.com")
     List<String> contacts;
   //getter/setter methods for 'contacts'
  //...
}


Code:
@Target( { FIELD})
@Retention(RUNTIME)
@Constraint(validatedBy = {ValidEmailList.Validator.class})
@Documented
public @interface ValidEmailList {
    String regexp();
    String message() default "Invalid email";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
  //...
       public class Validator implements ConstraintValidator<ValidEmailList, Iterable<String> {

               String regexp;

          @Override
      public void initialize(ValidEmailList c) {
                        this.regexp = c.regexp()
                }

      @Override
      public boolean isValid( Iterable<String> targets, ConstraintValidatorContext ctx) {
                          boolean result = true;
                           int i = 0;
                             for ( String email: targets) {
                                   if ( !email.matches( this.regexp))
                                       //how do I add a constraint violation to this item?
                                       //how do I use ctx and 'index' to mark this item with the constraint
                                       //violation?
                                      //....add a c. violation some how
                                      result = false;
                             }
                             return result;
                   }
          }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.