+ IsValidEmail

This commit is contained in:
Andreas Schulte 2024-06-05 21:32:49 +02:00
parent 98f8e21ad2
commit 0b60553c59
Signed by: andreas
GPG Key ID: E123DA7BD8F9C8AB
2 changed files with 59 additions and 26 deletions

View File

@ -3,6 +3,7 @@ package utils
import (
"net/url"
"os"
"regexp"
"github.com/google/uuid"
)
@ -58,6 +59,15 @@ func IsStringInSliceOfStrings(slice []string, target string) bool {
return false
}
// IsValidEmail returns true, if s is a email address.
func IsValidEmail(s string) bool {
// Used Regex instead of mail.ParseAddress(), as the latter incorrectly recognizes samples such as “bad@domain” as a valid address
// Shoutout to emailregex.com for the almighty regular expression
return regexp.MustCompile(
`(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])`,
).MatchString(s)
}
// IsValidURL returns true, if s is a valid URL
func IsValidURL(s string) bool {
u, err := url.Parse(s)

View File

@ -4,6 +4,42 @@ import (
"testing"
)
func TestDoesFileExist(t *testing.T) {
data := map[string]bool{
"/usr/bin/go": true,
"/etc/fstab": true,
"/etc/os-release": true,
"/usr/bin": false,
"/home/derschmierigetypvomaldi": false,
"/tmp/plainTextPasswords.txt": false,
}
for d, expectedResult := range data {
result := DoesFileExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestDoesFolderExist(t *testing.T) {
data := map[string]bool{
"/home": true,
"/tmp": true,
"/usr/bin": true,
"/home/derschmierigetypvomaldi": false,
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
"/usr/bin/go": false,
}
for d, expectedResult := range data {
result := DoesFolderExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestIsStringInSliceOfStrings(t *testing.T) {
slice1 := []string{"apple", "banana", "cherry", "date"}
target1 := "banana"
@ -76,36 +112,23 @@ func TestIsValidUUID(t *testing.T) {
}
}
func TestDoesFileExist(t *testing.T) {
func TestIsValidEmail(t *testing.T) {
data := map[string]bool{
"/usr/bin/go": true,
"/etc/fstab": true,
"/etc/os-release": true,
"/usr/bin": false,
"/home/derschmierigetypvomaldi": false,
"/tmp/plainTextPasswords.txt": false,
"test@example.com": true,
"another.test@domain.co.uk": true,
"username@domain.c": true,
"": false,
"invalid-email": false,
"bad@domain": false,
"@missingusername.com": false,
"plainaddress": false,
"@missingdomain": false,
"missingatsign.com": false,
"username@.com": false,
}
for d, expectedResult := range data {
result := DoesFileExist(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestDoesFolderExist(t *testing.T) {
data := map[string]bool{
"/home": true,
"/tmp": true,
"/usr/bin": true,
"/home/derschmierigetypvomaldi": false,
"/tmp/fdashfglkjdahjslkjfhjdsakljöhjf": false,
"/usr/bin/go": false,
}
for d, expectedResult := range data {
result := DoesFolderExist(d)
result := IsValidEmail(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}