Add email not blacklisted validator

Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
Émile Ré
2025-12-15 10:28:07 +01:00
parent 32f90ac310
commit 64d2ecee59
2 changed files with 46 additions and 1 deletions

View File

@@ -1 +1,47 @@
package validator
import (
_ "embed"
"strings"
)
var (
//go:embed data/disposable-email-domains/disposable_email_blocklist.conf
disposableEmailsRaw []byte
testEmails = []string{
"acme.com",
"acme.net",
"acme.org",
"ethereal.email",
"example.com",
"example.net",
"example.org",
"mailhog.local",
"mailslurp.com",
"test.com",
"test.net",
"test.org",
"localhost.localdomain",
}
blacklistedEmails = append(
strings.Split(strings.TrimSpace(string(disposableEmailsRaw)), "\n"),
testEmails...,
)
)
func NotBlacklisted() ValidatorFunc {
notOneOfSlice := NotOneOfSlice(blacklistedEmails)
return func(value any) *ValidationError {
err := notOneOfSlice(value)
if err == nil {
return newValidationError(
ErrorCodeInvalidEnum,
"must not be blacklisted",
)
}
return nil
}
}

View File

@@ -22,7 +22,6 @@ import (
)
var (
emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
uuidRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
domainRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`)
)