1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22: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:
chentong
2025-02-17 10:03:15 +08:00
committed by GitHub
parent db479ef1bc
commit a9c75b081d
3 changed files with 48 additions and 1 deletions

View File

@@ -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 {