I have a controller defined like this:
def registerCompany = Action.async(BodyParsers.parse.json) { request =>
request.body.validate[Company].fold(
errors => Future {
BadRequest(errors.mkString)
},
company => Future {
registrationService.registerCompany
Ok("saved")
}
)
}
Company is a simple case class
case class Company(name: String, address: Address, adminUser: Option[User] = None,
venues: Option[Set[Venue]] = None, _id: Option[Long]) {
}
so that I can take advantage of
implicit val companyFormatter = Json.format[Company]
So far so good, but now I want to have validation in the Company class. I've been googling a bit and the best I found was this:
http://koff.io/posts/292173-validation-in-scala/
So many solutions, yet, I'm not happy with any of them. Most of these solutions have known limitations or are a bit messy. I'd like to have declarative validation (annotation based), as that means I write less code and it looks cleaner.
I could mix java with scala and use JSR-303, but it doesn't work for case classes and I don't want to implement Reads and Writes for simple objects.
This is the closest I could find to what I want, but it doesn't support NotNull: https://github.com/bean-validation-scala/bean-validation-scala
Seems a bit like a luxury problem, with so many different solutions, but the truth is that in Java I can get the best of both worlds.
Is there anything else that I could use? Or any work around to the possibilities I'm listing here that could allow me to use both annotation based validation and case classes?