From ba1301d7c205f70be64339106f7c7f057a70017b Mon Sep 17 00:00:00 2001 From: Reza Khademi Date: Thu, 7 Mar 2024 15:00:02 +0330 Subject: [PATCH 1/2] added: email validation rule --- email.go | 17 +++++++++++ email_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ validator.go | 1 + 3 files changed, 102 insertions(+) create mode 100644 email.go create mode 100644 email_test.go 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..59efb2f --- /dev/null +++ b/email_test.go @@ -0,0 +1,84 @@ +package validator + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidator_Email(t *testing.T) { + tests := []struct { + name string + field string + value string + pattern string + isPassed bool + message string + expectedMsg string + }{ + { + name: "test an empty string value will fail email validation", + field: "email", + value: "", + pattern: EmailRegex, + 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: " ", + pattern: EmailRegex, + 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", + pattern: EmailRegex, + 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", + pattern: EmailRegex, + 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", + pattern: EmailRegex, + isPassed: true, + message: "", + expectedMsg: "", + }, + } + + for _, test := range tests { + v := New() + + v.RegexMatches(test.value, test.pattern, 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. From 8c27829c390d9b476133871f3aa8aec539348fa9 Mon Sep 17 00:00:00 2001 From: Reza Khademi Date: Thu, 7 Mar 2024 15:09:22 +0330 Subject: [PATCH 2/2] fixed: email function tests --- email_test.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/email_test.go b/email_test.go index 59efb2f..4a8fc6b 100644 --- a/email_test.go +++ b/email_test.go @@ -11,7 +11,6 @@ func TestValidator_Email(t *testing.T) { name string field string value string - pattern string isPassed bool message string expectedMsg string @@ -20,7 +19,6 @@ func TestValidator_Email(t *testing.T) { name: "test an empty string value will fail email validation", field: "email", value: "", - pattern: EmailRegex, isPassed: false, message: "email is not valid", expectedMsg: "email is not valid", @@ -29,7 +27,6 @@ func TestValidator_Email(t *testing.T) { name: "test an empty space string value will fail email validation", field: "email_address", value: " ", - pattern: EmailRegex, isPassed: false, message: "email_address is not valid", expectedMsg: "email_address is not valid", @@ -38,7 +35,6 @@ func TestValidator_Email(t *testing.T) { name: "test a wrong string value will fail email validation", field: "email", value: "09377475856", - pattern: EmailRegex, isPassed: false, message: "email is not valid", expectedMsg: "email is not valid", @@ -47,7 +43,6 @@ func TestValidator_Email(t *testing.T) { name: "test a wrong string value will fail email validation", field: "email", value: "^$*me%$e.com", - pattern: EmailRegex, isPassed: false, message: "email is not valid", expectedMsg: "email is not valid", @@ -56,7 +51,6 @@ func TestValidator_Email(t *testing.T) { name: "test a correct email string value will pass validation", field: "email", value: "rezakhdemix@gmail.com", - pattern: EmailRegex, isPassed: true, message: "", expectedMsg: "", @@ -66,7 +60,7 @@ func TestValidator_Email(t *testing.T) { for _, test := range tests { v := New() - v.RegexMatches(test.value, test.pattern, test.field, test.message) + v.Email(test.value, test.field, test.message) assert.Equal(t, test.isPassed, v.IsPassed())