11

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.

Olimpiu POP
  • 4,856
  • 4
  • 32
  • 46
Dónal
  • 181,534
  • 169
  • 555
  • 808

2 Answers2

9

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

Riduidel
  • 21,635
  • 13
  • 81
  • 178
7

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
  • 549,808
  • 132
  • 1,049
  • 1,115
  • 1
    @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 Sep 10 '10 at 08:19
  • 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. – Dónal Sep 10 '10 at 08:25
  • +1 Just for the Emmanuel Bernard fandom (listeners of "les castcodeurs" , hands up !) – Riduidel Sep 10 '10 at 09:41
  • @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 Sep 10 '10 at 09:51