I'm trying to use scalaz validation in our project and have ran into a following situation:
def rate(username: String, params: Map[String, String]): ValidationNEL[String, Int] = {
val voteV:Validation[String, RateVote] = validate(params, "vote") flatMap {v: String => RateVote(v)}
val voterV:Validation[String, Sring] = validate(params, "voter")
...
}
Now I have to return ValidationNEL containing possible parameter errors, if there were any, or use validated parameters to call the method:
storage.rate(username, voter, vote): Validation[String, Int]
I know, I could use |@| for the first part, but this code
(voterV.liftFailNel |@| voteV.liftFailNel) { (voter, rv) =>
storage.rate(username, voter, rv)
}
will return ValidationNEL[String, Validation[String, Int]]
. Is there a way to "flatten" this result, to get the ValidationNEL[String, Int]
?
You can flatten the result with a fold.
result.fold(e => e.fail, x => x.liftFailNel)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With