common/helpers: correctly validate netip.Addr/netip.Prefix

validate is only able to validate non-struct types (or recurse inside
struct). So, if we want to use "required" on some of them, we need a
custom type.

Fix #263
This commit is contained in:
Vincent Bernat
2022-11-15 18:37:46 +01:00
parent 6644d99001
commit bf99e2211e
3 changed files with 79 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ package helpers
import (
"net"
"net/netip"
"reflect"
"strconv"
@@ -31,6 +32,23 @@ func RegisterSubnetMapValidation[V any]() {
Validate.RegisterCustomTypeFunc(validatorFunc, zero)
}
// netipValidation validates netip.Addr and netip.Prefix by turning them into a string.
func netipValidation(fl reflect.Value) interface{} {
switch netipSomething := fl.Interface().(type) {
case netip.Addr:
if (netipSomething == netip.Addr{}) {
return ""
}
return netipSomething.String()
case netip.Prefix:
if (netipSomething == netip.Prefix{}) {
return ""
}
return netipSomething.String()
}
return nil
}
// isListen validates a <dns>:<port> combination for fields typically used for listening address
func isListen(fl validator.FieldLevel) bool {
val := fl.Field().String()
@@ -53,5 +71,6 @@ func isListen(fl validator.FieldLevel) bool {
func init() {
Validate = validator.New()
Validate.RegisterValidation("listen", isListen)
Validate.RegisterCustomTypeFunc(netipValidation, netip.Addr{}, netip.Prefix{})
RegisterSubnetMapValidation[string]()
}