-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from rezakhademix/add-email-validation-rule
added: email validation rule
- Loading branch information
Showing
3 changed files
with
96 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,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 | ||
} |
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,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], | ||
) | ||
} | ||
} | ||
} |
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