marshmallow
marshmallow copied to clipboard
load many functionality doesn't seem to be of much help
I have a UserSchema with a pre_load(pass_many=False) decorator, suppose if the pre_load decorator raises ValidationError, all the objects in the collection I passed became invalid even though there are some valid objects available. IMO errors of one object shouldn't stop the other objects in the collection from being deserialized, validated, and post-processed.
The hack would be to run a loop on the collection and load the object one by one but it would be nice if the 'many' attribute can handle this. There are some trade-offs in loading the object one by one.
from marshmallow import Schema, ValidationError, fields, pre_load
class UserSchema(Schema):
name = fields.Str()
age = fields.Int()
height = fields.Float()
@pre_load(pass_many=False)
def check_age(self, data, **kwargs):
if data['age'] < 18:
raise ValidationError('Minor user', 'age')
return data
user_schema = UserSchema()
data = [
{
'name': 'dk',
'age': 20,
'height': '5.5'
},
{
'name': 'dd',
'age': 19,
'height': '5.5'
},
{
'name': 'df',
'age': 10,
'height': '5.5'
}
]
try:
user_schema.load(data, many=True)
except ValidationError as err:
print(err.messages, err.valid_data)
for d in data:
try:
d = user_schema.load(d)
print(d)
except ValidationError as err:
print(err.messages, err.valid_data)
Suggestions are welcome.