【问题标题】:how to add custom ValidationError in Json Reads in PlayFramework如何在 PlayFramework 的 Json 读取中添加自定义 ValidationError
【发布时间】:2017-05-30 07:05:47
【问题描述】:

我正在使用 play Reads 验证助手我想在 json 异常的情况下显示一些自定义消息,例如:长度是最小然后指定或给定的电子邮件无效,我知道 play 会显示这样的错误消息 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 _)
}

我已尝试像这样添加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]

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

【问题讨论】:

  • JsPath.read 不像 html 表单验证参数那样工作。你在link 上问过同样的问题

标签: json scala playframework playframework-2.5 playframework-json


【解决方案1】:

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

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

【讨论】:

  • 其实(JsPath \ "firstName").read(minLength[String](1))是可以的(虽然我也觉得奇怪的是String类型参数没有给read)。它只是不给出自定义错误消息。
  • @CyrilleCorpet (JsPath \ "firstName").read[T](t: T) 只返回给出的任何参数,无需任何验证。它的目的不是验证,而是分配常数值。比如(JsPath \ "firstName").read("John")
  • 这不是我说的方法。而是(JsPath \ "firstName").read[T](t: Reads[T]): Reads[T]minLength[String](1)(我在回答中给出了签名和实现)是最小尺寸的Reads[String]
  • @CyrilleCorpet 是的,我明白了。我的意思是,如果他写(JsPath \ "firstName").read(minLength[String](1)),playjson 会将 firstName 字段解析为Constraint[String],这是错误的。因此我的回复是“在play json中没有(JsPath \ "firstName").read(minLength[String](1))这样的东西”
  • 哇,我真的很慢,但我终于明白你的意思了!但是,我尝试了这个,IntelliJ 将类型推断为Reads[String],而不是Reads[Reads[String]],所以在模棱两可的方法调用解析中肯定有一些东西使它在这种情况下更喜欢第二个。
【解决方案2】:

ValidationError 消息应该是用于翻译的键,而不是人类可读的消息。

但是,如果您仍想更改 minLength 的消息,则需要重新实现它,因为它是硬编码的。

谢天谢地,源代码是可用的,所以您可以随意更改它:

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

如果您想使用更通用的模式来指定错误,您唯一的访问权限是使用验证结果。例如,你可以这样做

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

或者,使用更紧凑的方法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多