Skip to content

Commit

Permalink
Merge pull request #362 from sschiz/add-is-imei
Browse files Browse the repository at this point in the history
Add IsIMEI validation function
  • Loading branch information
asaskevich committed Apr 28, 2020
2 parents 4a7f9ba + 980a5a4 commit 21a406d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
hasUpperCase string = ".*[[:upper:]]"
hasWhitespace string = ".*[[:space:]]"
hasWhitespaceOnly string = "^[[:space:]]+$"
IMEI string = "^[0-9a-f]{14}$|^\\d{15}$|^\\d{18}$"
)

// Used by IsFilePath func
Expand Down Expand Up @@ -100,4 +101,5 @@ var (
rxHasUpperCase = regexp.MustCompile(hasUpperCase)
rxHasWhitespace = regexp.MustCompile(hasWhitespace)
rxHasWhitespaceOnly = regexp.MustCompile(hasWhitespaceOnly)
rxIMEI = regexp.MustCompile(IMEI)
)
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var TagMap = map[string]Validator{
"ISO3166Alpha2": IsISO3166Alpha2,
"ISO3166Alpha3": IsISO3166Alpha3,
"ISO4217": IsISO4217,
"IMEI": IsIMEI,
}

// ISO3166Entry stores country codes
Expand Down
5 changes: 5 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,11 @@ func IsLongitude(str string) bool {
return rxLongitude.MatchString(str)
}

// IsIMEI check if a string is valid IMEI
func IsIMEI(str string) bool {
return rxIMEI.MatchString(str)
}

// IsRsaPublicKey check if a string is valid public key with provided length
func IsRsaPublicKey(str string, keylen int) bool {
bb := bytes.NewBufferString(str)
Expand Down
18 changes: 18 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4246,3 +4246,21 @@ func TestIsType(t *testing.T) {
}
}
}

func TestIsIMEI(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"990000862471854", true},
{"351756051523999", true},
{"9900008624718541", false},
{"1", false},
}
for _, test := range tests {
actual := IsIMEI(test.param)
if actual != test.expected {
t.Errorf("Expected IsIMEI(%q) to be %v, got %v", test.param, test.expected, actual)
}
}
}

0 comments on commit 21a406d

Please sign in to comment.