-->
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.  [ 2 posts ] 
Author Message
 Post subject: Failing fast with composite constraints
PostPosted: Thu May 23, 2013 7:14 am 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
Hi,

I'm wondering if there's a way to make the validation of a composite constraint fail fast (or short circuit) if the validation of a meta constraint fails. In my case, I have two constraints @NullHostileCollection for Collection<?> and composite @NumberList for List<? extends Number>, which look like

Code:
@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = NullHostileCollectionValidator.class)
@Documented
public @interface NullHostileCollection {


Code:
@Target({METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = NumberListValidator.class)
@Documented
@NullHostileCollection
@ReportAsSingleViolation
public @interface NumberList {


When I validate a number list which contains null values, the NullHostileCollectionValidator correctly considers the list invalid. However, validation then continues to the NumberListValidator instad of short circuiting. As a result, the NumberListValidator can't assume anything about the list and has to have extra checks to avoid NPEs, etc. Is it possible to make the validation short circuit? This behaviour makes even more sense when reporting as a single violation, IMO.

Thanks,
Emerson


Top
 Profile  
 
 Post subject: Re: Failing fast with composite constraints
PostPosted: Thu May 23, 2013 1:47 pm 
Hibernate Team
Hibernate Team

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

The Bean Validation spec says

Quote:
[If] a composed constraint is marked as @ReportAsSingleViolation, the evaluation of the composing constraints stops at the first failing constraint and the error report corresponding to the composed constraint is generated and returned.


Hibernate Validator validates the composing constraints before the composed constraint. Based on that I'd expect that the NumberListValidator is not invoked when @NullHostileCollection is violated (since you have annotated @NumberList with @ReportAsSingleViolation). Is this not the case, i.e. is the number list validator invoked in this case?

--Gunnar

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


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