Compare commits

..

No commits in common. "master" and "alias-period" have entirely different histories.

6 changed files with 10 additions and 43 deletions

View File

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

View File

@ -5,7 +5,7 @@ import (
)
const (
aliasRegex = `^(?i)[a-z][a-z0-9]*(\.[a-z0-9]+)*$`
aliasRegex = `^[a-zA-Z0-9]+(\.[a-zA-Z0.9]+)*$`
)
// IsValidAlias checks if the alias is a valid alias format

View File

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

View File

@ -39,7 +39,8 @@ func CalculateAgeWithYOB(yob int) int {
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 {
currentYear := time.Now().Year()
yearOfBirth, err := strconv.ParseInt(yob, 10, 64)

View File

@ -8,10 +8,7 @@ import (
// Define the regex patterns as constants
const (
// TODO: This should rather use a phone package to determine whether valid phone number for any region.
// Kenyan phone numbers: must be exactly 10 digits (07XXXXXXXX or 01XXXXXXXX) when starting with 0
// Or start with 254 / +254 and still follow the same pattern
phoneRegex = `^(?:\+254|254|0)(7\d{8}|1[01]\d{7})$`
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$`
)
// IsValidPhoneNumber checks if the given number is a valid phone number
@ -23,7 +20,7 @@ func IsValidPhoneNumber(phonenumber string) bool {
// FormatPhoneNumber formats a Kenyan phone number to "+254xxxxxxxx".
func FormatPhoneNumber(phone string) (string, error) {
if !IsValidPhoneNumber(phone) {
return "", errors.New("invalid phone number")
return "", errors.New("invalid phone number")
}
// Remove any leading "+" and spaces
@ -32,43 +29,12 @@ func FormatPhoneNumber(phone string) (string, error) {
// Replace leading "0" with "254" if present
if strings.HasPrefix(phone, "0") {
phone = "254" + phone[1:]
phone = "254" + phone[1:]
}
// Add "+" if not already present
if !strings.HasPrefix(phone, "254") {
return "", errors.New("unexpected format")
return "", errors.New("unexpected format")
}
return "+" + phone, nil
}
// FormatToLocalPhoneNumber converts a phone number like "+2547XXXXXXXX"
// or "2547XXXXXXXX" into the local format "07XXXXXXXX" / "01XXXXXXXX".
func FormatToLocalPhoneNumber(phone string) (string, error) {
// Remove leading "+" and spaces
phone = strings.TrimPrefix(phone, "+")
phone = strings.ReplaceAll(phone, " ", "")
// Must start with 254
if !strings.HasPrefix(phone, "254") {
return "", errors.New("invalid international phone format")
}
// Remove "254" prefix → get 7XXXXXXXX or 1XXXXXXXX
rest := phone[3:]
// Validate: must be 9 digits
if len(rest) != 9 {
return "", errors.New("invalid phone number length")
}
// Kenyan mobile numbers start with 7 or 1 (Safaricom/Airtel/Telkom prefixes)
if !(strings.HasPrefix(rest, "7") || strings.HasPrefix(rest, "1")) {
return "", errors.New("invalid Kenyan mobile prefix")
}
// Convert to local: prepend "0"
local := "0" + rest
return local, nil
}

View File

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