Feature/ogc 236 phone number type#2592
Conversation
❌ 1 Tests Failed:
View the top 1 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
| class PhoneNumberType(Enum): | ||
| ANY = 'any' | ||
| MOBILE = 'mobile' | ||
| FIXED_LINE = 'fixed_line' |
There was a problem hiding this comment.
Why are we defining our own type, if we could just reuse phonenumbers.PhoneNumberType? (I know they currently aren't real enums, so you have to use int at the annotation everywhere you accept those values, I opened an issue about that years ago: daviddrysdale/python-phonenumbers#229, so they're not as type-safe, but you also currently allow str instead of PhoneNumberType, so you're not gaining any type-safety anyways, you only get a runtime check, which you could still do if you used phonenumbers.PhoneNumberType)
ANY also seems unnecessary, why not just use None as the sentinel for any phone number type is valid?
In OCQMS I do the following:
if self.number_type in supported_types_for_region(region):
# Validate type if we can
number_type = number_type_for_number(numobj)
if number_type == PhoneNumberType.FIXED_LINE_OR_MOBILE:
if self.number_type in (
PhoneNumberType.FIXED_LINE,
PhoneNumberType.MOBILE
):
# make aggregate types match.
number_type = self.number_type
if number_type != self.number_type:
raise ValidationError(_(
'Invalid ${number_type}',
mapping={'number_type': self.format_number_type()}
))| valid_types, error = self.type_errors.get( | ||
| self.number_type, ((), self.invalid_phone_number) | ||
| ) | ||
| if phonenumbers.number_type(number) not in valid_types: |
There was a problem hiding this comment.
Depending on the country not all number types are possible, there's a supported_types_for_region function which tells you which types can be distinguished, if you can't distinguish the types you're asking for, then you shouldn't raise validation errors for them.
| type_errors = { | ||
| PhoneNumberType.FIXED_LINE.value: ( | ||
| ( | ||
| phonenumbers.PhoneNumberType.FIXED_LINE, | ||
| phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE | ||
| ), | ||
| fixed_line_required | ||
| ), | ||
| PhoneNumberType.MOBILE.value: ( | ||
| ( | ||
| phonenumbers.PhoneNumberType.MOBILE, | ||
| phonenumbers.PhoneNumberType.FIXED_LINE_OR_MOBILE | ||
| ), | ||
| mobile_required | ||
| ), | ||
| } |
There was a problem hiding this comment.
This seems overly complex, I would just special-case FIXED_LINE_OR_MOBILE in the validation function where we check if the types match. Like in the example code from OCQMS.
Org: Extend phone number validator
TYPE: Feature
LINK: ogc-236