This commit is contained in:
lash 2025-01-21 15:59:44 +00:00
parent ba8cbbccea
commit 9fbae0471f
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 9 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package identity
import ( import (
"regexp" "regexp"
) )
// Define the regex patterns as constants // Define the regex patterns as constants
const ( const (
addressRegex = `^0x[a-fA-F0-9]{40}$` addressRegex = `^0x[a-fA-F0-9]{40}$`

View File

@ -9,9 +9,9 @@ import (
// Identity contains all flavors of identifiers used across stream, api and // Identity contains all flavors of identifiers used across stream, api and
// client for a single agent. // client for a single agent.
type Identity struct { type Identity struct {
NormalAddress string NormalAddress string
ChecksumAddress string ChecksumAddress string
SessionId string SessionId string
} }
// CheckRecipient validates the recipient format based on the criteria // CheckRecipient validates the recipient format based on the criteria

View File

@ -39,8 +39,7 @@ func CalculateAgeWithYOB(yob int) int {
return currentYear - yob return currentYear - yob
} }
// IsValidYob checks if the provided yob can be considered valid
//IsValidYob checks if the provided yob can be considered valid
func IsValidYOb(yob string) bool { func IsValidYOb(yob string) bool {
currentYear := time.Now().Year() currentYear := time.Now().Year()
yearOfBirth, err := strconv.ParseInt(yob, 10, 64) yearOfBirth, err := strconv.ParseInt(yob, 10, 64)

View File

@ -8,7 +8,8 @@ import (
// Define the regex patterns as constants // Define the regex patterns as constants
const ( const (
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$` // TODO: This should rather use a phone package to determine whether valid phone number for any region.
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$`
) )
// IsValidPhoneNumber checks if the given number is a valid phone number // IsValidPhoneNumber checks if the given number is a valid phone number
@ -20,7 +21,7 @@ func IsValidPhoneNumber(phonenumber string) bool {
// FormatPhoneNumber formats a Kenyan phone number to "+254xxxxxxxx". // FormatPhoneNumber formats a Kenyan phone number to "+254xxxxxxxx".
func FormatPhoneNumber(phone string) (string, error) { func FormatPhoneNumber(phone string) (string, error) {
if !IsValidPhoneNumber(phone) { if !IsValidPhoneNumber(phone) {
return "", errors.New("invalid phone number") return "", errors.New("invalid phone number")
} }
// Remove any leading "+" and spaces // Remove any leading "+" and spaces
@ -29,11 +30,11 @@ func FormatPhoneNumber(phone string) (string, error) {
// Replace leading "0" with "254" if present // Replace leading "0" with "254" if present
if strings.HasPrefix(phone, "0") { if strings.HasPrefix(phone, "0") {
phone = "254" + phone[1:] phone = "254" + phone[1:]
} }
// Add "+" if not already present // Add "+" if not already present
if !strings.HasPrefix(phone, "254") { if !strings.HasPrefix(phone, "254") {
return "", errors.New("unexpected format") return "", errors.New("unexpected format")
} }
return "+" + phone, nil return "+" + phone, nil

View File

@ -12,7 +12,6 @@ const (
//Allowed incorrect PIN attempts //Allowed incorrect PIN attempts
AllowedPINAttempts = uint8(3) AllowedPINAttempts = uint8(3)
) )
// checks whether the given input is a 4 digit number // checks whether the given input is a 4 digit number