-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ $ go get -u github.com/x-validator/validator | |
### API列表 | ||
|
||
```go | ||
bool valid = validator.IsValidPhone("11011001111", false, []PhoneType{ AllType }) | ||
bool valid = validator.IsValidPhone("11011001111", false, AllTypePhoneNumber) | ||
|
||
bool valid = validator.IsValidMoney("23.54", false) | ||
|
||
|
@@ -48,11 +48,11 @@ bool valid = validator.IsValidEmail("[email protected]", false) | |
|
||
可以选择验证哪些手机号,或者全部手机号,参考了[ChinaMobilePhoneNumberRegex](https://github.com/VincentSit/ChinaMobilePhoneNumberRegex)项目: | ||
|
||
- **Basic**:仅基础运营商手机卡,11位手机卡-基础运营商,支持语音通话/短信/数据流量 | ||
- **Virtual**:仅虚拟运营商号码,11位手机卡-虚拟运营商,支持语音通话/短信/数据流量 | ||
- **NetOnly**:仅上网数据卡,11位上网卡,支持语音通话(部分)/短信/数据流量 | ||
- **IotOnly**:仅物联网数据卡,13位物联网数据卡,支持数据流量 | ||
- **AllType**:验证所有有效号码(手机卡 + 数据卡 + 上网卡) | ||
- **BasicPhoneNumber**:仅基础运营商手机卡,11位手机卡-基础运营商,支持语音通话/短信/数据流量 | ||
- **VirtualPhoneNumber**:仅虚拟运营商号码,11位手机卡-虚拟运营商,支持语音通话/短信/数据流量 | ||
- **NetOnlyPhoneNumber**:仅上网数据卡,11位上网卡,支持语音通话(部分)/短信/数据流量 | ||
- **IotOnlyPhoneNumber**:仅物联网数据卡,13位物联网数据卡,支持数据流量 | ||
- **AllTypePhoneNumber**:验证所有有效号码(手机卡 + 数据卡 + 上网卡) | ||
|
||
## 贡献 | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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,85 @@ | ||
// @Since 2024-03-24. | ||
// @Author Fury, All rights Reserved. | ||
|
||
package validator | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIsValidPhone(t *testing.T) { | ||
// test empty | ||
pts := []PhoneNumberType{BasicPhoneNumber, VirtualPhoneNumber, NetOnlyPhoneNumber, IotOnlyPhoneNumber, AllTypePhoneNumber} | ||
for _, pt := range pts { | ||
assert.True(t, IsValidPhoneNumber("", true, []PhoneNumberType{pt})) | ||
Check failure on line 15 in phonenumber_test.go GitHub Actions / test
|
||
assert.False(t, IsValidPhoneNumber("", false, []PhoneNumberType{pt})) | ||
Check failure on line 16 in phonenumber_test.go GitHub Actions / test
|
||
} | ||
|
||
// test phones | ||
caseMap := map[string]map[PhoneNumberType]bool{ | ||
"18157610011": { // valid basic | ||
BasicPhoneNumber: true, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: true, | ||
}, | ||
"16577810902": { // valid virtual | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: true, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: true, | ||
}, | ||
"14577812563": { // valid net | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: true, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: true, | ||
}, | ||
"1440775619012": { // valid iot | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: true, | ||
AllTypePhoneNumber: true, | ||
}, | ||
"123456": { // all invalid | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: false, | ||
}, | ||
"181778189012": { // all invalid | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: false, | ||
}, | ||
"1817781890123": { // all invalid | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: false, | ||
}, | ||
"181ab7781cd": { // all invalid | ||
BasicPhoneNumber: false, | ||
VirtualPhoneNumber: false, | ||
NetOnlyPhoneNumber: false, | ||
IotOnlyPhoneNumber: false, | ||
AllTypePhoneNumber: false, | ||
}, | ||
} | ||
for phone, kv := range caseMap { | ||
for pt, expect := range kv { | ||
assert.Equal(t, expect, IsValidPhoneNumber(phone, false, []PhoneNumberType{pt})) | ||
Check failure on line 80 in phonenumber_test.go GitHub Actions / test
|
||
assert.Equal(t, expect, IsValidPhoneNumber(phone, true, []PhoneNumberType{pt})) | ||
Check failure on line 81 in phonenumber_test.go GitHub Actions / test
|
||
} | ||
} | ||
|
||
} |
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,16 @@ | ||
package validator | ||
|
||
type Fn func(value string, allowEmpty bool) bool | ||
|
||
// TagMaps tags map, when you want to add a new validation to the validator | ||
// you can range this map to get the tag and valid function | ||
var TagMaps = map[string]Fn{ | ||
"email": IsValidEmail, | ||
"http_url": IsValidHttpURL, | ||
"id_card": IsValidIdCard, | ||
"identifier": IsValidIdentifier, | ||
"money": IsValidMoney, | ||
"phone_number": func(value string, allowEmpty bool) bool { | ||
return IsValidPhoneNumber(value, allowEmpty, AllTypePhoneNumber) | ||
}, | ||
} |