Closed koriroys closed 8 years ago
Thanks for thinking this through and getting things started!
I think step one is we should change the current API (or add a new one and deprecate the old one) to be Option 2 above, JSONAPI::Serializer.serialize_errors(errors)
. This would get rid of the first big problem of including the top-level data
key or needing to pass an object. We can do this without needing to build an adapter layer at this stage, it can just pass through the JSON into the errors
key like it currently does.
Then, stage 2 we should either add the adapter layer or a duck-typing check and then be able to handle ActiveModel::Errors
objects and automatically mash them into a valid JSON-API format. I've tried to avoid adding a configuration layer to this gem until absolutely required, and I think we can probably still avoid it for a while.
So, as a simple start, we should:
serialize(nil, errors: errors)
method and remove the documentation.serialize_errors(errors)
which basically just returns {errors: errors}
for now.Then stage two, separate PR:
respond_to?(:messages)
(or some other more specific part of ActiveModel::Errors
) and have logic for transforming the object into JSON-API valid error objects. This logic could exist as an ActiveModelErrorAdapter < JSONAPI::Serializer::ErrorAdapter
or something so that it can later be extended for other framework error objects.@koriroys If you can take on Step 1 that'd be great! Otherwise I can get to it in the next week or so, it's a quick change.
Another thought for later: we should probably just have a JSONAPI::Error
or JSONAPI::Serializer::Error
class, and the error adapter's job should just be to transform arbitrary objects into JSONAPI::Error
objects, which we then know how to serialize. This is how Ember's JSON-API error handling works internally.
Actually, leaving open for stage 2 support later on.
per #63, and the json-api spec,
The members data and errors MUST NOT coexist in the same document.
So I'm opening this issue to discuss a way forward.
@fotinakis I read that you don't want to bind this library to ActiveRecord, which I applaud. It is likely the most common case to be using AR though, so some kind of support, while leaving other options open seems good to me.
A first stab:
config/initializers/jsonapi_initializer.rb
option 1:
option 2:
option 3:
Of the three, I lean towards option 1, letting the user pass in the errors, and letting the configured
error_adapter
munge the errors into a form acceptable to JSON API.A user might use it like this:
some_controller.rb
Of course, you might be in the unenviable position of migrating an app to a new api, and so you should be able to override the configured error serializer when called. e.g.
I'm not sure where
some_custom_adapter.rb
would live though.