Skip to content

Commit

Permalink
Add Email validation checker and tests to v2. (#134)
Browse files Browse the repository at this point in the history
# Describe Request

Add Email validation checker and tests to v2.

# Change Type

New code.
  • Loading branch information
cinar authored Dec 27, 2024
1 parent b0096cb commit c47eeb6
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
41 changes: 41 additions & 0 deletions v2/email.go
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
}
76 changes: 76 additions & 0 deletions v2/email_test.go
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")
}
}
1 change: 1 addition & 0 deletions v2/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var makers = map[string]MakeCheckFunc{
nameASCII: makeASCII,
nameCIDR: makeCIDR,
nameDigits: makeDigits,
nameEmail: makeEmail,
nameMaxLen: makeMaxLen,
nameMinLen: makeMinLen,
nameRequired: makeRequired,
Expand Down

0 comments on commit c47eeb6

Please sign in to comment.