Force Play (Scala) Json Composite Validators to Fail on First Failed Validator

半腔热情 提交于 2019-12-11 03:45:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!