Improve e164 normalization check by re-parsing without country code
This commit is contained in:
parent
6d0345d327
commit
06eb890761
|
@ -42,11 +42,30 @@ public class Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final PhoneNumber phoneNumber = PHONE_NUMBER_UTIL.parse(number, null);
|
final PhoneNumber inputNumber = PHONE_NUMBER_UTIL.parse(number, null);
|
||||||
final String normalizedNumber = PHONE_NUMBER_UTIL.format(phoneNumber, PhoneNumberFormat.E164);
|
|
||||||
|
|
||||||
if (!number.equals(normalizedNumber)) {
|
// For normalization, we want to format from a version parsed with the country code removed.
|
||||||
throw new NonNormalizedPhoneNumberException(number, normalizedNumber);
|
// This handles some cases of "possible", but non-normalized input numbers with a doubled country code, that is
|
||||||
|
// with the format "+{country code} {country code} {national number}"
|
||||||
|
final int countryCode = inputNumber.getCountryCode();
|
||||||
|
final String region = PHONE_NUMBER_UTIL.getRegionCodeForCountryCode(countryCode);
|
||||||
|
|
||||||
|
final PhoneNumber normalizedNumber = switch (region) {
|
||||||
|
// the country code has no associated region. Be lenient (and simple) and accept the input number
|
||||||
|
case "ZZ", "001" -> inputNumber;
|
||||||
|
default -> {
|
||||||
|
final String maybeLeadingZero =
|
||||||
|
inputNumber.hasItalianLeadingZero() && inputNumber.isItalianLeadingZero() ? "0" : "";
|
||||||
|
yield PHONE_NUMBER_UTIL.parse(
|
||||||
|
maybeLeadingZero + inputNumber.getNationalNumber(), region);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
final String normalizedE164 = PHONE_NUMBER_UTIL.format(normalizedNumber,
|
||||||
|
PhoneNumberFormat.E164);
|
||||||
|
|
||||||
|
if (!number.equals(normalizedE164)) {
|
||||||
|
throw new NonNormalizedPhoneNumberException(number, normalizedE164);
|
||||||
}
|
}
|
||||||
} catch (final NumberParseException e) {
|
} catch (final NumberParseException e) {
|
||||||
throw new ImpossiblePhoneNumberException(e);
|
throw new ImpossiblePhoneNumberException(e);
|
||||||
|
|
|
@ -24,11 +24,13 @@ class ValidNumberTest {
|
||||||
"+71234567890",
|
"+71234567890",
|
||||||
"+447535742222",
|
"+447535742222",
|
||||||
"+4915174108888",
|
"+4915174108888",
|
||||||
|
"+2250707312345",
|
||||||
"+298123456",
|
"+298123456",
|
||||||
"+299123456",
|
"+299123456",
|
||||||
"+376123456",
|
"+376123456",
|
||||||
"+68512345",
|
"+68512345",
|
||||||
"+689123456"})
|
"+689123456",
|
||||||
|
"+80011111111"})
|
||||||
void requireNormalizedNumber(final String number) {
|
void requireNormalizedNumber(final String number) {
|
||||||
assertDoesNotThrow(() -> Util.requireNormalizedNumber(number));
|
assertDoesNotThrow(() -> Util.requireNormalizedNumber(number));
|
||||||
}
|
}
|
||||||
|
@ -56,6 +58,8 @@ class ValidNumberTest {
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ValueSource(strings = {
|
@ValueSource(strings = {
|
||||||
"+4407700900111",
|
"+4407700900111",
|
||||||
|
"+49493023125000", // double country code - this e164 is "possible"
|
||||||
|
"+494915110000000", // double country code - this e164 is "possible" and "valid"
|
||||||
"+1 415 123 1234",
|
"+1 415 123 1234",
|
||||||
"+1 (415) 123-1234",
|
"+1 (415) 123-1234",
|
||||||
"+1 415)123-1234",
|
"+1 415)123-1234",
|
||||||
|
|
Loading…
Reference in New Issue