Skip to content

Commit

Permalink
Refactor IsURLWithHTTPSorEmptyString
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jan 30, 2023
1 parent cdfeee0 commit 11aacd7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
39 changes: 31 additions & 8 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package validation

import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"fmt"
"net/url"
)

func IsURLWithHTTPSorEmptyString(i interface{}, s string) ([]string, []error) {
_, errors := validation.IsURLWithHTTPS(i, s)
for _, err := range errors {
if !strings.Contains(err.Error(), "url to not be empty") {
return nil, errors
func IsURLWithHTTPSorEmptyString(rawURL interface{}, key string) ([]string, []error) {
urlString, ok := rawURL.(string)
if !ok {
return nil, []error{
fmt.Errorf("expected type of %q to be string", key),
}
}

if urlString == "" {
return nil, nil
}

parsedURL, err := url.Parse(urlString)
if err != nil {
return nil, []error{
fmt.Errorf("expected %q to be a valid url, got %v: %+v", key, urlString, err),
}
}

if parsedURL.Host == "" {
return nil, []error{
fmt.Errorf("expected %q to have a host, got %v", key, urlString),
}
}

if parsedURL.Scheme != "https" {
return nil, []error{
fmt.Errorf("expected %q to have a url with schema of: %q, got %v", key, "https", urlString),
}
}

return nil, nil
}
6 changes: 6 additions & 0 deletions internal/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func TestIsURLWithHTTPSorEmptyString(t *testing.T) {
inputURL: "",
expectedErrors: nil,
},
{
inputURL: "broken/url",
expectedErrors: []string{
"expected \"theTestURL\" to have a host, got broken/url",
},
},
}

for i, testCase := range testCases {
Expand Down

0 comments on commit 11aacd7

Please sign in to comment.