Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
dcfa4ffe
Commit
dcfa4ffe
authored
1 year ago
by
Francé Wilke
Browse files
Options
Downloads
Patches
Plain Diff
Improve email address validation
parent
0519da16
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
string_utils/string_utils.go
+44
-1
44 additions, 1 deletion
string_utils/string_utils.go
utils/utils.go
+56
-11
56 additions, 11 deletions
utils/utils.go
with
100 additions
and
12 deletions
string_utils/string_utils.go
+
44
−
1
View file @
dcfa4ffe
...
...
@@ -26,6 +26,45 @@ const (
regexIndexSubmatchEnd
=
3
)
var
WhitespaceChars
=
[]
string
{
// Standard whitespace characters
"
\u0009
"
,
// Character tabulation
"
\u000A
"
,
// Line feed
"
\u000B
"
,
// Line tabulation
"
\u000C
"
,
// Form feed
"
\u000D
"
,
// Carriage return
"
\u0020
"
,
// Space
"
\u0085
"
,
// Next line
"
\u00A0
"
,
// No-break space
"
\u1680
"
,
// Ogham space mark
"
\u2000
"
,
// En quad
"
\u2001
"
,
// Em quad
"
\u2002
"
,
// En space
"
\u2003
"
,
// Em space
"
\u2004
"
,
// Three-per-em space
"
\u2005
"
,
// Four-per-em space
"
\u2006
"
,
// Six-per-em space
"
\u2007
"
,
// Figure space
"
\u2008
"
,
// Punctuation space
"
\u2009
"
,
// Thin space
"
\u200A
"
,
// Hair space
"
\u2028
"
,
// Line separator
"
\u2029
"
,
// Paragraph separator
"
\u202F
"
,
// Narrow no-break space
"
\u205F
"
,
// Medium mathematical space
"
\u3000
"
,
// Ideographic space
}
var
NonOfficialWhitespaceChars
=
[]
string
{
// Characters with property White_Space=no
"
\u180E
"
,
// Mongolian vowel separator
"
\u200B
"
,
// Zero width space
"
\u200C
"
,
// Zero width non-joiner
"
\u200D
"
,
// Zero width joiner
"
\u2060
"
,
// Word joiner
"
\uFEFF
"
,
// Zero width no-break space
}
var
snakeCaseRegex
=
regexp
.
MustCompile
(
"^"
+
snakeCasePattern
+
"$"
)
func
IsSnakeCase
(
name
string
)
bool
{
...
...
@@ -49,7 +88,11 @@ func ReplaceNonSpacingMarks(str string) string {
}
func
RemoveAllWhiteSpaces
(
s
string
)
string
{
return
strings
.
ReplaceAll
(
strings
.
ReplaceAll
(
s
,
" "
,
""
),
"
\t
"
,
""
)
cleanedString
:=
strings
.
ReplaceAll
(
strings
.
ReplaceAll
(
s
,
" "
,
""
),
"
\t
"
,
""
)
for
_
,
whitespaceChar
:=
range
WhitespaceChars
{
cleanedString
=
strings
.
ReplaceAll
(
cleanedString
,
whitespaceChar
,
""
)
}
return
cleanedString
}
func
ReplaceCaseInsensitive
(
string
,
toReplace
,
replaceWith
string
)
string
{
...
...
This diff is collapsed.
Click to expand it.
utils/utils.go
+
56
−
11
View file @
dcfa4ffe
...
...
@@ -2,10 +2,10 @@ package utils
import
(
"bytes"
emailverifier
"github.com/AfterShip/email-verifier"
"github.com/mohae/deepcopy"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/string_utils"
"net/mail"
"net/url"
"os"
"reflect"
...
...
@@ -55,23 +55,68 @@ func PointerToValue[V any](value *V) V {
}
func
ValidateEmailAddress
(
email
string
)
(
string
,
error
)
{
if
email
==
""
{
// To lower
cleanedEmail
:=
strings
.
ToLower
(
strings
.
TrimSpace
(
email
))
// Remove all whitespaces
cleanedEmail
=
string_utils
.
RemoveAllWhiteSpaces
(
cleanedEmail
)
// Also remove unofficial whitespaces
for
_
,
char
:=
range
string_utils
.
NonOfficialWhitespaceChars
{
cleanedEmail
=
strings
.
ReplaceAll
(
cleanedEmail
,
char
,
""
)
}
// Strip invalid characters
cleanedEmail
=
stripInvalidCharacters
(
cleanedEmail
)
// Make sure the email is not empty
if
cleanedEmail
==
""
{
return
""
,
errors
.
Error
(
"email address is empty"
)
}
cleanEmail
:=
strings
.
ToLower
(
strings
.
TrimSpace
(
email
))
cleanEmail
=
string_utils
.
RemoveAllWhiteSpaces
(
cleanEmail
)
// Parse and verify the email
verifier
:=
emailverifier
.
NewVerifier
()
result
,
err
:=
verifier
.
Verify
(
cleanedEmail
)
if
err
!=
nil
||
!
result
.
Syntax
.
Valid
{
return
cleanedEmail
,
errors
.
Wrap
(
err
,
"could not parse email address"
)
}
return
cleanedEmail
,
nil
}
// Remove ZWSP ("\u200B") characters with an empty string to remove it
cleanEmail
=
strings
.
ReplaceAll
(
cleanEmail
,
"
\u200B
"
,
""
)
func
stripInvalidCharacters
(
email
string
)
string
{
cleanEmail
:
=
email
// We validate it but still return it since in some cases we don't want to break everything if the email is bad
_
,
err
:=
mail
.
ParseAddress
(
cleanEmail
)
if
err
!=
nil
{
return
cleanEmail
,
errors
.
Wrap
(
err
,
"could not parse email address"
)
// Replace quotes, asterisks, etc.
cleanEmail
=
strings
.
ReplaceAll
(
cleanEmail
,
"'"
,
""
)
cleanEmail
=
strings
.
ReplaceAll
(
cleanEmail
,
"*"
,
""
)
cleanEmail
=
strings
.
ReplaceAll
(
cleanEmail
,
"!"
,
""
)
cleanEmail
=
strings
.
ReplaceAll
(
cleanEmail
,
"+"
,
""
)
// Trim invalid characters, like underscore, so that it still fails if it's inside the email
cleanEmail
=
strings
.
Trim
(
cleanEmail
,
"_"
)
return
cleanEmail
}
func
SplitAndCleanEmailAddresses
(
emails
string
)
[]
string
{
var
destinationEmails
[]
string
splitEmails
:=
string_utils
.
SplitString
(
emails
,
[]
rune
{
','
,
';'
})
if
len
(
splitEmails
)
>=
1
{
// Success - return these emails
for
_
,
email
:=
range
splitEmails
{
cleanedEmail
,
err
:=
ValidateEmailAddress
(
email
)
if
err
==
nil
&&
cleanedEmail
!=
""
{
destinationEmails
=
append
(
destinationEmails
,
cleanedEmail
)
}
}
if
len
(
destinationEmails
)
>
0
{
return
destinationEmails
}
}
return
clea
nEmail
,
nil
return
destinatio
nEmail
s
}
func
StripEmail
(
email
string
)
(
strippedEmail
string
,
strippedDomain
string
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment