Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
20 lines (15 sloc) 660 Bytes
package com.example.api.controllers.utils
import com.example.api.models.exceptions.ModelFormatException
import play.api.libs.json._
import play.api.mvc.{ BodyParser, BodyParsers }
import scala.concurrent.ExecutionContext
trait ControllerConventions {
protected def jsonModelParser[ModelClass](path: JsPath)(implicit reads: Reads[ModelClass], ec: ExecutionContext): BodyParser[ModelClass] = {
BodyParsers.parse.json.map { jsValue =>
jsValue.validate(path.json.pick[JsObject]).flatMap(_.validate[ModelClass]) match {
case JsSuccess(model, _) => model
case JsError(errors) => throw ModelFormatException(errors)
}
}
}
}
You can’t perform that action at this time.