Skip to content

Commit f2ebb45

Browse files
authored
fix url validation (#38)
1 parent 735334c commit f2ebb45

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ var (
301301
urlSubdomainRx = `((www\.)|([a-zA-Z0-9]([-\.][-\._a-zA-Z0-9]+)*))`
302302
urlPortRx = `(:(\d{1,5}))`
303303
urlPathRx = `((\/|\?|#)[^\s]*)`
304-
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 + `?$`)
304+
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 + `?$`)
305305
)
306306

307307
// IsURL check if the string is an URL.

common_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ type (
8383
People []Person `json:"people" binding:"MinSize(1)"`
8484
}
8585

86+
UrlForm struct {
87+
Url string `form:"Url" binding:"Url"`
88+
}
89+
8690
CustomErrorHandle struct {
8791
Rule `binding:"CustomRule"`
8892
}

validate_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,26 @@ var validationTestCases = []validationTestCase{
370370
},
371371
},
372372
},
373+
{
374+
description: "invalid url Validation",
375+
data: UrlForm{
376+
Url: "192.168.0.1",
377+
},
378+
expectedErrors: Errors{
379+
Error{
380+
FieldNames: []string{"Url"},
381+
Classification: "Url",
382+
Message: "Url",
383+
},
384+
},
385+
},
386+
{
387+
description: "valid url Validation",
388+
data: UrlForm{
389+
Url: "https://192.168.0.1:8000",
390+
},
391+
expectedErrors: Errors{},
392+
},
373393
}
374394

375395
func Test_Validation(t *testing.T) {

0 commit comments

Comments
 (0)