diff --git a/README.md b/README.md index 307b46e..3f2085b 100644 --- a/README.md +++ b/README.md @@ -422,7 +422,7 @@ The following code implements the aforementioned examples: result := validation.ValidateStruct(&a, validation.Field(&a.Unit, validation.When(a.Quantity != "", validation.Required).Else(validation.Nil)), validation.Field(&a.Phone, validation.When(a.Email == "", validation.Required.Error('Either phone or Email is required.')), - validation.Field(&a.Email, validation.When(a.Phone == "", validation.Required.Error('Either phone or Email is required.')), + validation.Field(&a.Email, validation.When(a.Phone == "", validation.Required.Error('Either phone or Email is required.')), ) ``` @@ -470,7 +470,8 @@ of a value satisfies certain requirements. Note that these rules only handle str or byte slice is empty, it is considered valid. You may use a `Required` rule to ensure a value is not empty. Below is the whole list of the rules provided by the `is` package: -* `Email`: validates if a string is an email or not +* `Email`: validates if a string is an email or not. It also checks if the MX record exists for the email domain. +* `EmailFormat`: validates if a string is an email or not. It does NOT check the existence of the MX record. * `URL`: validates if a string is a valid URL * `RequestURL`: validates if a string is a valid request URL * `RequestURI`: validates if a string is a valid request URI diff --git a/go.mod b/go.mod index 5a9665f..359af94 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/go-ozzo/ozzo-validation/v4 go 1.13 require ( + github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 github.com/stretchr/testify v1.4.0 - gopkg.in/asaskevich/govalidator.v9 v9.0.0-20180315120708-ccb8e960c48f ) diff --git a/go.sum b/go.sum index b50608e..7e52201 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0= +github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -6,8 +8,6 @@ github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -gopkg.in/asaskevich/govalidator.v9 v9.0.0-20180315120708-ccb8e960c48f h1:RVvpqSdNKxt6sENjmw0kdyyv8r18TdpmYTrvUUg2qkc= -gopkg.in/asaskevich/govalidator.v9 v9.0.0-20180315120708-ccb8e960c48f/go.mod h1:+MTrBL6wlsxv1uFXT6b9LWG7PJdrvUJEjl8tXOlk9OU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/is/rules.go b/is/rules.go index f05ea9e..6d7a751 100644 --- a/is/rules.go +++ b/is/rules.go @@ -10,7 +10,7 @@ import ( "regexp" "unicode" - "gopkg.in/asaskevich/govalidator.v9" + "github.com/asaskevich/govalidator" ) var ( @@ -125,8 +125,10 @@ var ( ) var ( - // Email validates if a string is an email or not. - Email = validation.NewStringRuleWithError(govalidator.IsEmail, ErrEmail) + // Email validates if a string is an email or not. It also checks if the MX record exists for the email domain. + Email = validation.NewStringRuleWithError(govalidator.IsExistingEmail, ErrEmail) + // EmailFormat validates if a string is an email or not. Note that it does NOT check if the MX record exists or not. + EmailFormat = validation.NewStringRuleWithError(govalidator.IsEmail, ErrEmail) // URL validates if a string is a valid URL URL = validation.NewStringRuleWithError(govalidator.IsURL, ErrURL) // RequestURL validates if a string is a valid request URL diff --git a/is/rules_test.go b/is/rules_test.go index 42f4cac..e7b9ba6 100644 --- a/is/rules_test.go +++ b/is/rules_test.go @@ -20,6 +20,7 @@ func TestAll(t *testing.T) { err string }{ {"Email", Email, "test@example.com", "example.com", "must be a valid email address"}, + {"EmailFormat", EmailFormat, "test@example.com", "example.com", "must be a valid email address"}, {"URL", URL, "http://example.com", "examplecom", "must be a valid URL"}, {"RequestURL", RequestURL, "http://example.com", "examplecom", "must be a valid request URL"}, {"RequestURI", RequestURI, "http://example.com", "examplecom", "must be a valid request URI"},