| 1 | // Package govalidator is package of validators and sanitizers for strings, structs and collections. |
| 2 | package govalidatorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/ |
| 3 | |
| 4 | import ( |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.21 fmt/" |
| 6 | "netscip-go gomod github.com/golang/go/src go1.21 net/" |
| 7 | "net/urlscip-go gomod github.com/golang/go/src go1.21 `net/url`/" |
| 8 | "reflectscip-go gomod github.com/golang/go/src go1.21 reflect/" |
| 9 | "regexpscip-go gomod github.com/golang/go/src go1.21 regexp/" |
| 10 | "strconvscip-go gomod github.com/golang/go/src go1.21 strconv/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.21 strings/" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | notNumberRegexpscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/notNumberRegexp. = regexpscip-go gomod github.com/golang/go/src go1.21 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.21 regexp/MustCompile().("[^0-9]+") |
| 16 | whiteSpacesAndMinusscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/whiteSpacesAndMinus. = regexpscip-go gomod github.com/golang/go/src go1.21 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.21 regexp/MustCompile().(`[\s-]+`) |
| 17 | ) |
| 18 | |
| 19 | // IsRequestURI check if the string rawurl, assuming |
| 20 | // it was received in an HTTP request, is an |
| 21 | // absolute URI or an absolute path. |
| 22 | func IsRequestURIscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsRequestURI().(rawurl string) bool { |
| 23 | _, err := urlscip-go gomod github.com/golang/go/src go1.21 `net/url`/.ParseRequestURIscip-go gomod github.com/golang/go/src go1.21 `net/url`/ParseRequestURI().(rawurl) |
| 24 | return err == nil |
| 25 | } |
| 26 | |
| 27 | // IsHexcolor check if the string is a hexadecimal color. |
| 28 | func IsHexcolorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsHexcolor().(str string) bool { |
| 29 | return rxHexcolorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxHexcolor..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(str) |
| 30 | } |
| 31 | |
| 32 | // IsRGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB). |
| 33 | func IsRGBcolorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsRGBcolor().(str string) bool { |
| 34 | return rxRGBcolorscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxRGBcolor..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(str) |
| 35 | } |
| 36 | |
| 37 | // IsCreditCard check if the string is a credit card. |
| 38 | func IsCreditCardscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsCreditCard().(str string) bool { |
| 39 | sanitized := notNumberRegexpscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/notNumberRegexp..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#ReplaceAllString().(str, "") |
| 40 | if !rxCreditCardscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxCreditCard..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(sanitized) { |
| 41 | return false |
| 42 | } |
| 43 | var sum int64 |
| 44 | var digit string |
| 45 | var tmpNum int64 |
| 46 | var shouldDouble bool |
| 47 | for i := len(sanitized) - 1; i >= 0; i-- { |
| 48 | digit = sanitized[i:(i + 1)] |
| 49 | tmpNum, _ = ToIntscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/ToInt().(digit) |
| 50 | if shouldDouble { |
| 51 | tmpNum *= 2 |
| 52 | if tmpNum >= 10 { |
| 53 | sum += (tmpNum % 10) + 1 |
| 54 | } else { |
| 55 | sum += tmpNum |
| 56 | } |
| 57 | } else { |
| 58 | sum += tmpNum |
| 59 | } |
| 60 | shouldDouble = !shouldDouble |
| 61 | } |
| 62 | |
| 63 | return sum%10 == 0 |
| 64 | } |
| 65 | |
| 66 | // IsISBN10 check if the string is an ISBN version 10. |
| 67 | func IsISBN10scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN10().(str string) bool { |
| 68 | return IsISBNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN().(str, 10) |
| 69 | } |
| 70 | |
| 71 | // IsISBN13 check if the string is an ISBN version 13. |
| 72 | func IsISBN13scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN13().(str string) bool { |
| 73 | return IsISBNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN().(str, 13) |
| 74 | } |
| 75 | |
| 76 | // IsISBN check if the string is an ISBN (version 10 or 13). |
| 77 | // If version value is not equal to 10 or 13, it will be check both variants. |
| 78 | func IsISBNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN().(str string, version int) bool { |
| 79 | sanitized := whiteSpacesAndMinusscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/whiteSpacesAndMinus..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#ReplaceAllString().(str, "") |
| 80 | var checksum int32 |
| 81 | var i int32 |
| 82 | if version == 10 { |
| 83 | if !rxISBN10scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxISBN10..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(sanitized) { |
| 84 | return false |
| 85 | } |
| 86 | for i = 0; i < 9; i++ { |
| 87 | checksum += (i + 1) * int32(sanitized[i]-'0') |
| 88 | } |
| 89 | if sanitized[9] == 'X' { |
| 90 | checksum += 10 * 10 |
| 91 | } else { |
| 92 | checksum += 10 * int32(sanitized[9]-'0') |
| 93 | } |
| 94 | if checksum%11 == 0 { |
| 95 | return true |
| 96 | } |
| 97 | return false |
| 98 | } else if version == 13 { |
| 99 | if !rxISBN13scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxISBN13..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(sanitized) { |
| 100 | return false |
| 101 | } |
| 102 | factor := []int32{1, 3} |
| 103 | for i = 0; i < 12; i++ { |
| 104 | checksum += factor[i%2] * int32(sanitized[i]-'0') |
| 105 | } |
| 106 | return (int32(sanitized[12]-'0'))-((10-(checksum%10))%10) == 0 |
| 107 | } |
| 108 | return IsISBNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN().(str, 10) || IsISBNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsISBN().(str, 13) |
| 109 | } |
| 110 | |
| 111 | // IsBase64 check if a string is base64 encoded. |
| 112 | func IsBase64scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsBase64().(str string) bool { |
| 113 | return rxBase64scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxBase64..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(str) |
| 114 | } |
| 115 | |
| 116 | // IsIPv6 check if the string is an IP version 6. |
| 117 | func IsIPv6scip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsIPv6().(str string) bool { |
| 118 | ip := netscip-go gomod github.com/golang/go/src go1.21 net/.ParseIPscip-go gomod github.com/golang/go/src go1.21 net/ParseIP().(str) |
| 119 | return ip != nil && stringsscip-go gomod github.com/golang/go/src go1.21 strings/.Containsscip-go gomod github.com/golang/go/src go1.21 strings/Contains().(str, ":") |
| 120 | } |
| 121 | |
| 122 | // IsMAC check if a string is valid MAC address. |
| 123 | // Possible MAC formats: |
| 124 | // 01:23:45:67:89:ab |
| 125 | // 01:23:45:67:89:ab:cd:ef |
| 126 | // 01-23-45-67-89-ab |
| 127 | // 01-23-45-67-89-ab-cd-ef |
| 128 | // 0123.4567.89ab |
| 129 | // 0123.4567.89ab.cdef |
| 130 | func IsMACscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsMAC().(str string) bool { |
| 131 | _, err := netscip-go gomod github.com/golang/go/src go1.21 net/.ParseMACscip-go gomod github.com/golang/go/src go1.21 net/ParseMAC().(str) |
| 132 | return err == nil |
| 133 | } |
| 134 | |
| 135 | // IsSSN will validate the given string as a U.S. Social Security Number |
| 136 | func IsSSNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsSSN().(str string) bool { |
| 137 | if str == "" || len(str) != 11 { |
| 138 | return false |
| 139 | } |
| 140 | return rxSSNscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxSSN..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(str) |
| 141 | } |
| 142 | |
| 143 | // ToInt convert the input string or any int type to an integer type 64, or 0 if the input is not an integer. |
| 144 | func ToIntscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/ToInt().(value interface{}) (res int64, err error) { |
| 145 | val := reflectscip-go gomod github.com/golang/go/src go1.21 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.21 reflect/ValueOf().(value) |
| 146 | |
| 147 | switch value.(type) { |
| 148 | case int, int8, int16, int32, int64: |
| 149 | res = val.Intscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Int().() |
| 150 | case uint, uint8, uint16, uint32, uint64: |
| 151 | res = int64(val.Uintscip-go gomod github.com/golang/go/src go1.21 reflect/Value#Uint().()) |
| 152 | case string: |
| 153 | if IsIntscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsInt().(val.Stringscip-go gomod github.com/golang/go/src go1.21 reflect/Value#String().()) { |
| 154 | res, err = strconvscip-go gomod github.com/golang/go/src go1.21 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.21 strconv/ParseInt().(val.Stringscip-go gomod github.com/golang/go/src go1.21 reflect/Value#String().(), 0, 64) |
| 155 | if err != nil { |
| 156 | res = 0 |
| 157 | } |
| 158 | } else { |
| 159 | err = fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("math: square root of negative number %g", value) |
| 160 | res = 0 |
| 161 | } |
| 162 | default: |
| 163 | err = fmtscip-go gomod github.com/golang/go/src go1.21 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.21 fmt/Errorf().("math: square root of negative number %g", value) |
| 164 | res = 0 |
| 165 | } |
| 166 | |
| 167 | return |
| 168 | } |
| 169 | |
| 170 | // IsInt check if the string is an integer. Empty string is valid. |
| 171 | func IsIntscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsInt().(str string) bool { |
| 172 | if IsNullscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsNull().(str) { |
| 173 | return true |
| 174 | } |
| 175 | return rxIntscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/rxInt..MatchStringscip-go gomod github.com/golang/go/src go1.21 regexp/Regexp#MatchString().(str) |
| 176 | } |
| 177 | |
| 178 | // IsNull check if the string is null. |
| 179 | func IsNullscip-go gomod k8s.io/kube-openapi . `k8s.io/kube-openapi/pkg/internal/third_party/govalidator`/IsNull().(str string) bool { |
| 180 | return len(str) == 0 |
| 181 | } |