New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong field name in errors dict when using @validates on load_from/data_key aliased field #748

Closed
m-novikov opened this Issue Mar 5, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@m-novikov
Contributor

m-novikov commented Mar 5, 2018

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.

@lafrech

This comment has been minimized.

Member

lafrech commented Apr 11, 2018

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']}

@lafrech lafrech added the bug label Apr 11, 2018

@lafrech lafrech changed the title from Inconsistent field names in error results. to Wrong field name in errors dict when using @validates on load_from/data_key aliased field Apr 11, 2018

@lafrech

This comment has been minimized.

Member

lafrech commented Aug 1, 2018

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.

@lafrech lafrech added the help wanted label Aug 1, 2018

@m-novikov

This comment has been minimized.

Contributor

m-novikov commented Aug 1, 2018

Yes, no problem. Will do it this week.

sloria added a commit that referenced this issue Aug 4, 2018

Merge pull request #903 from m-novikov/m-novikov/load-from
Use load_from as ValidationError field name when provided. Closes: #748

@sloria sloria closed this in a8c2cf4 Aug 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment