From 3feb20755f9df04f30f7be47546066a6d4c3e3db Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 14 Jan 2020 13:29:59 +0300 Subject: [PATCH 1/3] patterns: add IMEI regexp --- patterns.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/patterns.go b/patterns.go index 1cf9726..e55451c 100644 --- a/patterns.go +++ b/patterns.go @@ -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 @@ -100,4 +101,5 @@ var ( rxHasUpperCase = regexp.MustCompile(hasUpperCase) rxHasWhitespace = regexp.MustCompile(hasWhitespace) rxHasWhitespaceOnly = regexp.MustCompile(hasWhitespaceOnly) + rxIMEI = regexp.MustCompile(IMEI) ) From fa93e506497e4c99061a6a2db118a295d97ae9e0 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 14 Jan 2020 13:30:52 +0300 Subject: [PATCH 2/3] validator_test: add test case for IsIMEI --- validator_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/validator_test.go b/validator_test.go index 47743c9..618e6c2 100644 --- a/validator_test.go +++ b/validator_test.go @@ -4239,3 +4239,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) + } + } +} From 980a5a4d8020dfacbbe689c1d99c6e59039d3e12 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 14 Jan 2020 13:34:31 +0300 Subject: [PATCH 3/3] validator: add IsIMEI --- types.go | 1 + validator.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/types.go b/types.go index f42a346..b57b666 100644 --- a/types.go +++ b/types.go @@ -162,6 +162,7 @@ var TagMap = map[string]Validator{ "ISO3166Alpha2": IsISO3166Alpha2, "ISO3166Alpha3": IsISO3166Alpha3, "ISO4217": IsISO4217, + "IMEI": IsIMEI, } // ISO3166Entry stores country codes diff --git a/validator.go b/validator.go index 14682e0..1327fcd 100644 --- a/validator.go +++ b/validator.go @@ -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)