问题
I'm writing a custom validator for json objects.
The default method is to create a case class T
and an implicit reader of type Reads[T]
.
This is what my code looks like (I've created a simple "always fail" validator for the first json attribute just to illustrate my point).
(The second validator looks a bit different because I also need "id"
to validate the "body"
attribute, but I want to have it only run if the first one succeeds).
case class ExecuteQueryModel(datasourceId: Long, body: String)
object ExecuteQueryModel {
def makeModel(id: Long, tpl: (Long, String)): ExecuteQueryModel = new ExecuteQueryModel(id, tpl._2)
implicit val reads: Reads[ExecuteQueryModel] = (
(JsPath \ "id").read[Long](x => false) and
(JsPath). read[(Long, String)](connectionDataIsValid("id", "qbody")))(ExecuteQueryModel.makeModel _)
}
My problem is that I want the whole validation to fail if the first validator fails, so that I can count on the ID being valid when I run the second validator.
Is this possible? Thanks in advance. I'm running play version 2.4.0
来源:https://stackoverflow.com/questions/31077937/force-play-scala-json-composite-validators-to-fail-on-first-failed-validator