-->
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.  [ 8 posts ] 
Author Message
 Post subject: Short-circuit validation
PostPosted: Mon Mar 08, 2010 1:40 am 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Hi

I was wondering if there is a switch or an easy way to tell Hibernate Validator to do short circuit validation? So essentially, when a constraint fails, the subsequence constraints shouldn't get executed.

Example:

Code:

@NotEmpty
@Size.List( { @Size(min = 8), @Size(max = 32) })
@Pattern(regexp="[a-zA-Z ]+")
private String name;



When validation is executed against an object with the above constraints where name = "abc123", I get two error messages for @Size for @Pattern. How do I make it to show one message at a time?

Thanks


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Mon Mar 08, 2010 1:12 pm 
Hibernate Team
Hibernate Team

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

have a look at the group sequences feature.
In your case you could define the following group sequence (First, Second, Third are interfaces):

Code:
@GroupSequence({First.class, Second.class, Third.class, Default.class})
public interface OrderedChecks {
}


Code:
@NotEmpty(groups=First.class
@Size.List( { @Size(min = 8, groups=Second.class), @Size(max = 32, groups=Second.class) })
@Pattern(regexp="[a-zA-Z ]+", groups=Third.class)
private String name;


You then validate by calling:
Code:
validator.validate(myObject, OrderedChecks.class);


--Hardy


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Mon Mar 08, 2010 8:34 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Hi

Thanks for your reply.

I have tried using sequence and it's not the behaviour that I want.

If group sequence is used and there are more than one field in the object, the validator won't validate the subsequence groups unless the first group is successful?

Example:

Code:
@NotEmpty(groups=First.class)
@Size.List( { @Size(min = 8, groups=Second.class), @Size(max = 32, groups=Second.class) })
@Pattern(regexp="[a-zA-Z ]+", groups=Third.class)
private String name;

@NotEmpty(groups=First.class)
@Email(groups=Second.class)
private String email;


If name = "abcdef12345" and email = "xyz", the validator would return just one error message (invalid email), instead of two (invalid name pattern and invalid email) because the second group (of the email property) fails?

Thanks


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Mon Mar 08, 2010 9:21 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Right, there is of course always the possibility of combining your multiple annotations into a single custom constraint. You can either try to work with constraint composition or you just provide your own ConstraintValidator implementation for your custom constraint. In there you can implement the validation as you like.

Although, in your example you could use a single @Pattern and @Email annotation.

--Hardy


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Mon Mar 08, 2010 9:32 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
Thanks Hardy.

Yes, it's possible.

However, ideally, I would like to return a specific error when a condition fails. So if the length is incorrect, I would like to show the incorrect length error message. If there's an invalid character, I would like to return an invalid character message, etc. One message at a time.

I don't want to show a single generic error message when something fails.

Also, using sequence doesn't help me make the constraint validation in ordered, hence the validation error messages. If @NotEmpty, @Size and @Pattern were defined, the order of the validation is random, so I would sometime see the invalid size error first, then invalid pattern and vice versa.


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Tue Mar 09, 2010 9:20 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
newreaders wrote:
However, ideally, I would like to return a specific error when a condition fails. So if the length is incorrect, I would like to show the incorrect length error message. If there's an invalid character, I would like to return an invalid character message, etc. One message at a time.

If you implement your own ConstraintValidator you can either programmatically build your error message or return different message key for the default interpolator.

newreaders wrote:
Also, using sequence doesn't help me make the constraint validation in ordered

I don't understand - this is exactly what it does. A group sequence is an ordered validation.


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Tue Mar 09, 2010 11:06 pm 
Beginner
Beginner

Joined: Wed Sep 15, 2004 3:14 am
Posts: 35
hardy.ferentschik wrote:
I don't understand - this is exactly what it does. A group sequence is an ordered validation.


Yes, a group sequence is an ordered validation, it seems the sequence is relative to the group of other properties in an object. What I mean by this is per the following example:

Code:
@NotEmpty(groups=First.class)
@Size.List( { @Size(min = 8, groups=Second.class), @Size(max = 32, groups=Second.class) })
@Pattern(regexp="[a-zA-Z ]+", groups=Third.class)
private String name;

@NotEmpty(groups=First.class)
@Email(groups=Second.class)
private String email;


If:

Code:
name = "";
email = "xyz";


The validator would return just one error message, but in fact, it should return two error messages. One for name (@NotEmpty of First.class group) and one for email (since @Email in Second.class group). Currently, Hibernate Validator returns just one error for @NotEmpty of the name property.

I thought ordered constraint validation, would mean the validator would execute the constraints of the property in the ordered they were defined by the group(s) of that property only.


Top
 Profile  
 
 Post subject: Re: Short-circuit validation
PostPosted: Fri Mar 12, 2010 6:52 am 
Newbie

Joined: Fri Mar 12, 2010 6:47 am
Posts: 2
thanks for the coding....


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