Skip to content

Commit

Permalink
upgraded govalidator to v10 and added EmailFormat validation rule
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Apr 26, 2020
1 parent a80f7c5 commit f0cccd2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.')),
)
```

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
Expand Down
8 changes: 5 additions & 3 deletions is/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"regexp"
"unicode"

"gopkg.in/asaskevich/govalidator.v9"
"github.com/asaskevich/govalidator"
)

var (
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions is/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestAll(t *testing.T) {
err string
}{
{"Email", Email, "[email protected]", "example.com", "must be a valid email address"},
{"EmailFormat", EmailFormat, "[email protected]", "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"},
Expand Down

0 comments on commit f0cccd2

Please sign in to comment.