-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strip/Freeze code from asaskevich/govalidator and drop dependency
Signed-off-by: Davanum Srinivas <[email protected]>
- Loading branch information
Showing
7 changed files
with
585 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014 Alex Saskevich | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package govalidator | ||
|
||
import "regexp" | ||
|
||
// Basic regular expressions for validating strings | ||
const ( | ||
CreditCard string = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$" | ||
ISBN10 string = "^(?:[0-9]{9}X|[0-9]{10})$" | ||
ISBN13 string = "^(?:[0-9]{13})$" | ||
Hexcolor string = "^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$" | ||
RGBcolor string = "^rgb\\(\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*,\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])\\s*\\)$" | ||
Base64 string = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$" | ||
SSN string = `^\d{3}[- ]?\d{2}[- ]?\d{4}$` | ||
Int string = "^(?:[-+]?(?:0|[1-9][0-9]*))$" | ||
) | ||
|
||
var ( | ||
rxCreditCard = regexp.MustCompile(CreditCard) | ||
rxInt = regexp.MustCompile(Int) | ||
rxISBN10 = regexp.MustCompile(ISBN10) | ||
rxISBN13 = regexp.MustCompile(ISBN13) | ||
rxHexcolor = regexp.MustCompile(Hexcolor) | ||
rxRGBcolor = regexp.MustCompile(RGBcolor) | ||
rxBase64 = regexp.MustCompile(Base64) | ||
rxSSN = regexp.MustCompile(SSN) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
// Package govalidator is package of validators and sanitizers for strings, structs and collections. | ||
package govalidator | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"reflect" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var ( | ||
notNumberRegexp = regexp.MustCompile("[^0-9]+") | ||
whiteSpacesAndMinus = regexp.MustCompile(`[\s-]+`) | ||
) | ||
|
||
// IsRequestURI check if the string rawurl, assuming | ||
// it was received in an HTTP request, is an | ||
// absolute URI or an absolute path. | ||
func IsRequestURI(rawurl string) bool { | ||
_, err := url.ParseRequestURI(rawurl) | ||
return err == nil | ||
} | ||
|
||
// IsHexcolor check if the string is a hexadecimal color. | ||
func IsHexcolor(str string) bool { | ||
return rxHexcolor.MatchString(str) | ||
} | ||
|
||
// IsRGBcolor check if the string is a valid RGB color in form rgb(RRR, GGG, BBB). | ||
func IsRGBcolor(str string) bool { | ||
return rxRGBcolor.MatchString(str) | ||
} | ||
|
||
// IsCreditCard check if the string is a credit card. | ||
func IsCreditCard(str string) bool { | ||
sanitized := notNumberRegexp.ReplaceAllString(str, "") | ||
if !rxCreditCard.MatchString(sanitized) { | ||
return false | ||
} | ||
var sum int64 | ||
var digit string | ||
var tmpNum int64 | ||
var shouldDouble bool | ||
for i := len(sanitized) - 1; i >= 0; i-- { | ||
digit = sanitized[i:(i + 1)] | ||
tmpNum, _ = ToInt(digit) | ||
if shouldDouble { | ||
tmpNum *= 2 | ||
if tmpNum >= 10 { | ||
sum += (tmpNum % 10) + 1 | ||
} else { | ||
sum += tmpNum | ||
} | ||
} else { | ||
sum += tmpNum | ||
} | ||
shouldDouble = !shouldDouble | ||
} | ||
|
||
return sum%10 == 0 | ||
} | ||
|
||
// IsISBN10 check if the string is an ISBN version 10. | ||
func IsISBN10(str string) bool { | ||
return IsISBN(str, 10) | ||
} | ||
|
||
// IsISBN13 check if the string is an ISBN version 13. | ||
func IsISBN13(str string) bool { | ||
return IsISBN(str, 13) | ||
} | ||
|
||
// IsISBN check if the string is an ISBN (version 10 or 13). | ||
// If version value is not equal to 10 or 13, it will be check both variants. | ||
func IsISBN(str string, version int) bool { | ||
sanitized := whiteSpacesAndMinus.ReplaceAllString(str, "") | ||
var checksum int32 | ||
var i int32 | ||
if version == 10 { | ||
if !rxISBN10.MatchString(sanitized) { | ||
return false | ||
} | ||
for i = 0; i < 9; i++ { | ||
checksum += (i + 1) * int32(sanitized[i]-'0') | ||
} | ||
if sanitized[9] == 'X' { | ||
checksum += 10 * 10 | ||
} else { | ||
checksum += 10 * int32(sanitized[9]-'0') | ||
} | ||
if checksum%11 == 0 { | ||
return true | ||
} | ||
return false | ||
} else if version == 13 { | ||
if !rxISBN13.MatchString(sanitized) { | ||
return false | ||
} | ||
factor := []int32{1, 3} | ||
for i = 0; i < 12; i++ { | ||
checksum += factor[i%2] * int32(sanitized[i]-'0') | ||
} | ||
return (int32(sanitized[12]-'0'))-((10-(checksum%10))%10) == 0 | ||
} | ||
return IsISBN(str, 10) || IsISBN(str, 13) | ||
} | ||
|
||
// IsBase64 check if a string is base64 encoded. | ||
func IsBase64(str string) bool { | ||
return rxBase64.MatchString(str) | ||
} | ||
|
||
// IsIPv6 check if the string is an IP version 6. | ||
func IsIPv6(str string) bool { | ||
ip := net.ParseIP(str) | ||
return ip != nil && strings.Contains(str, ":") | ||
} | ||
|
||
// IsMAC check if a string is valid MAC address. | ||
// Possible MAC formats: | ||
// 01:23:45:67:89:ab | ||
// 01:23:45:67:89:ab:cd:ef | ||
// 01-23-45-67-89-ab | ||
// 01-23-45-67-89-ab-cd-ef | ||
// 0123.4567.89ab | ||
// 0123.4567.89ab.cdef | ||
func IsMAC(str string) bool { | ||
_, err := net.ParseMAC(str) | ||
return err == nil | ||
} | ||
|
||
// IsSSN will validate the given string as a U.S. Social Security Number | ||
func IsSSN(str string) bool { | ||
if str == "" || len(str) != 11 { | ||
return false | ||
} | ||
return rxSSN.MatchString(str) | ||
} | ||
|
||
// ToInt convert the input string or any int type to an integer type 64, or 0 if the input is not an integer. | ||
func ToInt(value interface{}) (res int64, err error) { | ||
val := reflect.ValueOf(value) | ||
|
||
switch value.(type) { | ||
case int, int8, int16, int32, int64: | ||
res = val.Int() | ||
case uint, uint8, uint16, uint32, uint64: | ||
res = int64(val.Uint()) | ||
case string: | ||
if IsInt(val.String()) { | ||
res, err = strconv.ParseInt(val.String(), 0, 64) | ||
if err != nil { | ||
res = 0 | ||
} | ||
} else { | ||
err = fmt.Errorf("math: square root of negative number %g", value) | ||
res = 0 | ||
} | ||
default: | ||
err = fmt.Errorf("math: square root of negative number %g", value) | ||
res = 0 | ||
} | ||
|
||
return | ||
} | ||
|
||
// IsInt check if the string is an integer. Empty string is valid. | ||
func IsInt(str string) bool { | ||
if IsNull(str) { | ||
return true | ||
} | ||
return rxInt.MatchString(str) | ||
} | ||
|
||
// IsNull check if the string is null. | ||
func IsNull(str string) bool { | ||
return len(str) == 0 | ||
} |
Oops, something went wrong.