libphonenumber-csharp icon indicating copy to clipboard operation
libphonenumber-csharp copied to clipboard

PhoneNumberUtil.IsValidNumber returns false for valid E164 number

Open vlad-kyrychuk-nf opened this issue 2 years ago • 2 comments

We found a bug where a Valid E164 number can be parsed and formatted. However the formatted E164 number is marked as invalid by the phone number util.

using System.Collections.Generic;
using Xunit;

namespace PhoneNumbers.Test
{
    public class PhoneNumberUtilTests
    {
        private readonly PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.GetInstance();

        [Fact]
        public void CanRoundTripE164Number()
        {

            var phoneNumber = "+1 555-555-5555";

            var resultPhoneNumber = phoneNumberUtil.Parse(phoneNumber, null);
            var formattedNumber = phoneNumberUtil.Format(resultPhoneNumber, PhoneNumberFormat.E164);
            Assert.NotEqual(phoneNumber, formattedNumber);

            var parsedFormattedPhoneNumber = phoneNumberUtil.Parse(formattedNumber, null);
            var roundTrippedIsValid = phoneNumberUtil.IsValidNumber(resultPhoneNumber);
            Assert.True(roundTrippedIsValid);
        }
    }
}

vlad-kyrychuk-nf avatar Apr 12 '22 16:04 vlad-kyrychuk-nf

That is an invalid number, perhaps famously so https://en.wikipedia.org/wiki/555_(telephone_number) See the behavior here https://libphonenumber.appspot.com/phonenumberparser?number=%2B15555555555

twcclegg avatar Apr 12 '22 16:04 twcclegg

@twcclegg Thanks for the update, I think if IsValid returned a result with message we would have caught this sooner. I've updated our code on our end and it seems resolved. Basically we noticed that if we take one number of various formats it could round trip but some of the output from format failed validation. If I find a test case that breaks this I'll update this issue, thanks again for your time.

On a side note, is there any way to validate a number for specific format like (E164)?

niemyjski avatar Apr 12 '22 19:04 niemyjski

Apologies, I discovered recently I need to research my github notification settings. Anyway, for e.164 you can pass null for region code which otherwise won’t work for other formats.

twcclegg avatar Nov 27 '22 00:11 twcclegg