-
Notifications
You must be signed in to change notification settings - Fork 315
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
Cannot re-validate validated_data in DRF #542
Comments
One thing I realized as I typed this out - I could potentially just not call
|
I added a fix in #543. In the meantime, here is a monkeypatch that works, I have this in my ###
### Start workaround for https://github.com/stefanfoulis/django-phonenumber-field/issues/542
###
import phonenumber_field.serializerfields as phonenumber_serializerfields
from phonenumber_field.phonenumber import PhoneNumber, to_python
from rest_framework.exceptions import ValidationError
from rest_framework.fields import CharField
def _relaxed_flexible_to_internal_value(self, data):
if isinstance(data, PhoneNumber):
phone_number = data
else:
str_value = super(phonenumber_serializerfields.PhoneNumberField, self).to_internal_value(
data
)
phone_number = to_python(str_value, region=self.region)
if phone_number and not phone_number.is_valid():
raise ValidationError(self.error_messages["invalid"])
return phone_number
setattr(
phonenumber_serializerfields.PhoneNumberField,
"to_internal_value",
_relaxed_flexible_to_internal_value,
)
###
### End workaround
### |
Thanks for the detailed report. I can reproduce the issue with the elements you’ve provided. Calling the validation twice looks incorrect to me, but I haven’t had time to dig into this issue today to understand whether it’s something from the serializers, from DRF or this library. |
Looks like DRF also handles getting a richer type as input to
Other fields also are written in a manner that would silently accept a richer (or pre-processed) type. The suggestion makes sense to me, please go ahead and add a test to the PR, I’ll happily include your work and release a bugfix. Thank you! |
@francoisfreitag will do! Thanks so much for the thoughtful review! |
This is a bit of a specific issue with writable nested serializers and the changed behavior in #523. Since 6.4.0 many of my phone number fields now fail validation.
Here is a very contrived serializer makeup that illustrates the problem:
Here's an example of using these serializers:
When I run this code, I get back a
ValidationError
onnested.primary_phone_number
:Digging into this, this is because the DRF
CharField
only accepts astr
,int
,float
orbool
. In the change in #523, we unconditionally pass thedata
field to the super method. However, in this piece of code in thecreate()
:The
primary_phone_number
entry is already aPhoneNumber
because of the implementation ofto_internal_value
. Initializing theNestedSerializer
again with thevalidated_data
runs through validation again (with theis_valid
call) and the new call tosuper().to_initial_value()
fails.I thought perhaps the fix would be to use
self.initial_data
instead ofvalidated_data
which has the original string instance, but this goes against the DRF docs of how to use writable nested serializers.Here is a workaround that fixes the issue and is as flexible as versions < 6.4.0:
The text was updated successfully, but these errors were encountered: