简体   繁体   English

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

[英]how to add custom ValidationError in Json Reads in PlayFramework

I am using play Reads validation helpers i want to show some custom message in case of json exception eg:length is minimum then specified or the given email is not valid , i knnow play displays the error message like this error.minLength but i want to display a reasonable message like please enter the character greater then 1 (or something ) here is my code 我正在使用播放读取验证助手我想在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 _)
}

i have tried to add ValidationError like this 我试图像这样添加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]

please helo how can i add custom validationError messages while reading json data 请问我怎么能在读取json数据时添加自定义validationError消息

There is no such thing as (JsPath \\ "firstName").read(minLength[String](1)) in play json. (JsPath \\ "firstName").read(minLength[String](1))没有(JsPath \\ "firstName").read(minLength[String](1)) what you can do with custom error message is this: 您可以使用自定义错误消息执行的操作是:

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

ValidationError messages are supposed to be keys to be used for translation, not human readable messages. ValidationError消息应该是用于翻译的密钥,而不是人类可读的消息。

However, if you still want to change the message for minLength , you'll need to reimplement it, since it is hard-coded. 但是,如果您仍想更改minLength的消息,则需要重新实现它,因为它是硬编码的。

Thankfully, the source code is available, so you can easily change it as you please: 值得庆幸的是,源代码可用,因此您可以随意轻松更改它:

def minLength[M](m: Int)(implicit reads: Reads[M], p: M => scala.collection.TraversableLike[_, M]) =
  filterNot[M](JsonValidationError("error.minLength", m))(_.size < m)

If you want to use a more generic pattern to specify errors, the only access you have is using the result from your validation. 如果要使用更通用的模式来指定错误,则唯一的访问权限是使用验证结果。 For instance, you could do 例如,你可以这样做

val json: JsValue = ???
json.validate[DirectUserSignUpValidation] match {
  case JsSuccess(dusuv, _) => doSomethingWith(dusuv)
  case JsError(errs) => doSomethingWithErrors(errs)
}

Or, with a more compact approach 或者,采用更紧凑的方法

json.validate[DirectUserSignUpValidation].
  fold(doSomethingWithErrors, doSomethingWith)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM