[Play-2.3][scala] validate request.body in ActionBuilder?

121 views
Skip to first unread message

Uriel Avalos

unread,
Feb 14, 2015, 6:06:08 PM2/14/15
to play-fr...@googlegroups.com

I'm using Playframework 2.3.X. I'm trying to construct an action function that validates fields in a JSON request.body. The use case is to build "blocks" of validation that I can then chain together.

However, I can't seem to access the request.body as a JSON inside the action builder. The following doesn't compile. The compiler can't resolve "asJson":

def ValidateJsonBodyAction = new ActionBuilder[Request] {

   def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = {

       val body= request.body.asJson
   }
}

UPDATE: It could also be that I'm approaching this the wrong way. I'm new to play, so alternative approaches are also welcome.

Julien Richard-Foy

unread,
Feb 16, 2015, 8:07:36 AM2/16/15
to play-fr...@googlegroups.com
Hi,

Indeed dealing with the request body is not the responsibility of ActionBuilders.

There is a BodyParser that may be useful for your usecase: parse.json[SomeDataType].

Onezino Gabriel Moreira

unread,
Feb 19, 2015, 11:41:14 AM2/19/15
to play-fr...@googlegroups.com
Hi Uriel, 

https://github.com/fge/json-schema-validator there is some libs to treat validation using Json-Schemas,
 alson a lib to auto generate schamas https://github.com/reinert/JJSchema/.

When you finish and if you can, share with us your code.
Reply all
Reply to author
Forward
0 new messages