github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/validator/ipaddress/ipaddress.go (about) 1 // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package ipaddress 6 7 import ( 8 "fmt" 9 "net" 10 "reflect" 11 ) 12 13 // ValidateString validates that the given string is either an IPv6 or an IPv4 address 14 func ValidateString(input string) (bool, error) { 15 ip := net.ParseIP(input) 16 17 if ip == nil { 18 return false, fmt.Errorf("%s is not an IP address", input) 19 } 20 21 return true, nil 22 } 23 24 // ValidateStructField validates a struct field holds either an IPv6 or an IPv4 address 25 func ValidateStructField(value reflect.Value, tag string) (bool, error) { 26 if value.Kind() != reflect.String { 27 return false, fmt.Errorf("only strings can be IPv6 validated") 28 } 29 30 return ValidateString(value.String()) 31 }