Marshmallow: Union field

Created on 8 Apr 2019  路  3Comments  路  Source: marshmallow-code/marshmallow

Can't seem to find this anywhere, but a field that allows an incoming value to be matched across several different types/fields would be pretty handy.

Example could be something like this:

fields.Union([fields.Str(validate=validate.Length(max=20)), fields.Int()])

Where if it failed to validate against the first field, it'd go through the rest of the fields provided until it finds one it matches, otherwise raising an error.

Most helpful comment

Here's an implementation. https://github.com/adamboche/python-marshmallow-union
I've added a link in the wiki as suggested.

All 3 comments

There isn't a built-in union field at the moment, and we don't have plans to add one in the near future. Since we won't get to this soon, I'm going to close this issue for now.

If you end up developing this as a separate package, please comment here and add it to the wiki: https://github.com/marshmallow-code/marshmallow/wiki/Ecosystem

Here's an implementation. https://github.com/adamboche/python-marshmallow-union
I've added a link in the wiki as suggested.

What's the recommended way of implementing this with marshmallow? validate?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lassandroan picture lassandroan  路  3Comments

j4k0bk picture j4k0bk  路  3Comments

rastikerdar picture rastikerdar  路  3Comments

ambye85 picture ambye85  路  4Comments

tadams42 picture tadams42  路  3Comments