【发布时间】:2015-05-18 11:01:24
【问题描述】:
我有这个代码来检查列表是否为空,如果它为空,它将有 ValidationError("should not empty")
case class Foo(t: List[String], r: String)
object Foo {
implicit val fooReads: Reads[Foo] = (
(JsPath \ "t").read[List[String]]
.filter(ValidationError("should not empty"))(_.nonEmpty) and
(JsPath \ "r").read[String]
)
}
但是,我不知道如何在测试中获取验证错误。
val json = """
|{
| "t": [],
| "r": "123"
|}
""".stripMargin
val result: JsResult[Foo] = json.validate[Foo]
result.isError shouldBe true
result.asEither.left.get.head._2.head.message shouldBe "should not empty"
代码有点丑,有没有更好的方法从 JsResult 测试 ValidationError ?
在此先感谢
【问题讨论】:
-
您是否只需要在测试中出现空列表的错误?或者您是否想要在您的读取中实施某种验证规则?这意味着该规则将在列表为空的任何时候抛出,不仅在测试中。
-
我只是想从中得到错误信息,但是语法很冗长
标签: scala playframework play-json