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

added: email validation rule #35

Merged
merged 2 commits into from
Mar 7, 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
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
Loading