I'm using Playframework 2.2.1 with Scala, and I'm writing a REST API where users can set partially unstructured data. Basically, users will POST JSON that looks something like this:
{
"id": 1,
"name": "MyObject",
"properties": [
"myFirstProperty": "Value 1",
"mySecondProperty": "Value 2"
]
}
and I want it to bind to a case class that looks like:
case class Preference(id: Long, name: String, properties: Map[String, String])
I am hoping to take advantage of Play's forms API for that so I can have a bunch of nice validation options for free, but I haven't been able to figure out a straightforward way to do that. Ideally (at least in my mind), I would be able to define a Form object along the lines of:
Form(
mapping(
"id" -> longNumber,
"name" -> nonEmptyText(min = 5),
"properties" -> map(nonEmptyText, nonEmptyText)
)(Preference.apply)(Preference.unapply)
)
Unfortunately, the "map(text, text)" is fictional. Has anyone done a similar binding to this using the forms API? If so, how? If not, I'm curious what you used instead for validation. Was it all done by hand?
Thanks in advance for the help!