简体   繁体   English

Scalaz验证

[英]Scalaz validation

I'm trying to use scalaz validation in our project and have ran into a following situation: 我正在尝试在我们的项目中使用scalaz验证并遇到以下情况:

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: 现在我必须返回包含可能的参数错误的ValidationNEL,如果有的话,或者使用经验证的参数来调用该方法:

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]] . 将返回ValidationNEL[String, Validation[String, Int]] Is there a way to "flatten" this result, to get the ValidationNEL[String, Int] ? 有没有办法“扁平化”这个结果,以获得ValidationNEL[String, Int]

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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