add generic in/not in validation

This commit is contained in:
Aneurin Barker Snook
2023-10-07 11:24:34 +01:00
parent b4b5b8b752
commit c2efe16dc0
2 changed files with 96 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
package validate
import "errors"
// In validates whether a value is found in a slice of allowed values.
func In[T comparable](allow []T) func(T) error {
return func(value T) error {
for _, cmp := range allow {
if cmp == value {
return nil
}
}
return errors.New("Not an allowed value")
}
}
// NotIn validates whether a value is not found in a slice of disallowed values.
func NotIn[T comparable](allow []T) func(T) error {
return func(value T) error {
for _, cmp := range allow {
if cmp == value {
return errors.New("Not an allowed value")
}
}
return nil
}
}