github.com/weaviate/weaviate@v1.24.6/usecases/objects/validation/phone_numbers.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package validation 13 14 import ( 15 "fmt" 16 "strings" 17 18 "github.com/nyaruka/phonenumbers" 19 "github.com/weaviate/weaviate/entities/models" 20 ) 21 22 func parsePhoneNumber(input, defaultCountry string) (*models.PhoneNumber, error) { 23 defaultCountry = strings.ToUpper(defaultCountry) 24 num, err := phonenumbers.Parse(input, defaultCountry) 25 if err != nil { 26 switch err { 27 case phonenumbers.ErrInvalidCountryCode: 28 return nil, fmt.Errorf("invalid phone number: invalid or missing defaultCountry - this field is optional if the specified number is in the international format, but required if the number is in national format, use ISO 3166-1 alpha-2") 29 default: 30 return nil, fmt.Errorf("invalid phone number: %v", err) 31 } 32 } 33 34 return &models.PhoneNumber{ 35 National: num.GetNationalNumber(), 36 NationalFormatted: phonenumbers.Format(num, phonenumbers.NATIONAL), 37 InternationalFormatted: phonenumbers.Format(num, phonenumbers.INTERNATIONAL), 38 CountryCode: uint64(num.GetCountryCode()), 39 DefaultCountry: defaultCountry, 40 Input: input, 41 Valid: phonenumbers.IsValidNumber(num), 42 }, nil 43 }