简体   繁体   中英

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] ?

你可以用折叠来平整结果。

result.fold(e => e.fail, x => x.liftFailNel)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM