diff --git a/email.go b/email.go new file mode 100644 index 0000000..aaab237 --- /dev/null +++ b/email.go @@ -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 +} diff --git a/email_test.go b/email_test.go new file mode 100644 index 0000000..4a8fc6b --- /dev/null +++ b/email_test.go @@ -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: "rezakhdemix@gmail.com", + 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], + ) + } + } +} diff --git a/validator.go b/validator.go index f3aac23..e1cd448 100644 --- a/validator.go +++ b/validator.go @@ -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.