【发布时间】:2014-12-06 16:07:36
【问题描述】:
我设法使用自定义约束实现表单验证,但现在我想对 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 是一个字符串。 如何应用我的附加自定义验证规则,例如检查给定的用户是否真的存在?这甚至可能吗?
当然,我可以在操作中的case success 部分中使用我的obj 并在那里执行额外的检查,但这似乎不太优雅,因为那样我就必须创建自己的错误消息并且可以在某些情况下只有用户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 格式提交呢?
【问题讨论】:
-
我建议在 JSON 验证后验证您的用户。我认为最好使用 JSON 验证来确保您可以进入 Scala 对象领域,然后您需要做的所有其他事情都会变得更加舒适。
标签: json scala validation playframework-2.3 playframework-json