github.com/profzone/eden-framework@v1.0.10/pkg/validate/validatetpl/url.go (about) 1 package validatetpl 2 3 import ( 4 "net/url" 5 "strings" 6 ) 7 8 const ( 9 HTTP_URL_TYPE_ERROR = "url类型错误" 10 HTTP_URL_VALUE_ERROR = "非法的http url" 11 HTTP_URL_SCHEME_ERROR = "scheme错误" 12 HTTP_URL_TOO_LONG = "url超过了256个字节" 13 ) 14 15 const ( 16 MAX_URL_LEN = 256 17 ) 18 19 func ValidateHttpUrl(v interface{}) (bool, string) { 20 s, ok := v.(string) 21 if !ok { 22 return false, HTTP_URL_TYPE_ERROR 23 } 24 25 if len(s) > MAX_URL_LEN { 26 return false, HTTP_URL_TOO_LONG 27 } 28 29 if u, err := url.Parse(s); err != nil { 30 return false, HTTP_URL_VALUE_ERROR 31 } else { 32 scheme := strings.ToLower(u.Scheme) 33 if scheme != "http" && scheme != "https" { 34 return false, HTTP_URL_SCHEME_ERROR 35 } 36 } 37 return true, "" 38 } 39 40 func ValidateHttpUrlOrEmpty(v interface{}) (bool, string) { 41 s, ok := v.(string) 42 if !ok { 43 return false, HTTP_URL_TYPE_ERROR 44 } 45 46 if s == "" { 47 return true, "" 48 } 49 50 return ValidateHttpUrl(v) 51 }