views:

51

answers:

2

Hi,

I'm looking for an annotation-based validation framework, that would allow me to validate parameter values. Something like the following:

void someMethod(@NotBlank String foo, @NotEmpty Collection bar, @Positive Integer baz, @NotNull Object obj) {

}

Validation of the parameters should happen automatically whenever an annotated method/constructor is called, so that there no way to bypass the validation.

If I could customise the error messages produced that would be nice. I'm using Java 1.5.

Thanks, Don

+5  A: 

That's a job for a JSR 303 - Bean Validation implementation, like Hibernate Validator (the RI). Check the various Bean Validation blog posts from Emmanuel Bernard (the spec lead).

Pascal Thivent
@Don AFAIK, BV is for Java 1.5+, it doesn't require Java 1.6 (with Hibernate Validator, I know that you'll have to provide the JAXB API and a JAXB implementation if you're using Java 1.5 since it's not part of the JDK).
Pascal Thivent
Thanks Pascal. I dislike the fact that you need to manually invoke the validation. I would like the params to be automatically validated whenever an annotated method is called.
Don
+1 Just for the Emmanuel Bernard fandom (listeners of "les castcodeurs" , hands up !)
Riduidel
@Don I used it hooked in other frameworks (JPA, JSF 2.0, Wicket, etc) so it does the job for me. But I get what you mean. @Riduidel Thanks, and you have my +1 too (didn't try OVal so far, only Hibernate Validator that covers my needs). Emmanuel did really a great job with the BV spec. And yes, *les castcodeurs* is a nice podcast, I like it too.
Pascal Thivent
+4  A: 

Try taking a look at OVal and others implementors of the JSR-303 (aka Bean validation)

Riduidel
Yes, OVal seems like a winner to me: http://oval.sourceforge.net/userguide.html#d4e233
seanizer