marshmallow_dataclass icon indicating copy to clipboard operation
marshmallow_dataclass copied to clipboard

Improve error message when enum validation fails

Open drewm1980 opened this issue 2 years ago • 0 comments

I made the following usage mistake:

I used another json library (orjson) to convert to and from JSON, thinking I could get away with only using marshmallow for deserialization.

orjson serializes Enums by value, but marshmallow serializes Enums by key, resulting in an error:

  File "/home/awagner/miniconda3/envs/luigi_py36/lib/python3.6/site-packages/marshmallow/schema.py", line 904, in _do_load
    raise exc
marshmallow.exceptions.ValidationError: {'model_size': ['Invalid enum member MyEnumValue']}

The solution was to use marshmallow for both serialization and deserialization.

If this is a common enough mistake, perhaps the error message could be improved, especially since you can detect if the value found matches one of the expected enum values. (in my case the Enum was string valued)

Otherwise, this ticket at least gives something for people to hit while searching.

Thanks for the useful library!

drewm1980 avatar Mar 04 '22 11:03 drewm1980