Marshmallow: Wrong field name in errors dict when using @validates on load_from/data_key aliased field

Created on 5 Mar 2018  路  3Comments  路  Source: marshmallow-code/marshmallow

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.

bug help wanted

Most helpful comment

Yes, no problem. Will do it this week.

All 3 comments

In marshmallow 3, this changed a bit.

  • load_from is merged with dump_to into data_key
  • only data_key is checked when deserializing
  • also, all Schemas are strict

Your 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sloria picture sloria  路  3Comments

pd-Shah picture pd-Shah  路  4Comments

agatheblues picture agatheblues  路  3Comments

ambye85 picture ambye85  路  4Comments

jayennis22 picture jayennis22  路  4Comments