Skip to content

Instantly share code, notes, and snippets.

@imeredith
Created September 12, 2012 03:39
Show Gist options
  • Save imeredith/3704141 to your computer and use it in GitHub Desktop.
Save imeredith/3704141 to your computer and use it in GitHub Desktop.
case class SomeModel(field1: String, field2)
def js[A:Reads](field: String)(implicit request: Request[JsValue]): Option[A] = (request.body \ field).asOpt[A]
def index = Action(parse.json) { implicit request =>
val field1 = js[String]("field1")
val field2 = js[String]("field2")
if(!field1.isDefined){
BadRequest(Json.toJson(Map("error" -> "Field 'field1' is not defined")))
} else if(!field2.isDefined){
BadRequest(Json.toJson(Map("error" -> "Field 'field2' is not defined")))
} else {
val model = SomeModel(field1.get, field2.get) //Should be ok because of the if checks above
// ...
// ...
Ok(Json.toJson(Map("message"->"everything was ok")))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment