Skinney / elm-phone-numbers / PhoneNumber

Functions for validating phone numbers

Metadata

You don't need to define any of these records yourself, please see the PhoneNumber.Countries module.


type alias Country =
{ id : String
, countryCode : String
, internationalPrefix : Maybe String
, nationalPrefix : Maybe String
, generalNumberPattern : Regex
, numberTypes : List NumberTypeData 
}

Contains phone related metadata for a country


type NumberType
    = FixedLine
    | Mobile
    | TollFree
    | PremiumRate
    | SharedCost
    | PersonalNumber
    | Voip
    | Pager
    | Uan
    | Emergency
    | Voicemail
    | ShortCode
    | StandardRate
    | CarrierSpecific
    | SmsServices
    | NoInternationalDialling

What kind of a number are we talking about? Fixed line? Mobile number? Pager number? etc.


type alias NumberTypeData =
{ numberType : NumberType
, exampleNumber : String
, pattern : Regex 
}

Describes a NumberType with an example number, as well as a regex to validate if a given number is of the set numberType

Validation


type alias ValidationConfig =
{ defaultCountry : Country
, otherCountries : List Country
, types : List NumberType 
}

Pass this record to valid or matches to define how validation should occur.

Validation will occur against the default country as well as the list of other countries, and validation will only occur against the list of number types. Providing a empty list of number types might still succeed, as every country has a general number pattern that will be checked.

Any number that doesn't have an international prefix will be treated as if it belongs to the default country.

anyType : List NumberType

A list of all number types. Use this if you don't care what kind of number something is, but want to know if the number can belong to a certain country.

valid : ValidationConfig -> String -> Basics.Bool

A simple test to see if the provided number matches anything provided by the validation config.

Query

matches : ValidationConfig -> String -> List ( Country, List NumberType )

Returns a list of tuples where the first element is a country that the number can belong to, and the second list contains the different types the number can be. It's possible the the second element is an empty list, as every country has a general number pattern that will be checked if the number type cannot be determined.