1
0
Fork 0

added the generic Valid function

Dieser Commit ist enthalten in:
Sebastian Tobie 2022-11-12 13:54:00 +01:00
Ursprung af1af1afc2
Commit 2002010a1e
2 geänderte Dateien mit 17 neuen und 1 gelöschten Zeilen

2
go.mod
Datei anzeigen

@ -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

16
validators.go Normale Datei
Datei anzeigen

@ -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
}