Note that there are some explanatory texts on larger screens.

plurals
  1. POScalaz Validation, validate inner value
    text
    copied!<p>I have a Validation object</p> <pre><code>val v = Validation[String, Option[Int]] </code></pre> <p>I need to make a second validation, to check if actual Integer value is equals to 100 for example. If I do </p> <pre><code>val vv = v.map(_.map(intValue =&gt; if (intValue == 100) intValue.success[String] else "Bad value found".fail[Integer])) </code></pre> <p>I get:</p> <pre><code>Validation[String, Option[Validation[String, Int]]] </code></pre> <p>How is it possible to get vv also as Validation[String, Option[Int]] in most concise way</p> <p>=========</p> <p>Found possible solution from my own:</p> <pre><code>val validation: Validation[String, Option[Int]] = Some(100).success[String] val validatedTwice: Validation[String, Option[Int]] = validation.fold( _ =&gt; validation, // if Failure then return it _.map(validateValue _) getOrElse validation // validate Successful result ) def validateValue(value: Int): Validation[String, Option[Int]] = { if (value == 100) Some(value).success[String] else "Bad value".fail[Option[Int]] } </code></pre> <p>Looks not concise and elegant although it works</p> <p>==============</p> <p>Second solution from my own, but also looks over-compicated:</p> <pre><code>val validatedTwice2: Validation[String, Option[Int]] = validation.flatMap( _.map(validateValue _).map(_.map(Some(_))) getOrElse validation) def validateValue(value: Int): Validation[String, Int] = { if (value == 100) value.success[String] else "Bad value".fail[Int] } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload