mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat(validator): add IsIpPort (#294)
* fix: json tag omitempty convert error * feat(validator): add IsIpPort - Add IsIpPort function to check if a string is in the format ip:port - Update validator_test.go to include tests for the new IsIpPort function - Add an example test for IsIpPort in validator_example_test.go
This commit is contained in:
@@ -207,6 +207,18 @@ func IsIp(ipstr string) bool {
|
||||
return ip != nil
|
||||
}
|
||||
|
||||
// IsIpPort check if the string is ip:port.
|
||||
// Play:
|
||||
func IsIpPort(str string) bool {
|
||||
host, port, err := net.SplitHostPort(str)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
ip := net.ParseIP(host)
|
||||
return ip != nil && IsPort(port)
|
||||
}
|
||||
|
||||
// IsIpV4 check if the string is a ipv4 address.
|
||||
// Play: https://go.dev/play/p/zBGT99EjaIu
|
||||
func IsIpV4(ipstr string) bool {
|
||||
|
||||
@@ -348,6 +348,24 @@ func ExampleIsIp() {
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleIsIpPort() {
|
||||
result1 := IsIpPort("127.0.0.1:8080")
|
||||
result2 := IsIpPort("[0:0:0:0:0:0:0:1]:8080")
|
||||
result3 := IsIpPort(":8080")
|
||||
result4 := IsIpPort("::0:0:0:0:")
|
||||
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
fmt.Println(result3)
|
||||
fmt.Println(result4)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleIsIpV4() {
|
||||
result1 := IsIpV4("127.0.0.1")
|
||||
result2 := IsIpV4("::0:0:0:0:0:0:1")
|
||||
|
||||
@@ -225,6 +225,23 @@ func TestIsIp(t *testing.T) {
|
||||
assert.Equal(false, IsIp("127"))
|
||||
}
|
||||
|
||||
func TestIsIpPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsIpPort")
|
||||
|
||||
assert.Equal(true, IsIpPort("[0:0:0:0:0:0:0:1]:8080")) // valid IPv6 and port
|
||||
assert.Equal(true, IsIpPort("127.0.0.1:8080")) // valid IP and port
|
||||
|
||||
assert.Equal(false, IsIpPort("")) // empty string
|
||||
assert.Equal(false, IsIpPort(":8080")) // only port
|
||||
assert.Equal(false, IsIpPort("127.0.0.1")) // valid IP without port
|
||||
assert.Equal(false, IsIpPort("0:0:0:0:0:0:0:1")) // valid IPv6 without port
|
||||
assert.Equal(false, IsIpPort("256.256.256.256:8080")) // invalid IP with valid port
|
||||
assert.Equal(false, IsIpPort("256.256.256.256:abc")) // invalid IP and invalid port
|
||||
assert.Equal(false, IsIpPort("127.0.0.1:70000")) // valid IP with invalid port
|
||||
}
|
||||
|
||||
func TestIsIpV4(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -315,7 +332,7 @@ func TestIsChinesePhone(t *testing.T) {
|
||||
|
||||
assert.Equal(true, IsChinesePhone("010-32116675"))
|
||||
assert.Equal(true, IsChinesePhone("0464-8756213"))
|
||||
assert.Equal(true, IsChinesePhone("0731-82251545")) //长沙晚报电话
|
||||
assert.Equal(true, IsChinesePhone("0731-82251545")) // 长沙晚报电话
|
||||
assert.Equal(false, IsChinesePhone("123-87562"))
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user