-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: implement email tag validation #28
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce2fbd5
Implement email validation tag feature
Copilot 8c8c906
Add comprehensive end-to-end tests for email validation
Copilot e723c03
Optimize email validation with pre-compiled regex for better performance
Copilot fe56677
refactor: consolidate string tests into two test cases as requested
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| type User struct { | ||
| Email1 string `validate:"required,email"` | ||
| Email2 string `validate:"email"` | ||
| } | ||
|
|
||
| func main() { | ||
| // Test case 1: Empty required email (should fail) | ||
| u1 := &User{ | ||
| Email1: "", | ||
| Email2: "", | ||
| } | ||
| if errs := UserValidate(u1); len(errs) > 0 { | ||
| fmt.Printf("User1: %+v Errors: ", u1) | ||
| for _, err := range errs { | ||
| fmt.Printf("%s; ", err) | ||
| } | ||
| fmt.Println() | ||
| } else { | ||
| fmt.Printf("User1: %+v is valid\n", u1) | ||
| } | ||
|
|
||
| // Test case 2: Invalid required email (should fail) | ||
| u2 := &User{ | ||
| Email1: "invalid.email", | ||
| Email2: "", | ||
| } | ||
| if errs := UserValidate(u2); len(errs) > 0 { | ||
| fmt.Printf("User2: %+v Errors: ", u2) | ||
| for _, err := range errs { | ||
| fmt.Printf("%s; ", err) | ||
| } | ||
| fmt.Println() | ||
| } else { | ||
| fmt.Printf("User2: %+v is valid\n", u2) | ||
| } | ||
|
|
||
| // Test case 3: Valid required email, empty optional email (should pass) | ||
| u3 := &User{ | ||
| Email1: "valid@example.com", | ||
| Email2: "", | ||
| } | ||
| if errs := UserValidate(u3); len(errs) > 0 { | ||
| fmt.Printf("User3: %+v Errors: ", u3) | ||
| for _, err := range errs { | ||
| fmt.Printf("%s; ", err) | ||
| } | ||
| fmt.Println() | ||
| } else { | ||
| fmt.Printf("User3: %+v is valid\n", u3) | ||
| } | ||
|
|
||
| // Test case 4: Valid required email, valid optional email (should pass) | ||
| u4 := &User{ | ||
| Email1: "user@domain.com", | ||
| Email2: "optional@test.org", | ||
| } | ||
| if errs := UserValidate(u4); len(errs) > 0 { | ||
| fmt.Printf("User4: %+v Errors: ", u4) | ||
| for _, err := range errs { | ||
| fmt.Printf("%s; ", err) | ||
| } | ||
| fmt.Println() | ||
| } else { | ||
| fmt.Printf("User4: %+v is valid\n", u4) | ||
| } | ||
|
|
||
| // Test case 5: Valid required email, invalid optional email (should fail) | ||
| u5 := &User{ | ||
| Email1: "user@domain.com", | ||
| Email2: "invalid.email", | ||
| } | ||
| if errs := UserValidate(u5); len(errs) > 0 { | ||
| fmt.Printf("User5: %+v Errors: ", u5) | ||
| for _, err := range errs { | ||
| fmt.Printf("%s; ", err) | ||
| } | ||
| fmt.Println() | ||
| } else { | ||
| fmt.Printf("User5: %+v is valid\n", u5) | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,26 @@ | ||
| package types | ||
|
|
||
| import "strings" | ||
| import ( | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // emailRegex is a pre-compiled regex for email validation | ||
| // This avoids recompiling the regex on every validation call | ||
| var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) | ||
|
|
||
| func ToLower(str string) string { | ||
| return strings.ToLower(str) | ||
| } | ||
|
|
||
| // IsValidEmail validates if a string is a valid email format | ||
| // Returns true for valid email format, false otherwise | ||
| // Empty string returns true (for optional email fields) | ||
| func IsValidEmail(email string) bool { | ||
| if email == "" { | ||
| return true // Empty email is valid for optional fields | ||
| } | ||
|
|
||
| // Use pre-compiled regex for better performance | ||
| return emailRegex.MatchString(email) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package types | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestIsValidEmail(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| email string | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "empty email (valid for optional fields)", | ||
| email: "", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "valid simple email", | ||
| email: "test@example.com", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "valid email with subdomain", | ||
| email: "user@mail.example.com", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "valid email with numbers and special chars", | ||
| email: "user123+tag@example.co.uk", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "valid email with dots and underscores", | ||
| email: "first.last_name@domain.org", | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "invalid email without @", | ||
| email: "invalid.email.com", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "invalid email without domain", | ||
| email: "user@", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "invalid email without local part", | ||
| email: "@domain.com", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "invalid email without TLD", | ||
| email: "user@domain", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "invalid email with spaces", | ||
| email: "user @domain.com", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "invalid email with multiple @", | ||
| email: "user@@domain.com", | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "invalid email with short TLD", | ||
| email: "user@domain.c", | ||
| want: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := IsValidEmail(tt.email); got != tt.want { | ||
| t.Errorf("IsValidEmail(%q) = %v, want %v", tt.email, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.