Skip to content
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

multiple fields in validates decorator #1965

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

dharani7998
Copy link

Fix for #1960

"""Register a field validator.

:param str field_name: Name of the field that the method validates.
"""
return set_hook(None, VALIDATES, field_name=field_name)
return set_hook(None, VALIDATES, field_names=field_names)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we try to preserve this hook interface for a minor release? The docstring for set_hook says "You should not need to use this method directly". Any objection to making validates return a wrapper that loops the calls to set_hook instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how the wrapper will work if we loop and call set_hook, __marshmallow_hook__[VALIDATES] value gets overwritten each time and ends with the last argument passed in the validates decorator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work, try this. It will only register the last field given in the decorator

from marshmallow import (
    Schema,
    fields,
    pre_dump,
    post_dump,
    pre_load,
    post_load,
    # pre_validate,
    validates,
    validates_schema,
    ValidationError,
    EXCLUDE,
    INCLUDE,
    RAISE,
)

class TestValidates(Schema):
    value = fields.Str()
    another_value = fields.Str()
    
    @validates('value', 'another_value')
    def validates_function(self, data, **kwargs):
        print(f'Inside validates: {{ {data}:{data} }}')


schema = TestValidates()
print(schema._hooks['validates'])
validator = getattr(schema, schema._hooks['validates'][0])
print(validator.__marshmallow_hook__)
print()



schema.load({
    'value': 'value',
    'another_value': 'another_value'
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants