From a9c75b081d670f32d862e105f3aeaef37b023d80 Mon Sep 17 00:00:00 2001 From: chentong Date: Mon, 17 Feb 2025 10:03:15 +0800 Subject: [PATCH] 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 --- validator/validator.go | 12 ++++++++++++ validator/validator_example_test.go | 18 ++++++++++++++++++ validator/validator_test.go | 19 ++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/validator/validator.go b/validator/validator.go index 6c4811d..90f2d9f 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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 { diff --git a/validator/validator_example_test.go b/validator/validator_example_test.go index c1c1ceb..c1e736a 100644 --- a/validator/validator_example_test.go +++ b/validator/validator_example_test.go @@ -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") diff --git a/validator/validator_test.go b/validator/validator_test.go index a1b8d28..99e4fb8 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -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")) }