-->
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.  [ 7 posts ] 
Author Message
 Post subject: Validators return true on isValid if value is null
PostPosted: Sat Jan 28, 2006 10:41 am 
Newbie

Joined: Mon Jan 23, 2006 1:11 pm
Posts: 15
Location: Leuven, Belgium
When i apply an annotation validator on a property, the validator will return true if the property is null.

Eg:
Code:
@ManyToMany
@Size(min=1) public List getUsers() { return users; }


When users = null, the validator will return true. I would expect it returns false because we don't know for sure that the size is at least 1.

I know that the documentation says that the @Size annotation should only be used on type array, list or map. One could argue that null is not an applicable type. But even then i would expect isValidate to return false because the value isn't applicable.

It appears this was already the intention of the author, because when i look at PatternValidator i see the following: (The value isn't applicable, thus isValid return false)

Code:
if ( !( value instanceof String ) ) return false;


Thus i wonder: Why do all the Validators return true when the value is null?

_________________
Don't applaud, throw money (paypal to timvw@users.sourceforge.net) - http://www.timvw.be


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 10:06 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
negative, add @NotNull for this behavior

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 4:48 pm 
Newbie

Joined: Mon Jan 23, 2006 1:11 pm
Posts: 15
Location: Leuven, Belgium
Thanks, i had already done that :) It just didn't feel very right the way the other validators behave on null.. But i'll get used to it.

_________________
Don't applaud, throw money (paypal to timvw@users.sourceforge.net) - http://www.timvw.be


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 30, 2006 6:15 pm 
Newbie

Joined: Mon Jan 23, 2006 1:11 pm
Posts: 15
Location: Leuven, Belgium
What i still ask myself: is there a particular reason to give null a special treatment than other values that are not applicable?

Eg: LengthValidator will return true for null and false for other values that aren't instanceof String either...

Code:
public boolean isValid(Object value) {
  if ( value == null ) return true;
  if ( !( value instanceof String ) ) return false;
  String string = (String) value;
  int length = string.length();
  return length >= min && length <= max;
}

_________________
Don't applaud, throw money (paypal to timvw@users.sourceforge.net) - http://www.timvw.be


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 6:08 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes. Null semantically means I don't know the information.
If you don't know the value why raising an invalid exception.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 31, 2006 7:27 am 
Newbie

Joined: Mon Jan 23, 2006 1:11 pm
Posts: 15
Location: Leuven, Belgium
emmanuel wrote:
Yes. Null semantically means I don't know the information.
If you don't know the value why raising an invalid exception.


For me it would be the ultimate reason to throw that exception. If i don't know the value, how can i be sure it isn't violating the constraint?

I can understand your POV: because you're not sure that it is valid or not, you decide to let it in anyway (blacklist). I prefer the approach to keep it out untill i'm absolutely sure that it's valid (whitelist).

_________________
Don't applaud, throw money (paypal to timvw@users.sourceforge.net) - http://www.timvw.be


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 01, 2006 4:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is not how ternary logic works

_________________
Emmanuel


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