joipolloi / json-validation-bundle

Symfony framework bundle providing an annotation for validating JSON passed with a request against a schema
MIT License
5 stars 5 forks source link

Return application/problem+json #1

Closed lvillarino closed 6 years ago

lvillarino commented 6 years ago

Hi:

How I return a application/problem+json format when the validation failed? https://tools.ietf.org/html/rfc7807

Thanks

johnnoel commented 6 years ago

Hi lvillarino,

That is a good question! I suppose at the moment there isn't an easy or clean way of doing so as the ValidateJsonListener throws a BadRequestHttpException rather than a bundle specific exception. If the bundle threw an InvalidJsonException (for example) that extended BadRequestHttpException then in theory you could set up a Symfony exception handler that listened for that exception and then produced the problem+json format you mention.

The handling of that exception would be application specific rather than included in a bundle as introducing more exception handlers would likely cause more work for developers down the line; either that or controlled through a configuration parameter. As for the unique exception, that's a fairly quick fix however I probably won't be able to look at this until sometime next week due to other commitments I'm afraid. If you want to submit a pull request though that would be great!

johnnoel commented 6 years ago

Hi lvillarino,

This should be easy to implement in your Symfony application now. I've made the listener throw a JsonValidationException when there is a problem with the JSON or the schema. So to solve your problem you'd set up a new exception listener (Symfony docs) that specifically catches the JsonValidationException and would then output your application/problem+json.

Hope that makes sense!

johnnoel commented 6 years ago

Hi @lvillarino,

Just a quick update to let you know that I've implemented an exception listener that can return application/problem+json responses and this should be available in version 1.0.3. The README has been updated with details but hopefully this should answer your query.

Many thanks for raising an issue about this, I had no idea about the content type so it was great to learn about it.

lvillarino commented 6 years ago

Thanks