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

@validates decorator doesn't work if attribute specified on field #350

Closed
density opened this Issue Dec 3, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@density

density commented Dec 3, 2015

tested on marshmallow 2.3.0, python 3.5.0

if a field has an attribute set on it, then there is no way to use the decorator @validates to validate the field. see the following test cases:

from marshmallow import Schema, fields, ValidationError, validates

class Broken(Schema):
    s = fields.String(attribute='string_name')

    @validates('s')
    def validate_string(self, data):
        raise ValidationError('nope')

class Broken2(Schema):
    s = fields.String(attribute='string_name')

    @validates('string_name')
    def validate_string(self, data):
        raise ValidationError('nope')

class Working(Schema):
    s = fields.String()

    @validates('s')
    def validate_string(self, data):
        raise ValidationError('nope')

Broken(strict=True).load({'s': 'hello'}) # does not throw validation error
Broken2(strict=True).load({'s': 'hello'}) # complains that 'string_name' field doesn't exist
Working(strict=True).load({'s': 'hello'}) # works (i.e., raises ValidationError)
@sloria

This comment has been minimized.

Member

sloria commented Dec 6, 2015

Thanks for reporting this, @density . I will look into this today.

@sloria sloria added the bug label Dec 6, 2015

@sloria sloria closed this in 6e51dc0 Dec 6, 2015

@sloria

This comment has been minimized.

Member

sloria commented Dec 6, 2015

This is now fixed in 2.4.0

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