ex-phone-number / ex_phone_number Goto Github PK
View Code? Open in Web Editor NEWElixir port of libphonenumber
License: MIT License
Elixir port of libphonenumber
License: MIT License
Can you release a new patch version instead of recommending using a reference to the git repo? The last few commits should be fine to be bundled into a 0.1.1 release without worrying about anything breaking.
Thank you for your work on porting libphonenumber
. We've been using it within our project with great success. Is is possible however to publish this package to Hex.pm?
It's definitely installed because I'm validating phone numbers just fine with it in my code but the mix task never appears in my list of available tasks.
I'm guessing Apache 2.0 (which most Elixir projects seem to use) or MIT.
I've been using this library for a few years, but there are errors to format and validate Argentina numbers.
A formatted national number of AR by the library is invalid and not formatted again properly.
The only difference of result is 'italian_leading_zero: true' at second formatting.
Could you check it? Thanks. :)
Initial formatting
iex> {:ok, phone_number} = ExPhoneNumber.parse("+54 114321-1200", "AR")
{:ok,
%ExPhoneNumber.Model.PhoneNumber{
country_code: 54,
country_code_source: nil,
extension: nil,
italian_leading_zero: nil,
national_number: 1143211200,
number_of_leading_zeros: nil,
preferred_domestic_carrier_code: nil,
raw_input: nil
}}
iex> ExPhoneNumber.format(phone_number, :national)
"011 4321-1200"
iex> ExPhoneNumber.is_valid_number?(phone_number)
true
Second formatting
iex> {:ok, phone_number} = ExPhoneNumber.parse("011 4321-1200", "AR")
{:ok,
%ExPhoneNumber.Model.PhoneNumber{
country_code: 54,
country_code_source: nil,
extension: nil,
italian_leading_zero: true,
national_number: 1143211200,
number_of_leading_zeros: nil,
preferred_domestic_carrier_code: nil,
raw_input: nil
}}
iex> ExPhoneNumber.format(phone_number, :national)
"01143211200"
iex> ExPhoneNumber.is_valid_number?(phone_number)
false
Seeing as the metadata used by this library is three years old and Google is making metadata changes rather frequently is it possible that you could add support for the new metadata files? I tried your instructions in #22.
You can try to:
- Replace this file from the ex_phone_number repository with this file from google libphonenumber.
- Test if it works.
- Add create a pull request here with the changes from point 1. We are welcome to update this.
But it 132/438 tests failed when doing so.
Hi, this is a doubt not an issue, I see that the Ruby implementation return the country code from a given phone number, like this:
number = GlobalPhone.parse('+1-312-555-1212')
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
Does this Elixir implementation return the same? I couldn't get this info, not even in tests.
Thanks!
This library doesn't appear to handle short codes properly.
Check this case:
iex(1)> {:ok, phone_number} = ExPhoneNumber.parse("88811", "US")
{:ok,
%ExPhoneNumber.Model.PhoneNumber{
country_code: 1,
national_number: 88811,
extension: nil,
italian_leading_zero: nil,
number_of_leading_zeros: nil,
raw_input: nil,
country_code_source: nil,
preferred_domestic_carrier_code: nil
}}
iex(2)> ExPhoneNumber.is_valid_number?(phone_number)
false
That is a valid shortcode, from which I received a confirmation code.
I'd be willing to take this on. A little guidance on where to read the code to port from in libphonenumber, and where it should go in ex_phone_number would be super helpful.
Any possibility of cutting a new release with the updated dependencies?
I am running erlang 1:18.2 and Elixir 1.2.4 on a VirtualBox with 2GB of memory. When I try to compile this project, I get
Crash dump is being written to: erl_crash.dump...*** stack smashing detected ***: /usr/lib/erlang/erts-7.2/bin/beam terminated
Typically this can be caused by memory pressure on the VM, but mem shows 600MB free at the time of the crash.
Hi,
One of the dependency is not safe according to mix deps.audit
. Here is the full output of the command :
Name: sweet_xml
Version: 0.6.6
Lockfile: /home/runner/work/app/app/mix.lock
CVE: 2019-15160
URL: https://github.com/kbrw/sweet_xml/issues/71
Title: Inline DTD allows XML bomb attack
Patched versions: >= 0.7.0
Vulnerabilities found!
Would it be possible to update that dependency and create a current release ?
iex(18)> {:ok, a} = ExPhoneNumber.parse("07 64 01 94 08 ", "US")
{:ok,
%ExPhoneNumber.Model.PhoneNumber{
country_code: 1,
country_code_source: nil,
extension: nil,
italian_leading_zero: true,
national_number: 764019408,
number_of_leading_zeros: nil,
preferred_domestic_carrier_code: nil,
raw_input: nil
}}
iex(19)> a |> ExPhoneNumber.is_possible_number?()
true
764019408
as a 9 digit number could physically never be a possible US phone number.
What exactly does is_possible_number?
check for?
Hi, first of all thanks for this library. Do you have plan to release newer version with updated dependencies and some validations soon?
thanks
Hi,
I just had a quick look through library and please correct me if I'm wrong but it seems that ex_phone_number reads & parses xml file on every function call.
If this is the case and there are enough resources at your disposal it would be nice to load & parse XML only once and query it directly from memory.
Hello!
I can see that the latest hex release is from 2020. There a lot of changes since then in the develop
branch.
Any plans to release that?
Hi, thank you for the helpful library you've built.
I stumbled upon a problem validating US phone numbers with area code 689 which seems to be a fairly new (several years old) phone area code for somewhere in Florida, at least according to this article.
When I test at https://libphonenumber.appspot.com with a number such as +16894023931
it validates it successfully, but this returns false:
> {:ok, parsed_phone} = ExPhoneNumber.parse("+16894023931", "US")
> ExPhoneNumber.is_valid_number?(parsed_phone)
false
It would be a nice to have a summary for any breaking changes that need to be rectified between version changes. It looks like a minor version was bumped today for formatting the codebase but I haven't checked to see if there were any API updates. A changelog would be a welcome addition.
Hi,
Do you have any plans to support tzdata ? eg https://github.com/daviddrysdale/python-phonenumbers/blob/dev/python/phonenumbers/tzdata/data0.py
It would be nice if this lib could parse ExPhoneNumber.parse("+977123456789")
without passing the country.
Lets say i have changeset like follow
def changeset_user_register(struct, params \%{}) do
struct
|> cast(params, [:name, :email, :countryCode, :phone])
|> validate_required(:name, [message: "Name Must Not Be Empty"])
|> validate_required(:countryCode, [message: "countryCode Must Not Be Empty"])
|> validate_required(:phone, [message: "Phone Number Must Not Be Empty"])
|> valid_phone_number
end
I am trying to validate number is valid or not so created custom validation like follow
'
defp valid_phone_number(struct) do
countryCode = get_field(struct, :countryCode)
phone = get_field(struct, :phone)
{result, phone_number} = ExPhoneNumber.parse(phone, countryCode)
if ExPhoneNumber.is_valid_number?(phone_number) do
struct
else
add_error(struct, :phone, "invalid number")
end
struct
end
When is run the application it shows no function clause matching in ExPhoneNumber.is_valid_number?/1
what mistake i made here?
Hi all,
We noticed PhoneNumberMetadata to be out of date for Mexican numbers. We updated a fork with the Mexican metadata, but we'd love to help maintain this library.
Happy to open a PR. Should we update just the mexican data (like we did in our fork) or the entire file? Does the community have a preference?
Updates to the PhoneNumberMetadata.xml
have not been published to Hex.pm which is causing validation errors when the phone number is actually valid. For example, US area code 689
is not valid in version 0.2.1
from Hex.
Hey folks
When we update this lib from version 0.3.0 to 0.4.0 it fails due to Tesla.
==> ex_phone_number
Compiling 22 files (.ex)
== Compilation error in file lib/ex_phone_number/mix/tasks/update_metadata.ex ==
** (CompileError) lib/ex_phone_number/mix/tasks/update_metadata.ex:11: module Tesla is not loaded and could not be found
(elixir 1.14.3) expanding macro: Kernel.use/1
lib/ex_phone_number/mix/tasks/update_metadata.ex:11: Mix.Tasks.UpdateMetadata.GitHub (module)
could not compile dependency :ex_phone_number, "mix compile" failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile ex_phone_number", update it with "mix deps.update ex_phone_number" or clean it with "mix deps.clean ex_phone_number"
This dependency is declared in the lib, but only on dev and test, so I believe it's only installed on the lib development and testing, but not when it's imported in other project. Is this indented?
My project also has Tesla, but I believe it's compiled later by mix.
This is how the dependency is on mix.lock
:
"ex_phone_number": {:hex, :ex_phone_number, "0.4.0", "2f9945be9f09bc892f9d50a504669dbe2cd590eb64ed110a2d8dbc551878337a", [:mix], [{:sweet_xml, "~> 0.7", [hex: :sweet_xml, repo: "hexpm", optional: false]}], "hexpm", "1a78506a19ee1d7bee511c81a8be35cca23053e519d6f47354926bd302aa202f"},
When ex_phone_number is included into a project, it generates a lot of noise when compiling for the first time:
17:39:40.286 [debug] region_code: "SZ"
17:39:40.286 [debug] has not national_prefix
17:39:40.286 [debug] national_prefix_formatting_rule: ""
17:39:40.286 [debug] carrier_code_formatting_rule: ""
17:39:40.286 [debug] available_formats count: 1
17:39:40.287 [debug] ---> number_format
(...)
while this might be helpful when actively developing the library, this should be disabled by default when using it elsewhere.
function called:
{:ok, ph} = ExPhoneNumber.parse("6191805152", "IN")
ExPhoneNumber.is_valid_number?(ph)
Expected response: true
Current response: false
Response from libphonenumber
: true
http://libphonenumber.appspot.com/phonenumberparser?number=6191805152&country=IN
As of now, Hex docs are empty for the functions. Although docs and some of the example usage is already present in the README, it would be nice to have the same and more on the hexdocs.
Hi, I was looking to get the region (or region code) from a number, and was wondering if you would be open to pull requests for such a feature, and if so, how to structure it.
Any number starting with +614 60 gets validated as false, even though it is a valid Australian mobile number.
It seems to be falsely checking a non existent region code as well as the country code (61)
I see no use-case for having those 2 branches at once.
Not having master
as the default branch can be confusing. See #27 (comment).
Unsafe variable found at:
lib/ex_phone_number/metadata/phone_metadata.ex:191
warning: the variable "phone_metadata" is unsafe as it has been set inside a case/cond/receive/if/&&/||. Please explicitly return the variable value instead. For example:
case int do
1 -> atom = :one
2 -> atom = :two
end
should be written as
atom =
case int do
1 -> :one
2 -> :two
end
cc @josemrb
I was redirected here from elixir-ecto/ecto#4383 by @josevalim
Correct. You must file a bug in ExPhoneNumber, since it should be able to handle the value it loads in cast. I will improve the docs here.
I will just paste the bug description I posted there:
We use EctoPhoneNumber to store fields in the database. This validates the data, but it can get invalidated when obfuscating.
With ecto schema backed by db when I have a field:
field(:contact_number, EctoPhoneNumber)
it loads the invalid data still fine.
Repo.get(Customer, 2929) |> Map.get(:contact_number)
#EctoPhoneNumber<14111111111>
Even when it's not valid
CustomersCore.LegacyRepo.all("customers" |> where([c], c.id == 2929) |> select([c], c.contact_number)) |> hd()
"14111111111"
"customers" |> where([c], c.id == 2929) |> select([c], c.contact_number) |> CustomersCore.LegacyRepo.all() |> hd() |> EctoPhoneNumber.cast()
:error
My problem is that I created an embedded schema for use with elastic search that has the same data and field definition and it fails when loading with Ecto.embedded_load.
Ecto.embedded_load(Customer, customer["_source"], :json)
** (ArgumentError) cannot load `"14111111111"` as type EctoPhoneNumber for field `contact_number` in schema CustomersCore.Schemas.Legacy.Customer
(ecto 3.11.1) lib/ecto/schema/loader.ex:76: anonymous fn/5 in Ecto.Schema.Loader.unsafe_load/4
(elixir 1.16.1) lib/enum.ex:2528: Enum."-reduce/3-lists^foldl/2-0-"/3
iex:24: (file)
```
### Expected behavior
I would expect that schema and embedded schema behave in the same way.
I am running Elixir 1.3.1 and it gave me the following error message: error: pathspec 'origin/master' did not match any file(s) known to git.
I believe it is because the master branch has been deleted and replaced with develop.
Thanks for the great package.
Would it be possible to make frequent releases, for example when the metadata is updated? This would make it easier to manage this dependency in other projects.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.