common/helpers: make validation work for SubnetMap

This commit is contained in:
Vincent Bernat
2022-08-14 15:06:53 +02:00
parent 574ec7e79e
commit d9a8262f76
3 changed files with 66 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ package helpers
import (
"net"
"reflect"
"strconv"
"github.com/go-playground/validator/v10"
@@ -13,6 +14,20 @@ import (
// Validate is a validator instance to be used everywhere.
var Validate *validator.Validate
// RegisterSubnetMapValidation register a new SubnetMap[] type for
// validation. As validator requires an explicit type, we cannot just
// register all subnetmaps.
func RegisterSubnetMapValidation[V any]() {
var zero SubnetMap[V]
validatorFunc := func(field reflect.Value) interface{} {
if subnetMap, ok := field.Interface().(SubnetMap[V]); ok {
return subnetMap.ToMap()
}
return nil
}
Validate.RegisterCustomTypeFunc(validatorFunc, zero)
}
// isListen validates a <dns>:<port> combination for fields typically used for listening address
func isListen(fl validator.FieldLevel) bool {
val := fl.Field().String()
@@ -35,4 +50,5 @@ func isListen(fl validator.FieldLevel) bool {
func init() {
Validate = validator.New()
Validate.RegisterValidation("listen", isListen)
RegisterSubnetMapValidation[string]()
}