1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 06:32:28 +08:00

publish lancet

This commit is contained in:
dudaodong
2021-11-28 21:28:23 +08:00
parent 37edb0fc8f
commit 3254591ab9
38 changed files with 5163 additions and 0 deletions

194
validator/validator.go Normal file
View File

@@ -0,0 +1,194 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package validator implements some validate function for string.
package validator
import (
"net"
"regexp"
"strconv"
"unicode"
)
// IsAlpha checks if the string contains only letters (a-zA-Z)
func IsAlpha(s string) bool {
pattern := `^[a-zA-Z]+$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(s)
}
// IsNumberStr check if the string can convert to a number.
func IsNumberStr(s string) bool {
return IsIntStr(s) || IsFloatStr(s)
}
// IsFloatStr check if the string can convert to a float.
func IsFloatStr(s string) bool {
_, e := strconv.ParseFloat(s, 64)
if e != nil {
return false
}
return true
}
// IsIntStr check if the string can convert to a integer.
func IsIntStr(s string) bool {
match, _ := regexp.MatchString(`^[\+-]?\d+$`, s)
return match
}
// IsIp check if the string is a ip address.
func IsIp(ipstr string) bool {
ip := net.ParseIP(ipstr)
if ip == nil {
return false
}
return true
}
// IsIpV4 check if the string is a ipv4 address.
func IsIpV4(ipstr string) bool {
ip := net.ParseIP(ipstr)
if ip == nil {
return false
}
for i := 0; i < len(ipstr); i++ {
switch ipstr[i] {
case '.':
return true
}
}
return false
}
// IsIpV6 check if the string is a ipv6 address.
func IsIpV6(ipstr string) bool {
ip := net.ParseIP(ipstr)
if ip == nil {
return false
}
for i := 0; i < len(ipstr); i++ {
switch ipstr[i] {
case ':':
return true
}
}
return false
}
// IsDns check if the string is dns.
func IsDns(dns string) bool {
pattern := `^[a-zA-Z]([a-zA-Z0-9\-]+[\.]?)*[a-zA-Z0-9]$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(dns)
}
// IsEmail check if the string is a email address.
func IsEmail(email string) bool {
pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`
reg := regexp.MustCompile(pattern)
return reg.MatchString(email)
}
// IsChineseMobile check if the string is chinese mobile number.
func IsChineseMobile(mobileNum string) bool {
pattern := "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$"
reg := regexp.MustCompile(pattern)
return reg.MatchString(mobileNum)
}
// IsChineseIdNum check if the string is chinese id number.
func IsChineseIdNum(id string) bool {
pattern := `^[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]$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(id)
}
// ContainChinese check if the string contain mandarin chinese.
func ContainChinese(s string) bool {
pattern := "[\u4e00-\u9fa5]"
reg := regexp.MustCompile(pattern)
return reg.MatchString(s)
}
// IsChinesePhone check if the string is chinese phone number.
// Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx
func IsChinesePhone(phone string) bool {
pattern := `\d{3}-\d{8}|\d{4}-\d{7}`
reg := regexp.MustCompile(pattern)
return reg.MatchString(phone)
}
// IsCreditCard check if the string is credit card.
func IsCreditCard(creditCart string) bool {
pattern := `^(?: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})$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(creditCart)
}
// IsBase64 check if the string is base64 string.
func IsBase64(base64 string) bool {
pattern := `^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$`
reg := regexp.MustCompile(pattern)
return reg.MatchString(base64)
}
// IsEmptyString check if the string is empty.
func IsEmptyString(s string) bool {
return len(s) == 0
}
// IsRegexMatch check if the string match the regexp
func IsRegexMatch(s, regex string) bool {
reg := regexp.MustCompile(regex)
return reg.MatchString(s)
}
// IsStrongPassword check if the string is strong password, if len(password) is less than the length param, return false
// Strong password: alpha(lower+upper) + number + special chars(!@#$%^&*()?><)
func IsStrongPassword(password string, length int) bool {
if len(password) < length {
return false
}
var num, lower, upper, special bool
for _, r := range password {
switch {
case unicode.IsDigit(r):
num = true
case unicode.IsUpper(r):
upper = true
case unicode.IsLower(r):
lower = true
case unicode.IsSymbol(r), unicode.IsPunct(r):
special = true
}
}
return num && lower && upper && special
// go doesn't support regexp (?=re)
//pattern := `^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=])(?=\S+$).$`
//reg := regexp.MustCompile(pattern)
//return reg.MatchString(password)
}
// IsWeakPassword check if the string is weak password
// Weak password: only letter or only number or letter + number
func IsWeakPassword(password string) bool {
var num, letter, special bool
for _, r := range password {
switch {
case unicode.IsDigit(r):
num = true
case unicode.IsLetter(r):
letter = true
case unicode.IsSymbol(r), unicode.IsPunct(r):
special = true
}
}
return (num || letter) && !special
}

283
validator/validator_test.go Normal file
View File

@@ -0,0 +1,283 @@
package validator
import (
"testing"
"github.com/duke-git/lancet/utils"
)
func TestIsNumberStr(t *testing.T) {
isNumberStr(t, "3.", true)
isNumberStr(t, "+3.", true)
isNumberStr(t, "-3.", true)
isNumberStr(t, "+3e2", true)
isNumberStr(t, "abc", false)
}
func isNumberStr(t *testing.T, source string, expected bool) {
res := IsNumberStr(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsNumberStr", source, expected, res)
t.FailNow()
}
}
func TestIsFloatStr(t *testing.T) {
isFloatStr(t, "3.", true)
isFloatStr(t, "+3.", true)
isFloatStr(t, "-3.", true)
isFloatStr(t, "12", true)
isFloatStr(t, "abc", false)
}
func isFloatStr(t *testing.T, source string, expected bool) {
res := IsFloatStr(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsFloatStr", source, expected, res)
t.FailNow()
}
}
func TestIsIntStr(t *testing.T) {
isIntStr(t, "+3", true)
isIntStr(t, "-3", true)
isIntStr(t, "3.", false)
isIntStr(t, "abc", false)
}
func isIntStr(t *testing.T, source string, expected bool) {
res := IsIntStr(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsIntStr", source, expected, res)
t.FailNow()
}
}
func TestIsIp(t *testing.T) {
isIp(t, "127.0.0.1", true)
isIp(t, "::0:0:0:0:0:0:1", true)
isIp(t, "120.0.0", false)
isIp(t, "abc", false)
}
func isIp(t *testing.T, source string, expected bool) {
res := IsIp(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsIp", source, expected, res)
t.FailNow()
}
}
func TestIsIpV4(t *testing.T) {
isIpV4(t, "127.0.0.1", true)
isIpV4(t, "::0:0:0:0:0:0:1", false)
}
func isIpV4(t *testing.T, source string, expected bool) {
res := IsIpV4(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsIpV4", source, expected, res)
t.FailNow()
}
}
func TestIsIpV6(t *testing.T) {
isIpV6(t, "127.0.0.1", false)
isIpV6(t, "::0:0:0:0:0:0:1", true)
}
func isIpV6(t *testing.T, source string, expected bool) {
res := IsIpV6(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsIpV6", source, expected, res)
t.FailNow()
}
}
func TestIsDns(t *testing.T) {
isDns(t, "abc.com", true)
isDns(t, "a.b.com", false)
}
func isDns(t *testing.T, source string, expected bool) {
res := IsDns(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsDns", source, expected, res)
t.FailNow()
}
}
func TestIsEmail(t *testing.T) {
isEmail(t, "abc@xyz.com", true)
isEmail(t, "a.b@@com", false)
}
func isEmail(t *testing.T, source string, expected bool) {
res := IsEmail(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsEmail", source, expected, res)
t.FailNow()
}
}
func TestContainChinese(t *testing.T) {
containChinese(t, "你好", true)
containChinese(t, "hello", false)
containChinese(t, "hello你好", true)
}
func containChinese(t *testing.T, source string, expected bool) {
res := ContainChinese(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsContainChineseChar", source, expected, res)
t.FailNow()
}
}
func TestIsChineseMobile(t *testing.T) {
isChineseMobile(t, "13263527980", true)
isChineseMobile(t, "434324324", false)
}
func isChineseMobile(t *testing.T, source string, expected bool) {
res := IsChineseMobile(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsChineseMobile", source, expected, res)
t.FailNow()
}
}
func TestIsChinesePhone(t *testing.T) {
isChinesePhone(t, "010-32116675", true)
isChinesePhone(t, "0464-8756213", true)
isChinesePhone(t, "123-87562", false)
}
func isChinesePhone(t *testing.T, source string, expected bool) {
res := IsChinesePhone(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsChinesePhone", source, expected, res)
t.FailNow()
}
}
func TestIsChineseIdNum(t *testing.T) {
isChineseIdNum(t, "210911192105130715", true)
isChineseIdNum(t, "21091119210513071X", true)
isChineseIdNum(t, "21091119210513071x", true)
isChineseIdNum(t, "123456", false)
}
func isChineseIdNum(t *testing.T, source string, expected bool) {
res := IsChineseIdNum(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsChineseIdNum", source, expected, res)
t.FailNow()
}
}
func TestIsCreditCard(t *testing.T) {
isCreditCard(t, "4111111111111111", true)
isCreditCard(t, "123456", false)
}
func isCreditCard(t *testing.T, source string, expected bool) {
res := IsCreditCard(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsCreditCard", source, expected, res)
t.FailNow()
}
}
func TestIsBase64(t *testing.T) {
isBase64(t, "aGVsbG8", true)
isBase64(t, "123456", false)
}
func isBase64(t *testing.T, source string, expected bool) {
res := IsBase64(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsBase64", source, expected, res)
t.FailNow()
}
}
func TestIsEmptyString(t *testing.T) {
isEmptyString(t, "111", false)
isEmptyString(t, " ", false)
isEmptyString(t, "\t", false)
isEmptyString(t, "", true)
}
func isEmptyString(t *testing.T, source string, expected bool) {
res := IsEmptyString(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsEmptyString", source, expected, res)
t.FailNow()
}
}
func TestIsAlpha(t *testing.T) {
isAlpha(t, "abc", true)
isAlpha(t, "111", false)
isAlpha(t, " ", false)
isAlpha(t, "\t", false)
isAlpha(t, "", false)
}
func isAlpha(t *testing.T, source string, expected bool) {
res := IsAlpha(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsAlpha", source, expected, res)
t.FailNow()
}
}
func TestIsRegexMatch(t *testing.T) {
isRegexMatch(t, "abc", `^[a-zA-Z]+$`, true)
isRegexMatch(t, "1ab", `^[a-zA-Z]+$`, false)
isRegexMatch(t, "111", `^[a-zA-Z]+$`, false)
isRegexMatch(t, "", `^[a-zA-Z]+$`, false)
}
func isRegexMatch(t *testing.T, source, regex string, expected bool) {
res := IsRegexMatch(source, regex)
if res != expected {
utils.LogFailedTestInfo(t, "IsRegexMatch", source, expected, res)
t.FailNow()
}
}
func TestIsStrongPassword(t *testing.T) {
isStrongPassword(t, "abc", 3, false)
isStrongPassword(t, "abc123", 6, false)
isStrongPassword(t, "abcABC", 6, false)
isStrongPassword(t, "abc123@#$", 9, false)
isStrongPassword(t, "abcABC123@#$", 16, false)
isStrongPassword(t, "abcABC123@#$", 12, true)
isStrongPassword(t, "abcABC123@#$", 10, true)
}
func isStrongPassword(t *testing.T, source string, length int, expected bool) {
res := IsStrongPassword(source, length)
if res != expected {
utils.LogFailedTestInfo(t, "IsStrongPassword", source, expected, res)
t.FailNow()
}
}
func TestIsWeakPassword(t *testing.T) {
isWeakPassword(t, "abc", true)
isWeakPassword(t, "123", true)
isWeakPassword(t, "abc123", true)
isWeakPassword(t, "abcABC123", true)
isWeakPassword(t, "abc123@#$", false)
}
func isWeakPassword(t *testing.T, source string, expected bool) {
res := IsWeakPassword(source)
if res != expected {
utils.LogFailedTestInfo(t, "IsWeakPassword", source, expected, res)
t.FailNow()
}
}