| 1 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
func IsRequestURI(rawurl string) bool { |
| 23 |
_, err := url.ParseRequestURI(rawurl) |
| 24 |
return err == nil |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
func IsHexcolor(str string) bool { |
| 29 |
return rxHexcolor.MatchString(str) |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
func IsRGBcolor(str string) bool { |
| 34 |
return rxRGBcolor.MatchString(str) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 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 |
|
| 67 |
func IsISBN10(str string) bool { |
| 68 |
return IsISBN(str, 10) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
func IsISBN13(str string) bool { |
| 73 |
return IsISBN(str, 13) |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 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 |
|
| 112 |
func IsBase64(str string) bool { |
| 113 |
return rxBase64.MatchString(str) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
func IsIPv6(str string) bool { |
| 118 |
ip := net.ParseIP(str) |
| 119 |
return ip != nil && strings.Contains(str, ":") |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
func IsMAC(str string) bool { |
| 131 |
_, err := net.ParseMAC(str) |
| 132 |
return err == nil |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func IsSSN(str string) bool { |
| 137 |
if str == "" || len(str) != 11 { |
| 138 |
return false |
| 139 |
} |
| 140 |
return rxSSN.MatchString(str) |
| 141 |
} |
| 142 |
|
| 143 |
|
| 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 |
|
| 171 |
func IsInt(str string) bool { |
| 172 |
if IsNull(str) { |
| 173 |
return true |
| 174 |
} |
| 175 |
return rxInt.MatchString(str) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
func IsNull(str string) bool { |
| 180 |
return len(str) == 0 |
| 181 |
} |