2

我试图在解组到 Play2.1 中的对象时验证 JSON。我定义的 Format 对象仅在 JSON 中不存在字段时进行验证,但我想验证该字段是否为非空字符串。这可能吗?我尝试在 reads() 调用中指定 minLength() 约束(如此处所示,但我得到一个编译器错误,提示找不到 minLength。这仅适用于元组方法吗?

请参阅以下 Specs2 Junit 测试,该测试现在失败,但在正确定义约束时应该通过:

import org.specs2.mutable._
import play.api.libs.json._

class SimpleValidation extends SpecificationWithJUnit{

  private val badPayload: JsValue = Json.obj(
    "simpleValue1" -> "mySimpleValue", // Comment this line out to pass test
    "simpleValue2" -> ""
  )

  "An IssueFormat" should {
    "validate when unmarshalling" in {

      badPayload.validate[SimpleObj].fold(
        valid = (res => {
          // Fail if valid
          failure("Payload should have been invalid")
        }),
        invalid = (e => {
          // Should be one error
          e.length mustBeEqualTo(1)
        }))

    }
  }
}

import play.api.libs.functional.syntax._

case class SimpleObj(simpleValue1: String, simpleValue2: String)
object SimpleObj {
  val simpleReads = (
    (__ \ "simpleValue1").read[String] and
    (__ \ "simpleValue2").read[String])(SimpleObj.apply _) // read[String](minLength(0)) yields compiler error
  val simpleWrites = (
    (__ \ "simpleValue1").write[String] and
    (__ \ "simpleValue2").write[String])(unlift(SimpleObj.unapply))
  implicit val simpleFormat: Format[SimpleObj] = Format(simpleReads, simpleWrites)
} 
4

2 回答 2

6

您可以在读取中使用 minLength:

import play.api.libs.json.Reads._

然后 minLength 应该可用,但是,请尝试以下格式:

implicit val simpleReads = (
  (__ \ "simpleValue1").read(minLength[String](1)) and
  (__ \ "simpleValue2").read(minLength[String](1))(SimpleObj.apply _)
于 2013-05-13T01:09:01.583 回答
2

在浏览了 Play2.1 文档之后,我能够添加一个客户读取验证器。如果将原始问题中的 SimpleObj 替换为以下内容,则测试用例将通过。不确定是否有更简单的方法可以做到这一点,但这绝对有效:

object SimpleObj {
  // defines a custom reads to be reused
  // a reads that verifies your value is not equal to a give value
  def notEqual[T](v: T)(implicit r: Reads[T]): Reads[T] = Reads.filterNot(ValidationError("validate.error.unexpected.value", v))(_ == v)

  implicit val simpleReads = (
    (__ \ "simpleValue1").read[String](notEqual("")) and
    (__ \ "simpleValue2").read[String](notEqual("")))(SimpleObj.apply _)

  val simpleWrites = (
    (__ \ "simpleValue1").write[String] and
    (__ \ "simpleValue2").write[String])(unlift(SimpleObj.unapply))
  implicit val simpleFormat: Format[SimpleObj] = Format(simpleReads, simpleWrites)
} 
于 2013-03-15T15:46:41.247 回答