Skip to content

Commit

Permalink
fix url validation (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
willhope authored Sep 18, 2020
1 parent 735334c commit f2ebb45
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ var (
urlSubdomainRx = `((www\.)|([a-zA-Z0-9]([-\.][-\._a-zA-Z0-9]+)*))`
urlPortRx = `(:(\d{1,5}))`
urlPathRx = `((\/|\?|#)[^\s]*)`
URLPattern = regexp.MustCompile(`^` + urlSchemaRx + `?` + urlUsernameRx + `?` + `((` + urlIPRx + `|(\[` + ipRx + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-_]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + urlSubdomainRx + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))\.?` + urlPortRx + `?` + urlPathRx + `?$`)
URLPattern = regexp.MustCompile(`^` + urlSchemaRx + urlUsernameRx + `?` + `((` + urlIPRx + `|(\[` + ipRx + `\])|(([a-zA-Z0-9]([a-zA-Z0-9-_]+)?[a-zA-Z0-9]([-\.][a-zA-Z0-9]+)*)|(` + urlSubdomainRx + `?))?(([a-zA-Z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-zA-Z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-zA-Z\x{00a1}-\x{ffff}]{1,}))?))\.?` + urlPortRx + `?` + urlPathRx + `?$`)
)

// IsURL check if the string is an URL.
Expand Down
4 changes: 4 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ type (
People []Person `json:"people" binding:"MinSize(1)"`
}

UrlForm struct {
Url string `form:"Url" binding:"Url"`
}

CustomErrorHandle struct {
Rule `binding:"CustomRule"`
}
Expand Down
20 changes: 20 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,26 @@ var validationTestCases = []validationTestCase{
},
},
},
{
description: "invalid url Validation",
data: UrlForm{
Url: "192.168.0.1",
},
expectedErrors: Errors{
Error{
FieldNames: []string{"Url"},
Classification: "Url",
Message: "Url",
},
},
},
{
description: "valid url Validation",
data: UrlForm{
Url: "https://192.168.0.1:8000",
},
expectedErrors: Errors{},
},
}

func Test_Validation(t *testing.T) {
Expand Down

0 comments on commit f2ebb45

Please sign in to comment.