marshmallow-oneofschema
marshmallow-oneofschema copied to clipboard
0 does not work as schema key
Suppose I want to map schemas like this:
class MySchema(OneOfSchema):
type_schemas = {
0: Schema1,
1: Schema2
}
When I try to load data with type 0, I get exception 'Missing data for required field.'.
Not sure that alone would solve your case but in those two lines:
https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/marshmallow_oneofschema/one_of_schema.py#L98
https://github.com/marshmallow-code/marshmallow-oneofschema/blob/master/marshmallow_oneofschema/one_of_schema.py#L174
if not type_schema:
could be replaced with
if type_schema is None:
Would you like to give it a try?
@lafrech the problem occurs during schema load.
👍 for the suggested solution, would also solve my use case with:
class MySchema(OneOfSchema):
type_schemas = {
True: Schema1,
False: Schema2
}
But I think this should be generalized to support None
values as well, so we should consider using a sentinel like marshmallow.missing
to mark that the value wasn't found