Last active
January 1, 2016 22:39
-
-
Save dnephin/8211419 to your computer and use it in GitHub Desktop.
Play custom JSON BodyParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ApiBodyParser extends BodyParsers { | |
def apply[T](key: String)(implicit rds: Reads[T]): BodyParser[T] = parse.json.flatMap { | |
jsValue => (jsValue \ key).validate[T] match { | |
case JsSuccess(item, path) => { | |
BodyParser("api parser") { | |
request => Done(Right(item), Input.Empty) | |
} | |
} | |
// TODO: json error message with validation error | |
case e: JsError => | |
parse.error(Future.successful(Results.BadRequest(e.toString))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment