github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/web.go (about) 1 package validation 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 "github.com/hashicorp/terraform-plugin-sdk/helper/schema" 9 ) 10 11 // IsURLWithHTTPS is a SchemaValidateFunc which tests if the provided value is of type string and a valid HTTPS URL 12 func IsURLWithHTTPS(i interface{}, k string) (_ []string, errors []error) { 13 return IsURLWithScheme([]string{"https"})(i, k) 14 } 15 16 // IsURLWithHTTPorHTTPS is a SchemaValidateFunc which tests if the provided value is of type string and a valid HTTP or HTTPS URL 17 func IsURLWithHTTPorHTTPS(i interface{}, k string) (_ []string, errors []error) { 18 return IsURLWithScheme([]string{"http", "https"})(i, k) 19 } 20 21 // IsURLWithScheme is a SchemaValidateFunc which tests if the provided value is of type string and a valid URL with the provided schemas 22 func IsURLWithScheme(validSchemes []string) schema.SchemaValidateFunc { 23 return func(i interface{}, k string) (_ []string, errors []error) { 24 v, ok := i.(string) 25 if !ok { 26 errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) 27 return 28 } 29 30 if v == "" { 31 errors = append(errors, fmt.Errorf("expected %q url to not be empty, got %v", k, i)) 32 return 33 } 34 35 u, err := url.Parse(v) 36 if err != nil { 37 errors = append(errors, fmt.Errorf("expected %q to be a valid url, got %v: %+v", k, v, err)) 38 return 39 } 40 41 if u.Host == "" { 42 errors = append(errors, fmt.Errorf("expected %q to have a host, got %v", k, v)) 43 return 44 } 45 46 for _, s := range validSchemes { 47 if u.Scheme == s { 48 return //last check so just return 49 } 50 } 51 52 errors = append(errors, fmt.Errorf("expected %q to have a url with schema of: %q, got %v", k, strings.Join(validSchemes, ","), v)) 53 return 54 } 55 }