add all validator, simplify error messages

This commit is contained in:
Aneurin Barker Snook
2023-10-07 13:42:17 +01:00
parent c2efe16dc0
commit c74486d122
8 changed files with 98 additions and 19 deletions
+9 -4
View File
@@ -2,24 +2,29 @@ package validate
import "errors"
// Validation error.
var (
ErrValueNotAllowed = errors.New("not allowed")
)
// In validates whether a value is found in a slice of allowed values.
func In[T comparable](allow []T) func(T) error {
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")
return ErrValueNotAllowed
}
}
// NotIn validates whether a value is not found in a slice of disallowed values.
func NotIn[T comparable](allow []T) func(T) error {
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 ErrValueNotAllowed
}
}
return nil