codahale/dropwizard — pull request #288 by cobbzilla (2013-03-24 20:00:15+00:00)
Title: Provide a means for Validator failures to be returned to the client as a JSON response.

Some context is here: https://groups.google.com/d/msg/dropwizard-user/ZvO3mnDTJYg/Y4Pt3yzt300J

I had a similar problem to the person who posted this. My clients expect JSON, all the time, even for errors. And more generally, keeping validation errors in code-friendly format seems to be a good idea. The current Dropwizard code (in Validator) discards all the juicy and useful violation information and instead crams the errors into a List of Strings.

Yes, you can use your own Validator, but that path is kludgy.

This patch introduces a handful of new classes and concepts:
* ValidationResult, which is what the Validator will now return from calls to validate. ValidationResult contains the same List of Strings, but also contains a List of ConstraintViolations, so that callers can have more freedom to work with those as well.
* InvalidEntityToJsonExceptionMapper, to use in place of InvalidEntityExceptionMapper. This mapper keeps the same 422 HTTP response, but the entity body is now a JSON array of (messageTemplate, message, invalidValue) tuples.
* ValidatorConfiguration, a new top-level configuration that allows you to override the default InvalidEntityExceptionMapper, and can be configured by adding the following to your YML config.

here's the config snippet:

validation:
invalidEntityExceptionMapperClass: com.yammer.dropwizard.jersey.InvalidEntityToJsonExceptionMapper

All tests pass and this is working great for me. There might be better/cleaner ways to do the overriding though (the new top-level config might be unpalatable for reasons I'm unable to think of), so I'm open to suggestions if there is a better way.