scala - validate multiple json values in play -


i have following json fields coming request

action {   request =>     val jsvalueoption = request.body.asjson     jsvalueoption.map {       json =>           val f1 = (json \ "f1").validate[string]           val f2 = (json \ "f2").validate[string]           val f3 = (json \ "f3").validate[string]     ... 

and need make sure of these fields present in order continue. otherwise, throw badrequest response. proper way of doing this? use get on these jsresult , use try-catch or else?

for comprehension part of answer removed because, @kipsigman pointed out, map/flatmap jsresult not behave applicative , accumulate failures. knew too. insert tony morris saying, "told so".

i wouldn't access body directly anyways , instead define reads/format either via macro below, or via applicative combinators @kipsigman.

case class myrequest(f1: string, f2: string, f3: string) object myrequest { implicit val reads: reads[myrequest] = json.reads[myrequest] }  action.async(bodyparsers.parse.json) { request =>   request.body.validate[myrequest].map {     myrequest =>        jsstring(s"f1 '${myrequest.f1}' f2 '${myrequest.f2}' f3 '${myrequest.f3}'")   } } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -