@@ -168,6 +168,9 @@ var (
168168 "url_encoded" : isURLEncoded ,
169169 "dir" : isDir ,
170170 "json" : isJSON ,
171+ "hostname_port" : isHostnamePort ,
172+ "lowercase" : isLowercase ,
173+ "uppercase" : isUppercase ,
171174 }
172175)
173176
@@ -2017,6 +2020,52 @@ func isJSON(fl FieldLevel) bool {
20172020 if field .Kind () == reflect .String {
20182021 val := field .String ()
20192022 return json .Valid ([]byte (val ))
2023+ }
2024+ panic (fmt .Sprintf ("Bad field type %T" , field .Interface ()))
2025+ }
2026+
2027+ // isHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
2028+ func isHostnamePort (fl FieldLevel ) bool {
2029+ val := fl .Field ().String ()
2030+ host , port , err := net .SplitHostPort (val )
2031+ if err != nil {
2032+ return false
2033+ }
2034+ // Port must be a iny <= 65535.
2035+ if portNum , err := strconv .ParseInt (port , 10 , 32 ); err != nil || portNum > 65535 || portNum < 1 {
2036+ return false
2037+ }
2038+
2039+ // If host is specified, it should match a DNS name
2040+ if host != "" {
2041+ return hostnameRegexRFC1123 .MatchString (host )
2042+ }
2043+ return true
2044+ }
2045+
2046+ // isLowercase is the validation function for validating if the current field's value is a lowercase string.
2047+ func isLowercase (fl FieldLevel ) bool {
2048+ field := fl .Field ()
2049+
2050+ if field .Kind () == reflect .String {
2051+ if field .String () == "" {
2052+ return false
2053+ }
2054+ return field .String () == strings .ToLower (field .String ())
2055+ }
2056+
2057+ panic (fmt .Sprintf ("Bad field type %T" , field .Interface ()))
2058+ }
2059+
2060+ // isUppercase is the validation function for validating if the current field's value is an uppercase string.
2061+ func isUppercase (fl FieldLevel ) bool {
2062+ field := fl .Field ()
2063+
2064+ if field .Kind () == reflect .String {
2065+ if field .String () == "" {
2066+ return false
2067+ }
2068+ return field .String () == strings .ToUpper (field .String ())
20202069 }
20212070
20222071 panic (fmt .Sprintf ("Bad field type %T" , field .Interface ()))
0 commit comments