Files

30 lines
605 B
Go
Raw Permalink Normal View History

2023-10-07 11:24:34 +01:00
package validate
2023-10-07 13:42:17 +01:00
var (
2023-11-19 20:42:48 +00:00
ErrValueNotAllowed = NewError("not allowed")
2023-10-07 13:42:17 +01:00
)
2023-10-07 11:24:34 +01:00
// In validates whether a value is found in a slice of allowed values.
2023-10-07 13:42:17 +01:00
func In[T comparable](allow ...T) func(T) error {
2023-10-07 11:24:34 +01:00
return func(value T) error {
for _, cmp := range allow {
if cmp == value {
return nil
}
}
2023-10-07 13:42:17 +01:00
return ErrValueNotAllowed
2023-10-07 11:24:34 +01:00
}
}
// NotIn validates whether a value is not found in a slice of disallowed values.
2023-10-07 13:42:17 +01:00
func NotIn[T comparable](allow ...T) func(T) error {
2023-10-07 11:24:34 +01:00
return func(value T) error {
for _, cmp := range allow {
if cmp == value {
2023-10-07 13:42:17 +01:00
return ErrValueNotAllowed
2023-10-07 11:24:34 +01:00
}
}
return nil
}
}