You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed that when validating a phone number a TypeError is raised. Comparing this to the EmailField where a ValidationError is raised. I would expect also a ValidationError. Is this behaviour intended?
Traceback (most recent call last):
File "/usr/lib/python3.9/code.py", line 90, in runcode
exec(code, self.locals)
File "", line 1, in
File "/path/to/test_phonenumberfield.py", line 16, in
print('Is phone valid:', s.is_valid())
File "/path/to/venv/lib/python3.9/site-packages/rest_framework/serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/path/to/venv/lib/python3.9/site-packages/rest_framework/serializers.py", line 419, in run_validation
value = self.to_internal_value(data)
File "/path/to/venv/lib/python3.9/site-packages/rest_framework/serializers.py", line 476, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/path/to/venv/lib/python3.9/site-packages/rest_framework/fields.py", line 799, in run_validation
return super().run_validation(data)
File "/path/to/venv/lib/python3.9/site-packages/rest_framework/fields.py", line 568, in run_validation
value = self.to_internal_value(data)
File "/path/to/venv/lib/python3.9/site-packages/phonenumber_field/serializerfields.py", line 12, in to_internal_value
phone_number = to_python(data)
File "/path/to/venv/lib/python3.9/site-packages/phonenumber_field/phonenumber.py", line 149, in to_python
raise TypeError("Can't convert %s to PhoneNumber." % type(value).name)
TypeError: Can't convert int to PhoneNumber.
I found some related issue but none seems to cover this: #306, #225, #389, #265
Sorry if I missed the relevant discussion
The text was updated successfully, but these errors were encountered:
That’s indeed a bug. A TypeError is not intended, the serializer should be able to safely validate input. Otherwise, it leads to crashes.
The to_python() method expects its input to be a string. It works well with urlencoded data received with a regular form post, because the data is a string, but not so well with DRF, as DRF accepts richer representations (JSON, XML, etc).
IMO, the serializer should make sure to represent the data as string before handling it to the to_python helper.
So if data is not a PhoneNumber instance the CharField.to_internal_value is used to return a string. This way the
else-case in to_python is never reached and the TypeError is not raised.
I noticed that when validating a phone number a
TypeError
is raised. Comparing this to theEmailField
where aValidationError
is raised. I would expect also aValidationError
. Is this behaviour intended?Output:
Output:
I found some related issue but none seems to cover this: #306, #225, #389, #265
Sorry if I missed the relevant discussion
The text was updated successfully, but these errors were encountered: