我设法使用自定义约束实现表单验证,但现在我想对 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
是一个字符串。如何应用我的附加自定义验证规则,例如检查给定用户是否真的存在?这甚至可能吗?
当然,我可以将我obj
的case 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 格式提交呢?