I am using marshmallow 2.14.
Given following code:
from marshmallow import Schema, fields, ValidationError, validates
class Broken(Schema):
f = fields.String(required=True, load_from='f-name')
f2 = fields.String(required=True, load_from='f2-name')
@validates('f')
def validate_string(self, data):
raise ValidationError('nope')
_, err = Broken().load({'f': 'hello', 'f2': None}) # does not throw validation error
print(err)
I get following output:
{'f': ['nope'], 'f2-name': ['Field may not be null.']}
But I would expect to end up with:
{'f-name': ['nope'], 'f2-name': ['Field may not be null.']}
There is seems to be no way to override this behavior.
I suppose it should use load_from field on load and dump_to on dump for errors.
In marshmallow 3, this changed a bit.
load_from is merged with dump_to into data_keydata_key is checked when deserializingSchemas are strictYour example would write:
from marshmallow import Schema, fields, ValidationError, validates
class Broken(Schema):
f = fields.String(required=True, data_key='f-name')
f2 = fields.String(required=True, data_key='f2-name')
@validates('f')
def validate_string(self, data):
raise ValidationError('nope')
try:
Broken().load({'f-name': 'hello', 'f2': None})
except ValidationError as exc:
print(exc)
And there is indeed an issue because when using @validates, the wrong field name is used ('f' instead of 'f-name':
{'f2-name': ['Field may not be null.'], 'f': ['nope']}
This can be fixed by modifying those two lines:
https://github.com/marshmallow-code/marshmallow/blob/2.x-line/marshmallow/schema.py#L889
https://github.com/marshmallow-code/marshmallow/blob/2.x-line/marshmallow/schema.py#L904
- field_name=field_name,
+ field_name=field_obj.load_from or field_name,
And likewise with data_key for the 3.x branch.
@m-novikov, would you be willing to send a PR with a test for both cases (many and not many)?
Please PR against 2.x branch. We can take care of porting to 3.x.
Thanks.
Yes, no problem. Will do it this week.
Most helpful comment
Yes, no problem. Will do it this week.