diff --git a/email.go b/email.go index 1d1fb53..3856599 100644 --- a/email.go +++ b/email.go @@ -30,8 +30,44 @@ func IsEmail(email string) Result { return ResultNotEmail } - user := email[:atIndex] + return isValidEmailUser(email[:atIndex]) +} + +// makeEmail makes a checker function for the email checker. +func makeEmail(_ string) CheckFunc { + return checkEmail +} + +// checkEmail checks if the given string is an email address. +func checkEmail(value, _ reflect.Value) Result { + if value.Kind() != reflect.String { + panic("string expected") + } + + return IsEmail(value.String()) +} + +// isValidEmailDomain checks if the email domain is a IPv4 or IPv6 address, or a FQDN. +func isValidEmailDomain(domain string) Result { + if len(domain) > 255 { + return ResultNotEmail + } + + if domain[0] == '[' { + if strings.HasPrefix(domain, ipV6Prefix) { + // postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334] + return IsIPV6(domain[len(ipV6Prefix) : len(domain)-1]) + } + + // postmaster@[123.123.123.123] + return IsIPV4(domain[1 : len(domain)-1]) + } + + return IsFqdn(domain) +} +// isValidEmailUser checks if the email user is valid. +func isValidEmailUser(user string) Result { // Cannot be empty user if user == "" || len(user) > 64 { return ResultNotEmail @@ -79,36 +115,3 @@ func IsEmail(email string) Result { return ResultValid } - -// makeEmail makes a checker function for the email checker. -func makeEmail(_ string) CheckFunc { - return checkEmail -} - -// checkEmail checks if the given string is an email address. -func checkEmail(value, _ reflect.Value) Result { - if value.Kind() != reflect.String { - panic("string expected") - } - - return IsEmail(value.String()) -} - -// isValidEmailDomain checks if the email domain is a IPv4 or IPv6 address, or a FQDN. -func isValidEmailDomain(domain string) Result { - if len(domain) > 255 { - return ResultNotEmail - } - - if domain[0] == '[' { - if strings.HasPrefix(domain, ipV6Prefix) { - // postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334] - return IsIPV6(domain[len(ipV6Prefix) : len(domain)-1]) - } - - // postmaster@[123.123.123.123] - return IsIPV4(domain[1 : len(domain)-1]) - } - - return IsFqdn(domain) -}