github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/validator/ipv6/ipv6.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 ipv6 6 7 import ( 8 "fmt" 9 "net" 10 "reflect" 11 ) 12 13 // ValidateString validates that the given string is 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 IPv6 address", input) 19 } 20 21 if ip.To4() != nil { 22 return false, fmt.Errorf("%s is not an IPv6 address", input) 23 } 24 25 return true, nil 26 } 27 28 // ValidateStructField validates a struct field holds an IPv4 address 29 func ValidateStructField(value reflect.Value, tag string) (bool, error) { 30 if value.Kind() != reflect.String { 31 return false, fmt.Errorf("only strings can be IPv6 validated") 32 } 33 34 return ValidateString(value.String()) 35 }