-->
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.  [ 4 posts ] 
Author Message
 Post subject: Deep validation of single attributes?
PostPosted: Mon May 31, 2010 11:40 am 
Newbie

Joined: Wed Nov 18, 2009 5:45 am
Posts: 3
Hi all,

I have some code which looks like this:

Code:
class Person {
  @Valid
  Set<Address> addresses
}

class Address {
    @NotEmpty
    @Length(max = 12)
    String streetName

    @NotEmpty
    String city
}


What I want to accomplish is to validate that all addresses have a valid streetName. Validating the complete object works without any problems by using the following code:

Code:
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
        validator.validate(person);


However, what I want to do is this:

Code:
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
        validator.validate(person, "address.streetName")


"address.streetName", however, doesn't work, because Hibernate thinks my address is a set without a streetName, which is correct. What I also tried was:
"address[].streetName", but then Hibernate complains with an error: "Property path must provide index or map key". If I add such a thing:
"address[0].streetName", it works, but only for the first item in the set, but not when the set is empty or there are two or more items in the set.

So, I was wondering: how do I get this work and how do I validate all the streetNames of all the addresses. Any help would be greatly appreciated!

Kind regards,

Erik Pragt


Top
 Profile  
 
 Post subject: Re: Deep validation of single attributes?
PostPosted: Tue Jun 01, 2010 4:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

there is no direct way to do this in Bean Validation. You could do something like this:
Code:
validator.validate(person, "address")

to validate all addresses, but this would of course also validate the city property.
You could write a loop which iterates over the addresses and call
Code:
validator.validate(person, "address[" + i + "].streetName")

something like this.

--Hardy


Top
 Profile  
 
 Post subject: Re: Deep validation of single attributes?
PostPosted: Tue Jun 01, 2010 7:30 am 
Newbie

Joined: Wed Nov 18, 2009 5:45 am
Posts: 3
Hi Hardy,

A shame that there's no wildcard operator to do this, especially when the ConstraintViolation.getPropertyPath() does return address[].streetName.

Thanks for the reply however, at least now I know I haven't overlooked something!

Erik


Top
 Profile  
 
 Post subject: Re: Deep validation of single attributes?
PostPosted: Tue Jun 01, 2010 7:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
A shame that there's no wildcard operator to do this, especially when the ConstraintViolation.getPropertyPath() does return address[].streetName.

Well, you are using a Set which does not define a order per definition, hence no index. The Bean Validation spec is quite vague on the exact property syntax when it comes to navigating the object graph. Hibernate Validator accepts "address[0].streetName", but it cannot guarantee that each time the same object gets validated.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.