2

I am using PlayFramework 2.6. I have the following classes

case class Account(homePage: URL, name: String)
object Account {
    import implicits.ReadsWrites.urlFormat
    implicit val format: Format[Account] = Json.format[Account]
}

And account is an optional field of type Account

case class IdentifiedGroup(objectType: String,
                       name: Option[String],
                       member: Option[Seq[Agent]],
                       mbox: Option[URI],
                       mboxSHA1SUM: Option[String],
                       openID: Option[URI],
                       account: Option[Account]) extends Group

When name field in Account is not a string but an object such as "name": { "key": "this should fail" } Then when I apply reads on IdentifiedGroup, the account will be None.

But the expected behaviour is that if account is present and invalid then I want JsError as a result instead.

I've been googling for more than a day and still can't seem to find the right answer :(

Thanks

dan1st
  • 12,568
  • 8
  • 34
  • 67
Alex To
  • 21
  • 2
  • This feels a bit surprising... but I guess it makes sense. I think you're going to have to write the explicit Format. https://www.playframework.com/documentation/2.6.x/ScalaJsonCombinators#Validation-with-Reads – Andy Hayden Aug 15 '18 at 05:10
  • This really should work out of the box. How did you implement `Reads[IdentifiedGroup]`? – cbley Aug 15 '18 at 07:07
  • `object test { case class Account(name: String) case class IdentifiedGroup(objectType: String, name: Option[String], acc: Option[Account]) implicit val formatAcc: Format[Account] = Json.format[Account] implicit val format: Format[IdentifiedGroup] = Json.format[IdentifiedGroup] def main( args:Array[String] ):Unit = { println(format.reads(Json.parse("""{"objectType": "type1", "name": "name1", "acc": { "name": {"type": "bla"}}}"""))) }` For me this code fails, Maybe you can provide more information? – Vladislav Kievski Aug 15 '18 at 07:49

0 Answers0