Scalaz 验证
我正在尝试在我们的项目中使用 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")
...
}
现在我必须返回包含可能的参数错误(如果有)的 ValidationNEL,或者使用经过验证的参数来调用该方法:
storage.rate(username, voter, vote): Validation[String, Int]
我知道,我可以使用|@|对于第一部分,但此代码
(voterV.liftFailNel |@| voteV.liftFailNel) { (voter, rv) =>
storage.rate(username, voter, rv)
}
将返回 ValidationNEL[String, Validation[String, Int]]。有没有办法“展平”这个结果,以获得 ValidationNEL[String, Int] ?
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]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过折叠将结果展平。
You can flatten the result with a fold.