diff --git a/go.mod b/go.mod index 3d2f716..ecf7e64 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module gitea.sebastian-tobie.de/sebastian/generic -go 1.18 +go 1.19 require github.com/rs/zerolog v1.27.0 diff --git a/validators.go b/validators.go new file mode 100644 index 0000000..6b31fe4 --- /dev/null +++ b/validators.go @@ -0,0 +1,16 @@ +package generic + +// Valid returns true if the argument is not nil and can be converted to the type. +// if additional validators are required they can be given as arguments +func Valid[T any](item any, validator ...func(T) bool) (ok bool) { + if item != nil { + var v, ok = item.(T) + if len(validator) != 0 { + for _, vali := range validator { + ok = ok && vali(v) + } + } + return ok + } + return false +}