-->
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: Enhanced method parameter validation: Proposal
PostPosted: Fri Jan 25, 2013 9:17 am 
Newbie

Joined: Fri Mar 12, 2010 9:09 am
Posts: 3
Hi

I'm rewriting our validation framework to Hibernate Validator. In general, I'm very happy with it, but there's one feature which I really miss.
You see, often a method parameter is actually the same as a property within a class, which already contains validation annotations.

Let's say you have a User class which contains an ID:
Code:
@Nonnull
@Range(min = 1, max = 9999)
// And some other validations
private Integer id;


And then you have a method which finds the user:
Code:
public User findUser(Integer id) { ... }


Using the method validation, you can of course specify:
Code:
public User findUser(@Nonnull @Range(min = 1, max = 9999) Integer id) { ... }


But that's a violation of the DRY principle, since the validation already exists in the User class.
That's why in our previous validation framework we had the annotation @ValidProperty. It assumes that the parameter's name is identical to the property of the specified class and then uses that property's validation.

For instance:
Code:
public User findUser(@ValidProperty(User.class) Integer id) { ... }


Of course, it should be possible to use a different parameter name, like:
Code:
public User findUser(@ValidProperty(value=User.class, property="id") Integer userID) { ... }


What do you think?


Top
 Profile  
 
 Post subject: Re: Enhanced method parameter validation: Proposal
PostPosted: Sat Jan 26, 2013 3:02 pm 
Hibernate Team
Hibernate Team

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

I think this is an interesting feature which surely would be useful. Would you mind creating an issue in our JIRA (https://hibernate.onjira.com/browse/HV) for this?

Regarding matching parameter names with property/field names, one would have to define a way for specifying parameter names, though. Currently there is no way to access parameter names at runtime in Java (likely this will be possible in Java 8). How did you solve that problem for your @ValidProperty annotation?

What one surely could do as a first step, is to propagate field/property constraints to the corresponding setter method.

Note that as a workaround you could also create specific types for your attributes (e.g. UserId) which then wraps the actual attribute (e.g. id) and specifies all the required constraints for this attribute. When using this wrapper type, it can be validated using the @Valid annotation, which works on fields/properties but also for method parameters.

--Gunnar

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


Top
 Profile  
 
 Post subject: Re: Enhanced method parameter validation: Proposal
PostPosted: Wed Jan 30, 2013 8:37 am 
Newbie

Joined: Fri Mar 12, 2010 9:09 am
Posts: 3
Gunnar wrote:
Hi,
I think this is an interesting feature which surely would be useful. Would you mind creating an issue in our JIRA (https://hibernate.onjira.com/browse/HV) for this?


Thanks for the reply! I've now created https://hibernate.onjira.com/browse/HV-686 - feel free to edit if something is not clear enough!


Top
 Profile  
 
 Post subject: Re: Enhanced method parameter validation: Proposal
PostPosted: Wed Jan 30, 2013 12:01 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Cool, thanks for reporting the feature request.

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