-
-
Notifications
You must be signed in to change notification settings - Fork 632
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
base: dev
Are you sure you want to change the base?
multiple fields in validates decorator #1965
Conversation
"""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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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'
})
Fix for #1960