1

I'm new to both Scala and Scalatra. I'm just wondering, but how do you use Scalatra commands to validate a nested object, such as images below as part of the validation of the overall object?

{ "id" : 1,
  "name" : "First item",
  "images": [
    { "src" : "first_image.jpg" },
    { "src" : "second_image.jpg" },
    { "src" : "third_image.jpg" }    
  ]
}
John Polling
  • 2,272
  • 2
  • 21
  • 21

1 Answers1

0

From the source code, it looks like only nested extraction of simple values is supported. Details below.

You can extract values from Json using nested paths:

  val name: Field[String] = asType[String]("keyword.name")

This will extract 'test' from this json:

{keyword:{name:'test'}}

You can look for the code of this extraction in the class org.scalatra.json.JsonValueReader. This reader is used in the method org.scalatra.commands.Command.bindTo.

The critical source is:

...
      val result = b.field.valueSource match {
        case ValueSource.Body => fieldBinding(data.read(name).right.map(_ map (_.asInstanceOf[fieldBinding.S])))
...

The call "_.asInstanceOf[fieldBinding.S]" is realized on a JValue object. This extraction only work for simple types (check this question: How to parse JSON in Scala using standard Scala classes?).

Community
  • 1
  • 1
EIIPII
  • 1,791
  • 1
  • 17
  • 10