Skip to content

Commit

Permalink
Merge pull request #5 from tigh-latte/enhancement/generic-any
Browse files Browse the repository at this point in the history
Enhancement: Generic `Any`
  • Loading branch information
theflyingcodr authored Feb 19, 2024
2 parents 5cf0762 + 8ed7477 commit 27caec5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ func Email(val string) ValidationFunc {
}
}

// AnyString will check if the provided string is in a set of allowed values.
func AnyString(val string, vv ...string) ValidationFunc {
// Any will check if the provided value is in a set of allowed values.
func Any[T comparable](val T, vv ...T) ValidationFunc {
return func() error {
for _, v := range vv {
if val == v {
Expand All @@ -292,3 +292,10 @@ func AnyString(val string, vv ...string) ValidationFunc {
return errors.New("value not found in allowed values")
}
}

// AnyString will check if the provided string is in a set of allowed values.
//
// Deprecated: use Any instead. Will be removed in a future release.
func AnyString(val string, vv ...string) ValidationFunc {
return Any(val, vv...)
}
5 changes: 3 additions & 2 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ func TestMatchBytes(t *testing.T) {
})
}
}

func TestEqualBool(t *testing.T) {
t.Parallel()
is := is.New(t)
Expand Down Expand Up @@ -969,7 +970,7 @@ func TestEmpty(t *testing.T) {
}
}

func TestAnyString(t *testing.T) {
func TestAny(t *testing.T) {
t.Parallel()
is := is.New(t)
tt := map[string]struct {
Expand Down Expand Up @@ -1000,7 +1001,7 @@ func TestAnyString(t *testing.T) {
for name, test := range tt {
t.Run(name, func(t *testing.T) {
is = is.NewRelaxed(t)
is.Equal(test.expErr, AnyString(test.val, test.list...)())
is.Equal(test.expErr, Any(test.val, test.list...)())
})
}
}

0 comments on commit 27caec5

Please sign in to comment.