{"id":11922,"date":"2014-08-26T15:06:00","date_gmt":"2014-08-26T14:06:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=9ffb9caa0030a17a5cbd9bd493416443"},"modified":"2015-08-18T13:11:21","modified_gmt":"2015-08-18T12:11:21","slug":"validation-with-warnings-in-scala-with-scalaz","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2014\/08\/26\/validation-with-warnings-in-scala-with-scalaz\/","title":{"rendered":"Validation with warnings in scala with scalaz"},"content":{"rendered":"<p><b>Monad<\/b>&nbsp;are containers with &#8216;special powers&#8217;, when it comes to applying function over its content.<br \/>Validation special power is propagating Failure over validation process.<\/p>\n<p>If you are not familiar with scalaz.Validation I urge you to read this example, which shows how to use Validation: <a href=\"https:\/\/gist.github.com\/oxbowlakes\/970717\">A Tale of 3 Nightclubs<\/a><\/p>\n<p>Basically validation looks like this:<\/p>\n<p>Scalaz.Validation uses idiomatic scala way to compose monads by&nbsp;<b>For Comprehension<\/b>.<\/p>\n<p>Concrete validation method, returning scalaz.Validation instances looks like this:<\/p>\n<p>Scalaz provide helper methods for wrapping values into Failure or Success.<\/p>\n<p>To sum it up. Validation is a an elegant way to handle application validation logic.<\/p>\n<h1 id=\"however-its-not-enough\"> However it&#8217;s not enough. <\/h1>\n<p>Our business rules require application logic&#8217;s to perform validation with warnings, which should not propagate as failures, but rather propagate independently of Success\/Failure types.<\/p>\n<p>We liked monad approach to data validation so we wanted to keep it that way.<\/p>\n<p>Let me introduce <b>Validation with warnings<\/b><\/p>\n<p>What it does is basically wrapping scalaz.Validation into another type&nbsp;responsible for carrying warnings over validation process<\/p>\n<p>Thank to scala type inference our validation code look&#8217;s just the same, but now for expression operates on ValidationWithWarnings type rather than Validation.<\/p>\n<p>OK, but what about validation code? We created similar helper methods for wrapping validation into ValidationWithWarnings and wrapping values directly into warnings.<\/p>\n<p>One could inline warning in for loop:<\/p>\n<p>Or use it in validation method:<\/p>\n<p>And of course chain it in for-loop:<\/p>\n<p><\/p>\n<h3 id=\"applicative\"> Applicative <\/h3>\n<div>We support scalaz.Applicative, so it&#8217;s possible to take few validations and apply them to function if all elements are successes, collecting any errors and warnings if present. <\/p>\n<p><\/p>\n<div>\n<h3 id=\"summing\">Summing <\/h3>\n<\/div>\n<p>Similarly to scalaz.Validation, we also support summing values, if value type has Semigroup typeclass:<\/p>\n<h3 id=\"repository\">Repository <\/h3>\n<\/div>\n<div>Code with examples in test files can be found at <a href=\"https:\/\/github.com\/Ajk4\/ValidationWithWarnings\">https:\/\/github.com\/Ajk4\/ValidationWithWarnings <\/a><\/div>\n<p><\/p>\n<div>\n<h3 id=\"qa\">Q&amp;A<\/h3>\n<\/div>\n<div><b>Why not use Writer Monad?<\/b><br \/>&#8211; Same reason why we prefer Validation over Either with left\/right projection. It&#8217;s more direct and descriptive.<\/p>\n<p><b>Why validation nel underhood?<\/b><br \/>&#8211; It suited our business needs best.<\/p>\n<p><b>Validation is not a Monad!<\/b><br \/>&#8211; True.&nbsp;<\/div>\n","protected":false},"excerpt":{"rendered":"Monad&nbsp;are containers with &#8216;special powers&#8217;, when it comes to applying function over its content.Validation special power is propagating Failure over validation process.If you are not familiar with scalaz.Validation I urge you to read this example,&#8230;\n","protected":false},"author":51,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[113],"class_list":{"0":"post-11922","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-scala"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11922","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\/51"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11922"}],"version-history":[{"count":3,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11922\/revisions"}],"predecessor-version":[{"id":12418,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11922\/revisions\/12418"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}