{"id":1570,"date":"2011-07-14T10:25:19","date_gmt":"2011-07-14T08:25:19","guid":{"rendered":"http:\/\/mcl.jogger.pl\/2011\/07\/14\/oval-validate-your-models-quickly-and-effortlessly\/"},"modified":"2023-03-23T11:06:49","modified_gmt":"2023-03-23T10:06:49","slug":"oval-validate-your-models-quickly-and-effortlessly","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2011\/07\/14\/oval-validate-your-models-quickly-and-effortlessly\/","title":{"rendered":"OVal &#8211; validate your models quickly and effortlessly!"},"content":{"rendered":"<p>Some time ago one of the projects at work required me to validate some Java POJOs. Theses were my model classes and I\u2019ve been creating them from incoming WebService requests. One would say that XSD would be sufficient for the task, for parts of this validations \u2013 sure, it would. But there were some advanced rules XSD would not handle, or would render the schema document very complicated.<\/p>\n<p>Rules I needed to express were like:<\/p>\n<ul>\n<li>person\u2019s <em>first_name<\/em> and <em>last_name<\/em> should be of appropriate length \u2013 between 2 and 20, and additionally one could pass a zero-length string just to remove the previous value<\/li>\n<li><em>state<\/em> field should consist only defined values \u2013 as in dictionary value \u2013 this one would be completable with XSD\u2019s enumerations, but would require often changing schema files and redistributing them to interested parties :(<\/li>\n<\/ul>\n<p>The library I\u2019ve decided to use for this task is <a href=\"http:\/\/oval.sf.net\">OVal<\/a> and it came out really nice! Read on to find out the details!<\/p>\n<p>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\u2019m not sure about the compatibility.<\/p>\n<p>I\u2019ve tried only a subset of the available checks, among which were:<\/p>\n<ul>\n<li>NotNull<\/li>\n<li>NotEmpty<\/li>\n<li>Length<\/li>\n<\/ul>\n<p>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\u2019s strength. Implementing a check is really easy.<\/p>\n<p>I needed an annotation that would check a field against some values in a dictionary. If field\u2019s 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 \u2013 called by the validation engine on a given field.<\/p>\n<p>Let\u2019s start with our new annotation:<\/p>\n<p>\u00a0<\/p>\n<p>In the above snippet I\u2019ve defined a check-annotation, that would be used like this:<\/p>\n<p>\u00a0<\/p>\n<p>You can pass <em>file<\/em> \u2013 containing dictionary values for this field. There is also <em>message<\/em> field in the annotation which is an error message returned by the validation engine of failed check \u2013 pretty handy. And can be expressed in <em>.properties<\/em> file as:<\/p>\n<p>\u00a0<\/p>\n<p>Placeholder, like <em>context<\/em>, will be replaced with correct values supplied by the validation engine.<\/p>\n<p>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 <em>DictionaryValue<\/em> annotation, it is called <em>DictionaryValueCheck<\/em> and I\u2019ve done this check this way:<\/p>\n<p>\u00a0<\/p>\n<p>What this basically does is:<\/p>\n<ol>\n<li>when <em>file<\/em> is set \u2013 read dictionary content from the file into map<\/li>\n<li>upon check request just lookup value in dictionary parsed from the input file<\/li>\n<\/ol>\n<p>And that\u2019s it!<\/p>\n<p>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.<\/p>\n<p>But perhaps I\u2019m reinventing the wheel and all this can be done easily with some other library? Share Your opinion!<\/p>\n","protected":false},"excerpt":{"rendered":"Some time ago one of the projects at work required me to validate some Java POJOs. Theses were my model classes and I&#8217;ve been creating them from incoming WebService requests. One would say that XSD would be sufficient for the task, for parts of this va&#8230;Some time ago one of the projects at work required me to validate some Java POJOs. Theses were my model classes and I&#8217;ve been creating them from incoming WebService requests. One would say that XSD would be sufficient for the task, for parts of this va&#8230;\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1570","post","type-post","status-publish","format-standard","category-development-design"],"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/1570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=1570"}],"version-history":[{"count":5,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/1570\/revisions"}],"predecessor-version":[{"id":15573,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/1570\/revisions\/15573"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=1570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=1570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=1570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}