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" }    
  ]
}

1 Answer 1

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?).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.