Digal Digal - 1 year ago 181
Scala Question

Scalaz validation

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]

Answer Source

You can flatten the result with a fold.

result.fold(e => e.fail, x => x.liftFailNel)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download