-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Email validation checker and tests to v2. (#134)
# Describe Request Add Email validation checker and tests to v2. # Change Type New code.
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2 | ||
|
||
import ( | ||
"net/mail" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
// nameEmail is the name of the email check. | ||
nameEmail = "email" | ||
) | ||
|
||
var ( | ||
// ErrNotEmail indicates that the given value is not a valid email address. | ||
ErrNotEmail = NewCheckError("Email") | ||
) | ||
|
||
// IsEmail checks if the value is a valid email address. | ||
func IsEmail(value string) (string, error) { | ||
_, err := mail.ParseAddress(value) | ||
if err != nil { | ||
return value, ErrNotEmail | ||
} | ||
return value, nil | ||
} | ||
|
||
// checkEmail checks if the value is a valid email address. | ||
func checkEmail(value reflect.Value) (reflect.Value, error) { | ||
_, err := IsEmail(value.Interface().(string)) | ||
return value, err | ||
} | ||
|
||
// makeEmail makes a checker function for the email checker. | ||
func makeEmail(_ string) CheckFunc[reflect.Value] { | ||
return checkEmail | ||
} |
This file contains 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,76 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func ExampleIsEmail() { | ||
_, err := v2.IsEmail("test@example.com") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func TestIsEmailInvalid(t *testing.T) { | ||
_, err := v2.IsEmail("invalid-email") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestIsEmailValid(t *testing.T) { | ||
_, err := v2.IsEmail("test@example.com") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCheckEmailNonString(t *testing.T) { | ||
defer FailIfNoPanic(t, "expected panic") | ||
|
||
type User struct { | ||
Email int `checkers:"email"` | ||
} | ||
|
||
user := &User{} | ||
|
||
v2.CheckStruct(user) | ||
} | ||
|
||
func TestCheckEmailInvalid(t *testing.T) { | ||
type User struct { | ||
Email string `checkers:"email"` | ||
} | ||
|
||
user := &User{ | ||
Email: "invalid-email", | ||
} | ||
|
||
_, ok := v2.CheckStruct(user) | ||
if ok { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestCheckEmailValid(t *testing.T) { | ||
type User struct { | ||
Email string `checkers:"email"` | ||
} | ||
|
||
user := &User{ | ||
Email: "test@example.com", | ||
} | ||
|
||
_, ok := v2.CheckStruct(user) | ||
if !ok { | ||
t.Fatal("expected valid") | ||
} | ||
} |
This file contains 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