12

我设法使用自定义约束实现表单验证,但现在我想对 JSON 数据做同样的事情。

如何将自定义验证规则应用于 JSON 解析器?

示例:客户端的 POST 请求中包含一个用户名username

// In the controller...

def postNew = Action { implicit request =>
    request.body.asJson.map { json =>
        json.validate[ExampleCaseClass] match {
            case success: JsSuccess[ExampleCaseClass] =>
                val obj: ExampleCaseClass = success.get
                // ...do something with obj...
                Ok("ok")
            case error: JsError =>
                BadRequest(JsError.toFlatJson(error))
        }
    } getOrElse(BadRequest(Json.obj("msg" -> "JSON request expected")))
}


// In ExampleCaseClass.scala...

case class ExampleCaseClass(username: String, somethingElse: String)

object ExampleCaseClass {
    // That's what I would use for a form:
    val userCheck: Mapping[String] = nonEmptyText.verifying(userExistsConstraint)

    implicit val exampleReads: Reads[ExampleCaseClass] = (
        (JsPath \ "username").read[String] and
        (JsPath \ "somethingElse").read[String]
    )(ExampleCaseClass.apply _)
}

据我所知,但这只能确保它username是一个字符串。如何应用我的附加自定义验证规则,例如检查给定用户是否真的存在?这甚至可能吗?

当然,我可以将我objcase success部分放入操作中并在那里执行额外的检查,但这似乎不是很优雅,因为我必须创建自己的错误消息并且只能JsError.toFlatJson(error)在某些情况下使用。经过几个小时的搜索和尝试,我找不到任何示例。

对于常规形式,我会使用这样的东西:

// In the controller object...

val userValidConstraint: Constraint[String] = Constraint("constraints.uservalid")({ username =>
    if (User.find(username).isDefined) {
        Valid
    } else {
        val errors = Seq(ValidationError("User does not exist"))
        Invalid(errors)
    }
})

val userCheck: Mapping[String] = nonEmptyText.verifying(userValidConstraint)

val exampleForm = Form(
    mapping(
        "username" -> userCheck
        // ...and maybe some more fields...
    )(ExampleCaseClass.apply)(ExampleCaseClass.unapply)
)


// In the controller's action method...

exampleForm.bindFromRequest.fold(
    formWithErrors => {
        BadRequest("Example error message")
    },
    formData => {
        // do something
        Ok("Valid!")
    }
)

但是如果数据以 JSON 格式提交呢?

4

1 回答 1

18

我能想到的最简单的方法是使用filterfrom 的方法Reads

假设我们有一些User对象将确定用户名是否存在:

object User {
    def findByName(name: String): Option[User] = ...
}

然后你可以Reads像这样构建你的:

import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.data.validation._

case class ExampleCaseClass(username: String, somethingElse: String)

object ExampleCaseClass {
    implicit val exampleReads: Reads[ExampleCaseClass] = (
        (JsPath \ "username").read[String].filter(ValidationError("User does not exist."))(findByName(_).isDefined) and
        (JsPath \ "somethingElse").read[String]
    )(ExampleCaseClass.apply _)
}

您的控制器功能可以使用 jsonBodyParser和来简化fold

def postNew = Action(parse.json) { implicit request =>
    request.body.validate[ExampleCaseClass].fold(
        error => BadRequest(JsError.toFlatJson(error)),
        obj => {
            // Do something with the validated object..
        }
    )
}

您还可以创建一个单独Reads[String]的来检查用户是否存在,并Reads[String]在您的Reads[ExampleCaseClass]:

val userValidate = Reads.StringReads.filter(ValidationError("User does not exist."))(findByName(_).isDefined)

implicit val exampleReads: Reads[ExampleCaseClass] = (
    (JsPath \ "username").read[String](userValidate) and
    (JsPath \ "somethingElse").read[String]
)(ExampleCaseClass.apply _)
于 2014-10-11T18:31:13.817 回答