Files
validate/in.go
T

33 lines
645 B
Go
Raw Normal View History

2023-10-07 11:24:34 +01:00
package validate
import "errors"
2023-10-07 13:42:17 +01:00
// Validation error.
var (
ErrValueNotAllowed = errors.New("not allowed")
)
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
}
}