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
Normal Nigerian number should be 10 digits long excluding country code #488
Comments
Hi, It may be that the phone number your are using is not actually correct, despite being correctly formatted. Perhaps try to construct it directly with the phonenumbers library: >>> phonenumbers.is_valid_number(
... phonenumbers.parse("+2341234567", "NG")
... )
True However: >>> phonenumbers.is_valid_number(
... phonenumbers.parse("+2340000000", "NG")
... )
False |
Thank you @francoisfreitag . I already fixed the issue. |
I am forced to reopen this issue. Let me be more elaborate. I have a serializer that tries to validate input phone number. From the input, I get the value inputted and format it using:
Then, as part of the validation, I created a regex and searches the formatted number for a conformity with my regex. If it doesn't, I want a
When I test this serializer, with an input number that doesn't match the regex, I get the following error:
Why this? |
The input value is converted from a string to a PhoneNumber python object by the serializer field, then verified for validity: django-phonenumber-field/phonenumber_field/serializerfields.py Lines 12 to 14 in 687f009
That verification occurs before your custom validation kicks in, which is why you are seeing the library error message and not yours.
|
Thanks for that. How can I ensure my test passes with my custom error raised? |
Maybe supply a valid phone number that does not match your regular expression? |
I have done that. However, test coverage is lower than the threshold (100%) because of that line. It's saying that custom validation error hasn't been covered. |
If the coverage says your line is not executed, there’s likely something wrong in your test. Re-read it carefully or use a debugger to step through the execution and understand why your line is not executed. |
I have fixed it. I'll provide the detail soon |
What I did was: in my test, I put a number that returns |
I am currently faced with the issue of validating Nigerian numbers. Normally, a typical Nigerian number is
+234 XXX XXX XXXX
or0XX XXX XXX XX
and when I test the formatted number against this regexr'^(0|\+234)[0-9]{10}'
, I always get this assertion errorMy
settings.py
hasPHONENUMBER_DEFAULT_REGION = 'NG'
, and I tested my regex against the return value ofphonenumbers.format_number( phonenumbers.parse(str(value), settings.PHONENUMBER_DEFAULT_REGION), phonenumbers.PhoneNumberFormat.E164 )
The text was updated successfully, but these errors were encountered: