【问题标题】:Json validation custom errorsJson 验证自定义错误
【发布时间】:2018-09-19 14:40:48
【问题描述】:

以下代码根据案例类验证 json 字段。如果名称、电子邮件、地址为空或提供整数而不是字符串,如何添加自定义错误消息?

implicit val reads: Reads[ValidateDetails] = (
  (JsPath \ "name").read[String] and
    (JsPath \ "email").read[String] and
    (JsPath \ "address").read[String])(ValidateDBConfigJson.apply _)
 }

我还想要以下 json 格式的自定义错误消息。

 {
 "ErrorMessages" : 
    [
    "Error 1", 
    "Error 2"
    ]
 }

【问题讨论】:

    标签: json scala playframework


    【解决方案1】:

    阅读后,对于每个字段,您可以filter 验证错误的结果。例如name字段:

    (JsPath \ "name").read[String](JsPath \"name").read[String].filter(ValidationError("The length for name should be more than 5 characters"))(_.length > 5)
    

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 2013-08-04
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多