Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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