diff --git a/.gitignore b/.gitignore index d1f189b..1b47306 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ fileutil/*.zip fileutil/*.link fileutil/unzip/* slice/testdata/* -cryptor/*.pem \ No newline at end of file +cryptor/*.pem +docs/node_modules +docs/.vitepress \ No newline at end of file diff --git a/docs/validator.md b/docs/validator.md index ad4a218..1b2ec27 100644 --- a/docs/validator.md +++ b/docs/validator.md @@ -55,6 +55,15 @@ import ( - [IsGBK](#IsGBK) - [IsASCII](#IsASCII) - [IsAIsPrintableSCII](#IsPrintable) +- [IsBin](#IsBin) +- [IsHex](#IsHex) +- [IsBase64URL](#IsBase64URL) +- [IsJWT](#IsJWT) +- [IsVisa](#IsVisa) +- [IsMasterCard](#IsMasterCard) +- [IsAmericanExpress](#IsAmericanExpress) +- [IsUnionPay](#IsUnionPay) +- [IsChinaUnionPay](#IsChinaUnionPay)
@@ -1026,4 +1035,301 @@ func main() { // true // false } -``` \ No newline at end of file +``` + +### IsBin + +

Checks if a give string is a valid binary value or not.

+ +Signature: + +```go +func IsBin(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsBin("0101") + result2 := validator.IsBin("0b1101") + result3 := validator.IsBin("b1101") + result4 := validator.IsBin("1201") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsHex + +

Checks if a give string is a valid hexadecimal value or not.

+ +Signature: + +```go +func IsHex(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsHex("0xabcde") + result2 := validator.IsHex("0XABCDE") + result3 := validator.IsHex("cdfeg") + result4 := validator.IsHex("0xcdfeg") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsBase64URL + +

Checks if a give string is a valid URL-safe Base64 encoded string.

+ +Signature: + +```go +func IsBase64URL(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ") + result2 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ==") + result3 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ=") + result4 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ===") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsJWT + +

Checks if a give string is is a valid JSON Web Token (JWT).

+ +Signature: + +```go +func IsJWT(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910") + result2 := validator.IsJWT("abc") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsVisa + +

Checks if a give string is a valid visa card nubmer or not.

+ +Signature: + +```go +func IsVisa(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsVisa("4111111111111111") + result2 := validator.IsVisa("123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsMasterCard + +

Checks if a give string is a valid mastercard nubmer or not.

+ +Signature: + +```go +func IsMasterCard(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsMasterCard("5425233430109903") + result2 := validator.IsMasterCard("4111111111111111") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsAmericanExpress + +

Checks if a give string is a valid american express nubmer or not.

+ +Signature: + +```go +func IsAmericanExpress(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsAmericanExpress("342883359122187") + result2 := validator.IsAmericanExpress("3782822463100007") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsVisa + +

Checks if a give string is a valid union pay nubmer or not.

+ +Signature: + +```go +func IsUnionPay(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsUnionPay("6221263430109903") + result2 := validator.IsUnionPay("3782822463100007") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChinaUnionPay + +

Checks if a give string is a valid china union pay nubmer or not.

+ +Signature: + +```go +func IsChinaUnionPay(v string) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsChinaUnionPay("6250941006528599") + result2 := validator.IsChinaUnionPay("3782822463100007") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` diff --git a/docs/validator_zh-CN.md b/docs/validator_zh-CN.md index a1f34db..57cf305 100644 --- a/docs/validator_zh-CN.md +++ b/docs/validator_zh-CN.md @@ -55,6 +55,15 @@ import ( - [IsGBK](#IsGBK) - [IsASCII](#IsASCII) - [IsPrintable](#IsPrintable) +- [IsBin](#IsBin) +- [IsHex](#IsHex) +- [IsBase64URL](#IsBase64URL) +- [IsJWT](#IsJWT) +- [IsVisa](#IsVisa) +- [IsMasterCard](#IsMasterCard) +- [IsAmericanExpress](#IsAmericanExpress) +- [IsUnionPay](#IsUnionPay) +- [IsChinaUnionPay](#IsChinaUnionPay)
@@ -958,6 +967,7 @@ func main() { fmt.Println("data encoding is unknown") } ``` + ### IsASCII

验证字符串全部为ASCII字符。

@@ -1036,4 +1046,301 @@ func main() { // true // false } -``` \ No newline at end of file +``` + +### IsBin + +

检查字符串是否是有效的二进制数。

+ +函数签名: + +```go +func IsBin(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsBin("0101") + result2 := validator.IsBin("0b1101") + result3 := validator.IsBin("b1101") + result4 := validator.IsBin("1201") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsHex + +

检查字符串是否是有效的十六进制数。

+ +函数签名: + +```go +func IsHex(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsHex("0xabcde") + result2 := validator.IsHex("0XABCDE") + result3 := validator.IsHex("cdfeg") + result4 := validator.IsHex("0xcdfeg") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsBase64URL + +

检查字符串是否是有效的base64 url。

+ +函数签名: + +```go +func IsBase64URL(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ") + result2 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ==") + result3 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ=") + result4 := validator.IsBase64URL("SAGsbG8sIHdvcmxkIQ===") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // false + // false +} +``` + +### IsJWT + +

检查字符串是否是有效的JSON Web Token (JWT)。

+ +函数签名: + +```go +func IsJWT(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910") + result2 := validator.IsJWT("abc") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsVisa + +

检查字符串是否是有效的visa卡号。

+ +函数签名: + +```go +func IsVisa(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsVisa("4111111111111111") + result2 := validator.IsVisa("123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsMasterCard + +

检查字符串是否是有效的MasterCard卡号。

+ +函数签名: + +```go +func IsMasterCard(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsMasterCard("5425233430109903") + result2 := validator.IsMasterCard("4111111111111111") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsAmericanExpress + +

检查字符串是否是有效的American Express卡号。

+ +函数签名: + +```go +func IsAmericanExpress(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsAmericanExpress("342883359122187") + result2 := validator.IsAmericanExpress("3782822463100007") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsUnionPay + +

检查字符串是否是有效的美国银联卡号。

+ +函数签名: + +```go +func IsUnionPay(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsUnionPay("6221263430109903") + result2 := validator.IsUnionPay("3782822463100007") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsChinaUnionPay + +

检查字符串是否是有效的中国银联卡号。

+ +函数签名: + +```go +func IsChinaUnionPay(v string) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/validator" +) + +func main() { + result1 := validator.IsChinaUnionPay("6250941006528599") + result2 := validator.IsChinaUnionPay("3782822463100007") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` diff --git a/fileutil/file.go b/fileutil/file.go index a8ada20..cdceb10 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -168,7 +168,15 @@ func ListFileNames(path string) ([]string, error) { } // Zip create zip file, fpath could be a single file or a directory -func Zip(fpath string, destPath string) error { +func Zip(path string, destPath string) error { + if IsDir(path) { + return zipFolder(path, destPath) + } + + return zipFile(path, destPath) +} + +func zipFile(filePath string, destPath string) error { zipFile, err := os.Create(destPath) if err != nil { return err @@ -178,7 +186,97 @@ func Zip(fpath string, destPath string) error { archive := zip.NewWriter(zipFile) defer archive.Close() - return addFileToArchive(fpath, archive) + return addFileToArchive1(filePath, archive) +} + +func zipFolder(folderPath string, destPath string) error { + outFile, err := os.Create(destPath) + if err != nil { + return err + } + defer outFile.Close() + + w := zip.NewWriter(outFile) + + err = addFileToArchive2(w, folderPath, "") + if err != nil { + return err + } + + err = w.Close() + if err != nil { + return err + } + + return nil +} + +func addFileToArchive1(fpath string, archive *zip.Writer) error { + err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + + header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/") + + if info.IsDir() { + header.Name += "/" + } else { + header.Method = zip.Deflate + writer, err := archive.CreateHeader(header) + if err != nil { + return err + } + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + if _, err := io.Copy(writer, file); err != nil { + return err + } + } + return nil + }) + return err +} + +func addFileToArchive2(w *zip.Writer, basePath, baseInZip string) error { + files, err := os.ReadDir(basePath) + if err != nil { + return err + } + if !strings.HasSuffix(basePath, "/") { + basePath = basePath + "/" + } + + for _, file := range files { + if !file.IsDir() { + dat, err := os.ReadFile(basePath + file.Name()) + if err != nil { + return err + } + + f, err := w.Create(baseInZip + file.Name()) + if err != nil { + return err + } + _, err = f.Write(dat) + if err != nil { + return err + } + } else if file.IsDir() { + newBase := basePath + file.Name() + "/" + addFileToArchive2(w, newBase, baseInZip+file.Name()+"/") + } + } + + return nil } // UnZip unzip the file and save it to destPath @@ -259,7 +357,7 @@ func ZipAppendEntry(fpath string, destPath string) error { } } - err = addFileToArchive(fpath, archive) + err = addFileToArchive1(fpath, archive) if err != nil { return err @@ -281,41 +379,6 @@ func ZipAppendEntry(fpath string, destPath string) error { return CopyFile(tempFile.Name(), destPath) } -func addFileToArchive(fpath string, archive *zip.Writer) error { - err := filepath.Walk(fpath, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - header, err := zip.FileInfoHeader(info) - if err != nil { - return err - } - - header.Name = strings.TrimPrefix(path, filepath.Dir(fpath)+"/") - - if info.IsDir() { - header.Name += "/" - } else { - header.Method = zip.Deflate - writer, err := archive.CreateHeader(header) - if err != nil { - return err - } - file, err := os.Open(path) - if err != nil { - return err - } - defer file.Close() - if _, err := io.Copy(writer, file); err != nil { - return err - } - } - return nil - }) - return err -} - func safeFilepathJoin(path1, path2 string) (string, error) { relPath, err := filepath.Rel(".", path2) if err != nil || strings.HasPrefix(relPath, "..") { diff --git a/fileutil/testdata/test1.csv b/fileutil/testdata/test1.csv index c8961e7..ce44349 100644 --- a/fileutil/testdata/test1.csv +++ b/fileutil/testdata/test1.csv @@ -1,2 +1,3 @@ Lili,22,female Jim,21,male + diff --git a/validator/validator.go b/validator/validator.go index 930cc12..3a897da 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -16,18 +16,26 @@ import ( ) var ( - alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`) - letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`) - intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`) - urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`) - dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`) - emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`) - chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`) - chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`) - chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]") - chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`) - creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|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}|6[27][0-9]{14})$`) - base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`) + alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`) + letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`) + intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`) + urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`) + dnsMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`) + emailMatcher *regexp.Regexp = regexp.MustCompile(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`) + chineseMobileMatcher *regexp.Regexp = regexp.MustCompile(`^1(?:3\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$`) + chineseIdMatcher *regexp.Regexp = regexp.MustCompile(`^[1-9]\d{5}(18|19|20|21|22)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`) + chineseMatcher *regexp.Regexp = regexp.MustCompile("[\u4e00-\u9fa5]") + chinesePhoneMatcher *regexp.Regexp = regexp.MustCompile(`\d{3}-\d{8}|\d{4}-\d{7}`) + creditCardMatcher *regexp.Regexp = regexp.MustCompile(`^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|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}|6[27][0-9]{14})$`) + base64Matcher *regexp.Regexp = regexp.MustCompile(`^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`) + base64URLMatcher *regexp.Regexp = regexp.MustCompile(`^([A-Za-z0-9_-]{4})*([A-Za-z0-9_-]{2}(==)?|[A-Za-z0-9_-]{3}=?)?$`) + binMatcher *regexp.Regexp = regexp.MustCompile(`^(0b)?[01]+$`) + hexMatcher *regexp.Regexp = regexp.MustCompile(`^(#|0x|0X)?[0-9a-fA-F]+$`) + visaMatcher *regexp.Regexp = regexp.MustCompile(`^4[0-9]{12}(?:[0-9]{3})?$`) + masterCardMatcher *regexp.Regexp = regexp.MustCompile(`^5[1-5][0-9]{14}$`) + americanExpressMatcher *regexp.Regexp = regexp.MustCompile(`^3[47][0-9]{13}$`) + unionPay *regexp.Regexp = regexp.MustCompile("^62[0-5]\\d{13,16}$") + chinaUnionPay *regexp.Regexp = regexp.MustCompile(`^62[0-9]{14,17}$`) ) // IsAlpha checks if the string contains only letters (a-zA-Z) @@ -362,3 +370,59 @@ func IsPrintable(str string) bool { } return true } + +// IsBin check if a give string is a valid binary value or not. +func IsBin(v string) bool { + return binMatcher.MatchString(v) +} + +// IsHex check if a give string is a valid hexadecimal value or not. +func IsHex(v string) bool { + return hexMatcher.MatchString(v) +} + +// IsBase64URL check if a give string is a valid URL-safe Base64 encoded string. +func IsBase64URL(v string) bool { + return base64URLMatcher.MatchString(v) +} + +// IsJWT check if a give string is a valid JSON Web Token (JWT). +func IsJWT(v string) bool { + strings := strings.Split(v, ".") + if len(strings) != 3 { + return false + } + + for _, s := range strings { + if !IsBase64URL(s) { + return false + } + } + + return true +} + +// IsVisa check if a give string is a valid visa card nubmer or not. +func IsVisa(v string) bool { + return visaMatcher.MatchString(v) +} + +// IsMasterCard check if a give string is a valid master card nubmer or not. +func IsMasterCard(v string) bool { + return masterCardMatcher.MatchString(v) +} + +// IsAmericanExpress check if a give string is a valid american expression card nubmer or not. +func IsAmericanExpress(v string) bool { + return americanExpressMatcher.MatchString(v) +} + +// IsUnionPay check if a give string is a valid union pay nubmer or not. +func IsUnionPay(v string) bool { + return unionPay.MatchString(v) +} + +// IsChinaUnionPay check if a give string is a valid china union pay nubmer or not. +func IsChinaUnionPay(v string) bool { + return chinaUnionPay.MatchString(v) +} diff --git a/validator/validator_test.go b/validator/validator_test.go index ce7fc36..4a76b9f 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -448,3 +448,90 @@ func TestIsPrintable(t *testing.T) { assert.Equal(true, IsPrintable("😄")) assert.Equal(false, IsPrintable("\u0000")) } + +func TestIsBin(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsBin") + + assert.Equal(true, IsBin("0101")) + assert.Equal(true, IsBin("0b1101")) + + assert.Equal(false, IsBin("b1101")) + assert.Equal(false, IsBin("1201")) + assert.Equal(false, IsBin("")) +} + +func TestIsHex(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsHex") + + assert.Equal(true, IsHex("ABCDE")) + assert.Equal(true, IsHex("abcde")) + assert.Equal(true, IsHex("0xabcde")) + assert.Equal(true, IsHex("0Xabcde")) + assert.Equal(true, IsHex("#abcde")) + + assert.Equal(false, IsHex("cdfeg")) + assert.Equal(false, IsHex("0xcdfeg")) + assert.Equal(false, IsHex("")) +} + +func TestIsBase64URL(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsBase64URL") + + assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ")) + assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ==")) + + assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ=")) + assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ===")) + // assert.Equal(false, IsBase64URL("")) +} + +func TestIsJWT(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsJWT") + + assert.Equal(true, IsJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibWVzc2FnZSI6IlB1dGluIGlzIGFic29sdXRlIHNoaXQiLCJpYXQiOjE1MTYyMzkwMjJ9.wkLWA5GtCpWdxNOrRse8yHZgORDgf8TpJp73WUQb910")) + assert.Equal(false, IsJWT("abc")) +} + +func TestIsVisa(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsVisa") + + assert.Equal(true, IsVisa("4111111111111111")) + assert.Equal(false, IsVisa("123")) +} + +func TestIsMasterCard(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsMasterCard") + + assert.Equal(true, IsMasterCard("5425233430109903")) + assert.Equal(false, IsMasterCard("4111111111111111")) +} + +func TestIsAmericanExpress(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsAmericanExpress") + + assert.Equal(true, IsAmericanExpress("342883359122187")) + assert.Equal(false, IsAmericanExpress("3782822463100007")) +} + +func TestIsUnionPay(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsUnionPay") + + assert.Equal(true, IsUnionPay("6221263430109903")) + assert.Equal(false, IsUnionPay("3782822463100007")) +} + +func TestIsChinaUnionPay(t *testing.T) { + t.Parallel() + assert := internal.NewAssert(t, "TestIsChinaUnionPay") + + assert.Equal(true, IsChinaUnionPay("6250941006528599")) + assert.Equal(false, IsChinaUnionPay("3782822463100007")) +}