Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a parameter is missing the error is returned without a source #93

Open
JoeWoodward opened this issue Sep 2, 2018 · 5 comments
Open

Comments

@JoeWoodward
Copy link

JoeWoodward commented Sep 2, 2018

Currently when my model validates a value that was completely excluded from the payload I get a response with empty pointers.

{
  "errors": [
    {
      "title": "Invalid user_id",
      "detail": "User can't be blank",
      "source": {}
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

In rails we know the source ahead of time. If it's not a query parameter then it must be a pointer.

3 situations...
resource :user, only: :show and no id is passed or we return 404 error
/url?page=1 we would either not require it or return a custom error
Missing payload attribute we should automatically infer the expected type and value of the source object e.g. source = { pointer: '/data/attributes/user_id}

This would then return

{
  "errors": [
    {
      "title": "Invalid user_id",
      "detail": "User can't be blank",
      "source": {
        "pointer": "/data/attributes/user_id"
      }
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}
@JoeWoodward
Copy link
Author

Been investigating and although this seems like it would be useful for debugging it's actually against the RFC6901 definition. A pointer can only point to a JSON key and cannot point to a missing key.

However, maybe it can be returned in the meta to help with debugging

@JoeWoodward
Copy link
Author

JoeWoodward commented Sep 2, 2018

Actually I think it should reference '/data/attributes'
http://jsonapi.org/examples/#error-objects-source-usage

{
  "errors": [
    {
      "source": { "pointer": "" },
      "detail":  "Missing `data` Member at document's top level."
    }
  ]
}

It uses source to point to the top-level of the document (""). (Pointing to “/” would be an appropriate reference to the string "some value" in the request document {"": "some value"}. Pointing to "/data" would be invalid because the request document did not have a value at "/data", and source is always given with reference to the request document.)

Following this format if @pointer is missing from the active record errors serializer class then we can assume that it should be pointing to the /data/attributes node

@JoeWoodward
Copy link
Author

#94

@bsylvain
Copy link

bsylvain commented Mar 5, 2019

Also if the parameter has been aliased :

   attribute :reply do
       @object.content
     end

The source is empty. There should be a way to set alias for errors also

@beauby
Copy link
Member

beauby commented Apr 5, 2019

@bsylvain This should be the case, providing you use jsonapi-rb deserializers. Could you show a minimal example where this fails?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants