|
| 1 | +// Package govalidator is package of validators and sanitizers for strings, structs and collections. |
| 2 | +package govalidator |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/url" |
| 8 | + "reflect" |
| 9 | + "regexp" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + notNumberRegexp = regexp.MustCompile("[^0-9]+") |
| 16 | + whiteSpacesAndMinus = 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 IsRequestURI(rawurl string) bool { |
| 23 | + _, err := url.ParseRequestURI(rawurl) |
| 24 | + return err == nil |
| 25 | +} |
| 26 | + |
| 27 | +// IsHexcolor check if the string is a hexadecimal color. |
| 28 | +func IsHexcolor(str string) bool { |
| 29 | + return rxHexcolor.MatchString(str) |
| 30 | +} |
| 31 | + |
| 32 | +// IsRGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB). |
| 33 | +func IsRGBcolor(str string) bool { |
| 34 | + return rxRGBcolor.MatchString(str) |
| 35 | +} |
| 36 | + |
| 37 | +// IsCreditCard check if the string is a credit card. |
| 38 | +func IsCreditCard(str string) bool { |
| 39 | + sanitized := notNumberRegexp.ReplaceAllString(str, "") |
| 40 | + if !rxCreditCard.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, _ = 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 IsISBN10(str string) bool { |
| 68 | + return IsISBN(str, 10) |
| 69 | +} |
| 70 | + |
| 71 | +// IsISBN13 check if the string is an ISBN version 13. |
| 72 | +func IsISBN13(str string) bool { |
| 73 | + return 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 IsISBN(str string, version int) bool { |
| 79 | + sanitized := whiteSpacesAndMinus.ReplaceAllString(str, "") |
| 80 | + var checksum int32 |
| 81 | + var i int32 |
| 82 | + if version == 10 { |
| 83 | + if !rxISBN10.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 !rxISBN13.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 IsISBN(str, 10) || IsISBN(str, 13) |
| 109 | +} |
| 110 | + |
| 111 | +// IsBase64 check if a string is base64 encoded. |
| 112 | +func IsBase64(str string) bool { |
| 113 | + return rxBase64.MatchString(str) |
| 114 | +} |
| 115 | + |
| 116 | +// IsIPv6 check if the string is an IP version 6. |
| 117 | +func IsIPv6(str string) bool { |
| 118 | + ip := net.ParseIP(str) |
| 119 | + return ip != nil && 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 IsMAC(str string) bool { |
| 131 | + _, err := 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 IsSSN(str string) bool { |
| 137 | + if str == "" || len(str) != 11 { |
| 138 | + return false |
| 139 | + } |
| 140 | + return rxSSN.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 ToInt(value interface{}) (res int64, err error) { |
| 145 | + val := reflect.ValueOf(value) |
| 146 | + |
| 147 | + switch value.(type) { |
| 148 | + case int, int8, int16, int32, int64: |
| 149 | + res = val.Int() |
| 150 | + case uint, uint8, uint16, uint32, uint64: |
| 151 | + res = int64(val.Uint()) |
| 152 | + case string: |
| 153 | + if IsInt(val.String()) { |
| 154 | + res, err = strconv.ParseInt(val.String(), 0, 64) |
| 155 | + if err != nil { |
| 156 | + res = 0 |
| 157 | + } |
| 158 | + } else { |
| 159 | + err = fmt.Errorf("math: square root of negative number %g", value) |
| 160 | + res = 0 |
| 161 | + } |
| 162 | + default: |
| 163 | + err = 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 IsInt(str string) bool { |
| 172 | + if IsNull(str) { |
| 173 | + return true |
| 174 | + } |
| 175 | + return rxInt.MatchString(str) |
| 176 | +} |
| 177 | + |
| 178 | +// IsNull check if the string is null. |
| 179 | +func IsNull(str string) bool { |
| 180 | + return len(str) == 0 |
| 181 | +} |
0 commit comments