如何在 PlayFramework 中的 Json Reads 中添加自定义 ValidationError

swa*_*eed 3 json scala playframework playframework-json playframework-2.5

我正在使用播放读取验证助手我想在 json 异常的情况下显示一些自定义消息,例如:长度是最小值然后指定或给定的电子邮件无效,我知道播放显示这样的错误消息,error.minLength但我想显示一个合理的像请输入大于 1 的字符(或其他字符)这样的消息,这是我的代码

case class DirectUserSignUpValidation(firstName: String,
                                      lastName: String,
                                      email: String,
                                      password: String) extends Serializable

object DirectUserSignUpValidation {
  var validationErrorMsg=""
  implicit val readDirectUser: Reads[DirectUserSignUpValidation] = (
  (JsPath \ "firstName").read(minLength[String](1)) and
    (JsPath \ "lastName").read(minLength[String](1)) and
    (JsPath \ "email").read(email) and
    (JsPath \ "password").read(minLength[String](8).
      filterNot(ValidationError("Password is all numbers"))(_.forall(_.isDigit)).
      filterNot(ValidationError("Password is all letters"))(_.forall(_.isLetter))
    )) (UserSignUpValidation.apply _)
}
Run Code Online (Sandbox Code Playgroud)

我尝试添加ValidationError像这样

 (JsPath \ "email").read(email,Seq(ValidationError("email address not correct")) and
   but its giving me compile time error


  too many arguments for method read: (t: T)play.api.libs.json.Reads[T]
Run Code Online (Sandbox Code Playgroud)

请问您好,我如何在读取 json 数据时添加自定义验证错误消息

sha*_*yan 5

(JsPath \ "firstName").read(minLength[String](1))在播放 json 中没有这样的东西。您可以对自定义错误消息执行以下操作:

(JsPath \ "firstName").read[String].filter(ValidationError("your.error.message"))(_.length > 0)
Run Code Online (Sandbox Code Playgroud)