-->
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: Hibernate Validator Annotation Processor not checking @Min
PostPosted: Thu Feb 24, 2011 11:22 am 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
The Hibernate Validator site says the annotation processor will catch "Annotated strings with @Min to specify a minimum length even though you should have used @Size". This isn't the case, because in this code:
Code:
public class SomeObject {

    @Size(min = 10, max = 20)
    String foo;

    @Min(20)             // not marked as an error
    String foo3;

    @Valid                 // correctly marked as an error
    boolean foo2;
}

Is this an oversight, or is there a reason the check isn't being performed?

Thanks,
Emerson


Top
 Profile  
 
 Post subject: Re: Hibernate Validator Annotation Processor not checking @Min
PostPosted: Thu Feb 24, 2011 2:59 pm 
Hibernate Team
Hibernate Team

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

thanks for your report.

I checked with the implemention (annotation processor and Hibernate Validator runtime) and was a bit surprised myself :-) Hibernate Validator actually supports @Min (and also @Max) for Strings (in addition to what is demanded by the Bean Validation specification), though not for checking the length of Strings, but for checking that the number represented by a given String is larger/smaller than the value given in the annotation. Therefore the annotation processor does not mark Strings annotated with @Min as an error.

The misunderstanding that @Min can be used for restricting the length of a String still could happen of course. I created HV-442 for improving the documentation on that matter.

Gunnar

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


Top
 Profile  
 
 Post subject: Re: Hibernate Validator Annotation Processor not checking @Min
PostPosted: Thu Feb 24, 2011 8:50 pm 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
Thanks. That makes sense. It's misleading because of the added type support, but it makes sense and as you suggested, is probably best clarified using the documentation. Maybe a "strict" option on the annotation processor can be added that warns the user when HV imports are being used instead of BV imports of the same name, to highlight these differences?

On a related note, would @Min return a ConstraintViolation if the String isn't numeric? And would the string be treated as an int, a double, a long, ...? I can't dig around the source right now, but I'm curious. :)


Top
 Profile  
 
 Post subject: Re: Hibernate Validator Annotation Processor not checking @Min
PostPosted: Fri Feb 25, 2011 8:45 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Quote:
... that warns the user when HV imports are being used instead of BV imports of the same name

Not sure, I'm following. To which import do you refer here? The @Min constraint is the one from javax.validation.constraints, it's just that HV supports this constraint for more data types than required by the specification.

The @Min validator for Strings uses new BigDecimal(String) for parsing, so any number format understood by BigDecimal is supported. If a NumberFormatException occurs during parsing due to an invalid number format a ConstraintViolation would be the result.

In case you want to have a look at the sources, you can find them at GitHub.

_________________
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.  [ 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.