marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

Validate that the ID field exists, but don't load it

Open multimeric opened this issue 6 years ago • 2 comments

Following on from discussion here: https://github.com/json-api/json-api/issues/1427.

Basically, it makes sense for JSON API servers to validate that there is an ID in all data it deals with, BUT there are situations where you don't want to have to see this ID after you load some data.

For instance, I have an example where my server returns a dictionary that corresponds to the keyword arguments to plotly.plot(), but it won't accept an ID field in the kwargs.

My current hack is this:

class EphemeralSchema(Schema):
    id = f.String(dump_only=True)

data = EphemeralSchema(unknown=EXCLUDE).load(response.json)

However, this means I have to use unknown=EXCLUDE, which I don't actually want to do (I want to include arbitrary kwargs). I also can't use exclude=['id'], because that will trigger json-api's schema validation.

What is the best solution here? I'm happy to write a PR, but I need a little guidance on how to best implement this.

multimeric avatar Sep 17 '19 06:09 multimeric

@TMiguelT Did you ever figure out a way to do this? I need to do something similar and can't figure out a way besides unknown=EXCLUDE (which I don't want to do).

olinger avatar Feb 19 '20 18:02 olinger

No, it looks like I'm still using unknown=EXCLUDE in the code that uses this, sadly.

multimeric avatar Feb 19 '20 22:02 multimeric