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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| unix_addr | Unix domain socket end point Address |
| uri | URI String |
| url | URL String |
| http_url | HTTP URL String |
| http_url | HTTP(s) URL String |
| https_url | HTTPS-only URL String |
| url_encoded | URL Encoded |
| urn_rfc2141 | Urn RFC 2141 String |

Expand Down
24 changes: 24 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var (
"email": isEmail,
"url": isURL,
"http_url": isHttpURL,
"https_url": isHttpsURL,
"uri": isURI,
"urn_rfc2141": isUrnRFC2141, // RFC 2141
"file": isFile,
Expand Down Expand Up @@ -1513,6 +1514,29 @@ func isHttpURL(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %s", field.Type()))
}

// isHttpsURL is the validation function for validating if the current field's value is a valid HTTPS-only URL.
func isHttpsURL(fl FieldLevel) bool {
if !isURL(fl) {
return false
}

field := fl.Field()
switch field.Kind() {
case reflect.String:

s := strings.ToLower(field.String())

url, err := url.Parse(s)
if err != nil || url.Host == "" {
return false
}

return url.Scheme == "https"
}

panic(fmt.Sprintf("Bad field type %s", field.Type()))
}

// isUrnRFC2141 is the validation function for validating if the current field's value is a valid URN as per RFC 2141.
func isUrnRFC2141(fl FieldLevel) bool {
field := fl.Field()
Expand Down
74 changes: 74 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8360,6 +8360,80 @@ func TestHttpUrl(t *testing.T) {
PanicMatches(t, func() { _ = validate.Var(i, "http_url") }, "Bad field type int")
}

func TestHttpsUrl(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"https://foo.bar#com", true},
{"https://foobar.com", true},
{"HTTPS://foobar.com", true},
{"http://foobar.com", false},
{"foobar.com", false},
{"https://foobar.coffee/", true},
{"https://foobar.中文网/", true},
{"https://foobar.org/", true},
{"https://foobar.org:8080/", true},
{"ftp://foobar.ru/", false},
{"file:///etc/passwd", false},
{"file://C:/windows/win.ini", false},
{"https://user:pass@www.foobar.com/", true},
{"https://127.0.0.1/", true},
{"https://duckduckgo.com/?q=%2F", true},
{"https://localhost:3000/", true},
{"https://foobar.com/?foo=bar#baz=qux", true},
{"https://foobar.com?foo=bar", true},
{"https://www.xn--froschgrn-x9a.net/", true},
{"", false},
{"a://b", false},
{"xyz://foobar.com", false},
{"invalid.", false},
{".com", false},
{"rtmp://foobar.com", false},
{"https://www.foo_bar.com/", true},
{"https://localhost:3000/", true},
{"https://foobar.com/#baz", true},
{"https://foobar.com#baz=qux", true},
{"https://foobar.com/t$-_.+!*\\'(),", true},
{"https://www.foobar.com/~foobar", true},
{"https://www.-foobar.com/", true},
{"https://www.foo---bar.com/", true},
{"mailto:someone@example.com", false},
{"irc://irc.server.org/channel", false},
{"irc://#channel@network", false},
{"/abs/test/dir", false},
{"./rel/test/dir", false},
{"https:", false},
{"https://", false},
{"https://#invalid", false},
{"http://1.1.1.1", false},
}

validate := New()

for i, test := range tests {
errs := validate.Var(test.param, "https_url")

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d HTTPS URL failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d HTTPS URL failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "https_url" {
t.Fatalf("Index: %d HTTPS URL failed Error: %s", i, errs)
}
}
}
}

i := 1
PanicMatches(t, func() { _ = validate.Var(i, "https_url") }, "Bad field type int")
}

func TestUri(t *testing.T) {
tests := []struct {
param string
Expand Down
Loading