Skip to content

Fix bug in validators documentation #8779

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

Merged
merged 1 commit into from
Dec 1, 2022
Merged

Conversation

FlorisHoogenboom
Copy link
Contributor

Function validators seem to have to return their value (which will become part of validated_data) as part of their signature.

Note: Before submitting this pull request, please review our contributing guidelines.

Description

Please describe your pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue. When linking to an issue, please use refs #... in the description of the pull request.

Function validators seem to have to return their value (which will become part of `validated_data`) as part of their signature.
@auvipy auvipy self-requested a review November 28, 2022 04:27
@auvipy auvipy merged commit 614bd87 into encode:master Dec 1, 2022
@Ariki
Copy link

Ariki commented Dec 23, 2022

I believe it's a mistake. There is no much difference between function based and class based validators. These are just two different ways to provide a callable, and the returned values are not used.

In contrast to this, .validate_<field_name> serializer methods must return the validated value.

@auvipy
Copy link
Member

auvipy commented Dec 23, 2022

are you suggesting to revert this?

@FlorisHoogenboom
Copy link
Contributor Author

FlorisHoogenboom commented Dec 23, 2022

Are you sure about that @Ariki. I think you are right for the validators that come with Django but it seems behaviour of dango-rest is a bit different here, but I might be off.

If I just run a test and trace the stack I eventually end up here:

validated_value = validate_method(validated_value)

I don't have time to create a simple example to reproduce right now, but I remember I initiated this change because I was running into unexplainable null values suddenly when adding validators which seems to agree with the snippet above.

@Ariki
Copy link

Ariki commented Dec 23, 2022

The code line above is responsible for running validation methods of a serializer which indeed should return a value. The class-based and function-based validators are run in run_validators method which mostly inherits its behavior from the Field class. This method does not use values returned from the validators it calls.

if getattr(validator, 'requires_context', False):
validator(value, self)
else:
validator(value)

So yes, I suggest to revert this.

@FlorisHoogenboom FlorisHoogenboom deleted the patch-1 branch December 23, 2022 17:15
@FlorisHoogenboom
Copy link
Contributor Author

Ok I see what you are mean. You are right indeed, but I must say I find that difference in behaviour of those two than rather confusing to be honest. Any design choice that's behind that?

auvipy added a commit that referenced this pull request Dec 23, 2022
@auvipy
Copy link
Member

auvipy commented Dec 23, 2022

opened a revert PR

auvipy added a commit that referenced this pull request Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants