OVal – validate your models quickly and effortlessly!

Some time ago one of the projects at work required me to validate some Java POJOs. Theses were my model classes and I've been creating them from incoming WebService requests. One would say that XSD would be sufficient for the task, for parts of this validations - sure, it would. But there were some advanced rules XSD would not handle, or would render the schema document very complicated. Rules I needed to express were like:
  • person's first_name and last_name should be of appropriate length - between 2 and 20, and additionally one could pass a zero-length string just to remove the previous value
  • state field should consist only defined values - as in dictionary value - this one would be completable with XSD's enumerations, but would require often changing schema files and redistributing them to interested parties :(
The library I've decided to use for this task is OVal and it came out really nice! Read on to find out the details! Oval is quite mature library that allows POJO validation, but is not JSR303 (bean validation) implementation. It has converters that enable it to understand those annotations, but I'm not sure about the compatibility. I've tried only a subset of the available checks, among which were:
  • NotNull
  • NotEmpty
  • Length
There are many more, and their attributes give interesting ways to configure the validation process. But using them was rather easy and did not require to much brainstorming. What I really needed were custom checks. And in this area OVal shows it's strength. Implementing a check is really easy. I needed an annotation that would check a field against some values in a dictionary. If field's value was in the given set, than the validation would succeed, if not, an exception would be thrown. To accomplish this task it is required to implement two classes: annotation class and check class - called by the validation engine on a given field. Let's start with our new annotation:   In the above snippet I've defined a check-annotation, that would be used like this:   You can pass file - containing dictionary values for this field. There is also message field in the annotation which is an error message returned by the validation engine of failed check - pretty handy. And can be expressed in .properties file as:   Placeholder, like context, will be replaced with correct values supplied by the validation engine. Annotating a field is not enough. It is also needed to create a validator for this kind of check. The name of the class is already defined in DictionaryValue annotation, it is called DictionaryValueCheck and I've done this check this way:   What this basically does is:
  1. when file is set - read dictionary content from the file into map
  2. upon check request just lookup value in dictionary parsed from the input file
And that's it! For me Oval is really great tool. With it at ones disposal it is extremely easy to create any imaginable validation you need. This library is really easy to use and offers lots of handy features. But perhaps I'm reinventing the wheel and all this can be done easily with some other library? Share Your opinion!