Skip to content

Commit

Permalink
added validator for E164
Browse files Browse the repository at this point in the history
  • Loading branch information
baharkhd committed Jan 31, 2021
1 parent 7a23bdc commit c241566
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
hasWhitespaceOnly string = "^[[:space:]]+$"
IMEI string = "^[0-9a-f]{14}$|^\\d{15}$|^\\d{18}$"
IMSI string = "^\\d{14,15}$"
E164 string = `^\+?[1-9]\d{1,14}$`
)

// Used by IsFilePath func
Expand Down Expand Up @@ -104,4 +105,5 @@ var (
rxHasWhitespaceOnly = regexp.MustCompile(hasWhitespaceOnly)
rxIMEI = regexp.MustCompile(IMEI)
rxIMSI = regexp.MustCompile(IMSI)
rxE164 = regexp.MustCompile(E164)
)
4 changes: 4 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,3 +1625,7 @@ func (sv stringValues) Len() int { return len(sv) }
func (sv stringValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
func (sv stringValues) Less(i, j int) bool { return sv.get(i) < sv.get(j) }
func (sv stringValues) get(i int) string { return sv[i].String() }

func IsE164(str string) bool {
return rxE164.MatchString(str)
}
21 changes: 21 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3594,3 +3594,24 @@ func TestIsIMSI(t *testing.T) {
}
}
}

func TestIsE164(t *testing.T) {
t.Parallel()

var tests = []struct {
param string
expected bool
}{
{"+14155552671", true},
{"+442071838750", true},
{"+551155256325", true},
{"+226071234567 ", false},
{"+06071234567 ", false},
}
for _, test := range tests {
actual := IsE164(test.param)
if actual != test.expected {
t.Errorf("Expected IsURL(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}

0 comments on commit c241566

Please sign in to comment.