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

Validate known fields and keep unknown fields? #747

Closed
DanielFerreiraJorge opened this Issue Mar 1, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@DanielFerreiraJorge

DanielFerreiraJorge commented Mar 1, 2018

Hi, I have a use case where I have a defined schema with some fields that need to be validated, and other fields that are "unknown" (a json document of this type has over 700 fields in various nesting levels) and does not need validation but need to be serialized and deserialized. Is this something that is possible with marshmallow? I could not find an answer for this specific use case in the docs. I saw #127 but this is not quite what I need. I need to validate everything defined in the schema and let it pass anything that is not defined.

Thanks!

@DanielFerreiraJorge DanielFerreiraJorge changed the title from Validate known field and keep unknown fields? to Validate known fields and keep unknown fields? Mar 1, 2018

@rlubke

This comment has been minimized.

rlubke commented Mar 5, 2018

I was just looking for a similar feature. For evolvability, it would be handy to have a way to have access to all field/values that weren't handled by the deserialization process to allow handling after the fact.

@ramnes

This comment has been minimized.

Contributor

ramnes commented Mar 16, 2018

See that PR and @sloria's answer: #595

@lafrech

This comment has been minimized.

Member

lafrech commented Apr 11, 2018

Closing as duplicate of #718.

If the feature described in there was added, this could be achieved by adding

    class Meta:
        additional_properties = fields.Raw()

to the Schema, for instance.

In the meantime, you should be able to achieve this with a post_load method: it receives both result and original_data, so you can complete result with original_data. I believe this answers @rlubke's comment.

Please react if I'm wrong and I can reopen.

@lafrech lafrech closed this Apr 11, 2018

@sloria

This comment has been minimized.

Member

sloria commented Jul 5, 2018

It is now possible to keep unknown fields in 3.0.0b12.

https://marshmallow.readthedocs.io/en/3.0/quickstart.html#handling-unknown-fields

See #853 for a proposal to specify an output field for unknown fields.

@rlubke

This comment has been minimized.

rlubke commented Jul 6, 2018

This is good to see. Looking at the docs, it's unclear to me how the unknown fields are stored and how they can be used.

For example, if we're using this option, and I deser an entity that has unknown fields, if I serialize the same instance again, are those unknown fields inserted into the stream automatically?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment