add generic in/not in validation
This commit is contained in:
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
+69
@@ -0,0 +1,69 @@
|
|||||||
|
package validate
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIn(t *testing.T) {
|
||||||
|
type TestCase[T comparable] struct {
|
||||||
|
S []T
|
||||||
|
Input T
|
||||||
|
Err bool
|
||||||
|
}
|
||||||
|
|
||||||
|
strIn := []string{"abcd", "ef", "1234"}
|
||||||
|
strTestCases := []TestCase[string]{
|
||||||
|
{S: strIn, Input: "abcd"},
|
||||||
|
{S: strIn, Input: "ef"},
|
||||||
|
{S: strIn, Input: "1234"},
|
||||||
|
{S: strIn, Input: "5678", Err: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range strTestCases {
|
||||||
|
t.Logf("%q in %s", tc.Input, strings.Join(tc.S, ", "))
|
||||||
|
|
||||||
|
f := In(tc.S)
|
||||||
|
err := f(tc.Input)
|
||||||
|
|
||||||
|
if tc.Err {
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error; got nil")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Expected nil; got %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
intIn := []int{1, 23, 456}
|
||||||
|
intTestCases := []TestCase[int]{
|
||||||
|
{S: intIn, Input: 1},
|
||||||
|
{S: intIn, Input: 23},
|
||||||
|
{S: intIn, Input: 456},
|
||||||
|
{S: intIn, Input: 789, Err: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range intTestCases {
|
||||||
|
intf := []string{}
|
||||||
|
for _, v := range tc.S {
|
||||||
|
intf = append(intf, fmt.Sprint(v))
|
||||||
|
}
|
||||||
|
t.Logf("%d in %s", tc.Input, strings.Join(intf, ", "))
|
||||||
|
|
||||||
|
f := In(tc.S)
|
||||||
|
err := f(tc.Input)
|
||||||
|
|
||||||
|
if tc.Err {
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error; got nil")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Expected nil; got %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user