Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor ValidateEmail method interface #22

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bulkvalidate/bulkvalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func RunBulkValidation(inputFilePath, outputFilePath string) {
if err != nil {
log.Printf("Error: %s %s", email, err.Error())
}
emailResults, err := mailvalidate.ValidateEmail(request, syntaxResults)
emailResults, err := mailvalidate.ValidateEmail(request)
if err != nil {
log.Printf("Error: %s %s", email, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func VerifyEmail(email string) {
request := run.BuildRequest(email)
syntaxResults := VerifySyntax(email, false)
domainResults := VerifyDomain(syntaxResults.Domain, false)
emailResults, err := mailvalidate.ValidateEmail(request, syntaxResults)
emailResults, err := mailvalidate.ValidateEmail(request)
if err != nil {
fmt.Println(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/syntax/syntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func TestIsValidEmailSyntax(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, _ := IsValidEmailSyntax(tt.email); got != tt.want {
t.Errorf("IsValidEmailSyntax(%q) = %v, want %v", tt.email, got, tt.want)
if isValid, _ := IsValidEmailSyntax(tt.email); isValid != tt.want {
t.Errorf("IsValidEmailSyntax(%q) = %v, want %v", tt.email, isValid, tt.want)
}
})
}
Expand Down
25 changes: 13 additions & 12 deletions mailvalidate/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ type SyntaxValidation struct {
}

func ValidateEmailSyntax(email string) SyntaxValidation {
var results SyntaxValidation
ok, cleanEmail := syntax.IsValidEmailSyntax(email)
if !ok {
return results
isValid, cleanEmail := syntax.IsValidEmailSyntax(email)
if !isValid {
return SyntaxValidation{}
}

user, domain, ok := syntax.GetEmailUserAndDomain(cleanEmail)
if !ok {
return results
return SyntaxValidation{}
}
return SyntaxValidation{
IsValid: true,
User: user,
Domain: domain,
}
results.IsValid = true
results.User = user
results.Domain = domain
return results
}

func ValidateDomain(validationRequest EmailValidationRequest, validateCatchAll bool) (DomainValidation, error) {
Expand Down Expand Up @@ -121,12 +121,13 @@ func ValidateDomainWithCustomKnownProviders(validationRequest EmailValidationReq
return results, nil
}

func ValidateEmail(validationRequest EmailValidationRequest, emailSyntaxResults SyntaxValidation) (EmailValidation, error) {
func ValidateEmail(validationRequest EmailValidationRequest) (EmailValidation, error) {
var results EmailValidation
if err := validateRequest(&validationRequest); err != nil {
return results, errors.Wrap(err, "Invalid request")
}
if !emailSyntaxResults.IsValid {
emailSyntaxResult := ValidateEmailSyntax(validationRequest.Email)
if !emailSyntaxResult.IsValid {
return results, fmt.Errorf("Invalid email address")
}

Expand All @@ -140,7 +141,7 @@ func ValidateEmail(validationRequest EmailValidationRequest, emailSyntaxResults
log.Fatal(err)
}

email := fmt.Sprintf("%s@%s", emailSyntaxResults.User, emailSyntaxResults.Domain)
email := fmt.Sprintf("%s@%s", emailSyntaxResult.User, emailSyntaxResult.Domain)

isFreeEmail, err := IsFreeEmailCheck(email, freeEmails)
if err != nil {
Expand Down
Loading