From 44e51ac07e8068ea0fa600a93856bbf2110733ab Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Mon, 10 Mar 2025 13:52:41 +0200 Subject: [PATCH] Validate and format phone number --- go.mod | 1 + go.sum | 2 ++ utils/utils.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/go.mod b/go.mod index 6d05d75..de9ec15 100644 --- a/go.mod +++ b/go.mod @@ -69,6 +69,7 @@ require ( github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dongri/phonenumber v0.1.6 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-errors/errors v1.4.1 // indirect github.com/go-logr/logr v1.4.2 // indirect diff --git a/go.sum b/go.sum index bd0354a..fdef866 100644 --- a/go.sum +++ b/go.sum @@ -97,6 +97,8 @@ github.com/dimuska139/go-email-normalizer/v2 v2.0.0 h1:LH41ypO4BFast9bc8hNu6YEkR github.com/dimuska139/go-email-normalizer/v2 v2.0.0/go.mod h1:2Gil1j/rfUKJ5BHc/uxxyRiuk3YTg6/C3D7dz7jVQfw= github.com/dlsniper/debugger v0.6.0 h1:AyPoOtJviCmig9AKNRAPPw5B5UyB+cI72zY3Jb+6LlA= github.com/dlsniper/debugger v0.6.0/go.mod h1:FFdRcPU2Yo4P411bp5U97DHJUSUMKcqw1QMGUu0uVb8= +github.com/dongri/phonenumber v0.1.6 h1:qLikrBt6mvaY2lg3gBoZjbRPxD1CsDlvhgmb5PHThE8= +github.com/dongri/phonenumber v0.1.6/go.mod h1:cuHFSstIxh6qh/Qs/SCV3Grb/JMYregBLuXELvSYmT4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= diff --git a/utils/utils.go b/utils/utils.go index 2c8d90b..733680d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -4,6 +4,7 @@ import ( "bytes" emailverifier "github.com/AfterShip/email-verifier" normalizer "github.com/dimuska139/go-email-normalizer/v2" + "github.com/dongri/phonenumber" "github.com/jinzhu/now" "github.com/mohae/deepcopy" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" @@ -59,6 +60,35 @@ func PointerToValue[V any](value *V) V { return *new(V) // zero value of V } +func ValidateAndFormatPhoneNumber(phoneNumber string) (isValid bool, formattedNumber string) { + countryAlpha2Code := "" + + if strings.HasPrefix(phoneNumber, "+") { + formattedNumber = strings.TrimPrefix(phoneNumber, "+") + // If the number starts with a country code, get the country's alpha2 code from the number + i := phonenumber.GetISO3166ByNumber(formattedNumber, false) + if i.Alpha2 != "" { + countryAlpha2Code = i.Alpha2 + } + } else { + // Parse as a South African number by default + formattedNumber = phonenumber.Parse(phoneNumber, "ZA") + if formattedNumber != "" { + // Get the country's alpha2 code from the number + i := phonenumber.GetISO3166ByNumber(formattedNumber, false) + if i.Alpha2 != "" { + countryAlpha2Code = i.Alpha2 + } + } + } + + if countryAlpha2Code == "" { + return false, "" + } + + return true, formattedNumber +} + func ValidateEmailAddress(email string, options ...map[string]string) (string, error) { // To lower cleanedEmail := strings.ToLower(strings.TrimSpace(email)) -- GitLab