Skip to content

Commit

Permalink
Merge pull request #35 from rezakhademix/add-email-validation-rule
Browse files Browse the repository at this point in the history
added: email validation rule
  • Loading branch information
rezakhademix authored Mar 7, 2024
2 parents fdc3072 + 8c27829 commit f973795
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
17 changes: 17 additions & 0 deletions email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package validator

const (
// Email represents the rule name which will be used to find the default error message.
Email = "email"
// EmailMsg is the default error message format for fields with the email validation rule.
EmailMsg = "%s is not valid"
// EmailRegex is the default pattern to validate email field by RFC 5322 rule.
EmailRegex = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])"
)

// Email checks the value of s under validation must match the EmailRegex regular expression.
func (v *Validator) Email(s, field, msg string) *Validator {
v.RegexMatches(s, EmailRegex, field, v.msg(Email, msg, field))

return v
}
78 changes: 78 additions & 0 deletions email_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package validator

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidator_Email(t *testing.T) {
tests := []struct {
name string
field string
value string
isPassed bool
message string
expectedMsg string
}{
{
name: "test an empty string value will fail email validation",
field: "email",
value: "",
isPassed: false,
message: "email is not valid",
expectedMsg: "email is not valid",
},
{
name: "test an empty space string value will fail email validation",
field: "email_address",
value: " ",
isPassed: false,
message: "email_address is not valid",
expectedMsg: "email_address is not valid",
},
{
name: "test a wrong string value will fail email validation",
field: "email",
value: "09377475856",
isPassed: false,
message: "email is not valid",
expectedMsg: "email is not valid",
},
{
name: "test a wrong string value will fail email validation",
field: "email",
value: "^$*me%$e.com",
isPassed: false,
message: "email is not valid",
expectedMsg: "email is not valid",
},
{
name: "test a correct email string value will pass validation",
field: "email",
value: "[email protected]",
isPassed: true,
message: "",
expectedMsg: "",
},
}

for _, test := range tests {
v := New()

v.Email(test.value, test.field, test.message)

assert.Equal(t, test.isPassed, v.IsPassed())

if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"assertion failed, expectedMsg: %s, validatorMsg: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
1 change: 1 addition & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
Between: BetweenMsg,
NotExists: NotExistsMsg,
Regex: RegexMsg,
Email: EmailMsg,
}

// ErrMethodMessageNotFound is the default message when a method does not have any error message on methodToErrorMessage.
Expand Down

0 comments on commit f973795

Please sign in to comment.