Skip to content

Commit 48ca92d

Browse files
authored
Feature/development (#11)
* adding isInArray and the isNotInArray and IsNotIn * fixed test issues * adding null check to the new added functions
1 parent e075051 commit 48ca92d

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

validator/is.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,75 @@ func IsEmail(value interface{}) error {
8484
// IsIn checks if a value is in a predefined list of allowed values.
8585
func IsIn(allowedValues ...interface{}) ValidatorFunc {
8686
return func(value interface{}) error {
87+
// check if the value is nil
88+
if value == nil {
89+
return errors.New("value is nil")
90+
}
8791
for _, allowed := range allowedValues {
88-
if value == allowed {
92+
if reflect.DeepEqual(value, allowed) {
8993
return nil
9094
}
9195
}
9296
return fmt.Errorf("value must be one of %v", allowedValues)
9397
}
9498
}
9599

100+
// IsNotIn checks if a value is not in a predefined list of disallowed values.
101+
func IsNotIn(disallowedValues ...interface{}) ValidatorFunc {
102+
return func(value interface{}) error {
103+
// check if the value is nil
104+
if value == nil {
105+
return errors.New("value is nil")
106+
}
107+
for _, disallowed := range disallowedValues {
108+
if reflect.DeepEqual(value, disallowed) {
109+
return fmt.Errorf("value must not be one of %v", disallowedValues)
110+
}
111+
}
112+
return nil
113+
}
114+
}
115+
116+
// IsInArray checks if a value is in an array.
117+
func IsInArray(array interface{}) ValidatorFunc {
118+
return func(value interface{}) error {
119+
// check if the value is nil
120+
if value == nil {
121+
return errors.New("value is nil")
122+
}
123+
arr := reflect.ValueOf(array)
124+
if arr.Kind() != reflect.Slice && arr.Kind() != reflect.Array {
125+
return fmt.Errorf("expected an array or slice, got %T", array)
126+
}
127+
for i := 0; i < arr.Len(); i++ {
128+
if reflect.DeepEqual(value, arr.Index(i).Interface()) {
129+
return nil
130+
}
131+
}
132+
return fmt.Errorf("value must be one of %v", array)
133+
}
134+
}
135+
136+
// IsNotInArray checks if a value is not in an array.
137+
func IsNotInArray(array interface{}) ValidatorFunc {
138+
return func(value interface{}) error {
139+
// check if the value is nil
140+
if value == nil {
141+
return errors.New("value is nil")
142+
}
143+
arr := reflect.ValueOf(array)
144+
if arr.Kind() != reflect.Slice && arr.Kind() != reflect.Array {
145+
return fmt.Errorf("expected an array or slice, got %T", array)
146+
}
147+
for i := 0; i < arr.Len(); i++ {
148+
if reflect.DeepEqual(value, arr.Index(i).Interface()) {
149+
return fmt.Errorf("value must not be one of %v", array)
150+
}
151+
}
152+
return nil
153+
}
154+
}
155+
96156
// IsString checks if a value is a string.
97157
func IsString(value interface{}) error {
98158
if reflect.TypeOf(value).Kind() != reflect.String {

validator/is_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,65 @@ func TestIsIn(t *testing.T) {
113113
})
114114
}
115115
}
116+
func TestIsNotIn(t *testing.T) {
117+
isNotIn := IsNotIn("apple", "banana", "cherry")
118+
tests := []struct {
119+
name string
120+
input interface{}
121+
error error
122+
}{
123+
{"valid value", "grape", nil},
124+
{"invalid value", nil, errors.New("value is nil")},
125+
{"invalid value", "apple", errors.New("value must not be one of [apple banana cherry]")},
126+
{"wrong type", 123, nil},
127+
}
128+
for _, test := range tests {
129+
t.Run(test.name, func(t *testing.T) {
130+
err := isNotIn(test.input)
131+
require.Equal(t, test.error, err)
132+
})
133+
}
134+
}
135+
136+
func TestIsInArray(t *testing.T) {
137+
isInArray := IsInArray([]string{"apple", "banana", "cherry"})
138+
tests := []struct {
139+
name string
140+
input interface{}
141+
error error
142+
}{
143+
{"valid value", "apple", nil},
144+
{"invalid value", nil, errors.New("value is nil")},
145+
{"invalid value", "grape", errors.New("value must be one of [apple banana cherry]")},
146+
{"wrong type", 123, errors.New("value must be one of [apple banana cherry]")},
147+
}
148+
for _, test := range tests {
149+
t.Run(test.name, func(t *testing.T) {
150+
err := isInArray(test.input)
151+
require.Equal(t, test.error, err)
152+
})
153+
}
154+
}
116155

156+
func TestIsNotInArray(t *testing.T) {
157+
isNotInArray := IsNotInArray([]string{"apple", "banana", "cherry"})
158+
tests := []struct {
159+
name string
160+
input interface{}
161+
error error
162+
}{
163+
{"valid value", "grape", nil},
164+
{"invalid value", nil, errors.New("value is nil")},
165+
{"invalid value", "apple", errors.New("value must not be one of [apple banana cherry]")},
166+
{"wrong type", 123, nil},
167+
}
168+
for _, test := range tests {
169+
t.Run(test.name, func(t *testing.T) {
170+
err := isNotInArray(test.input)
171+
require.Equal(t, test.error, err)
172+
})
173+
}
174+
}
117175
func TestIsString(t *testing.T) {
118176
tests := []struct {
119177
name string

validator/transformers_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func TestReplace(t *testing.T) {
171171
expected interface{}
172172
}{
173173
{"foo bar", "bar bar"},
174+
{"foo bar foo", "bar bar bar"},
174175
{"hello world", "hello world"},
175176
{123, 123}, // Non-string input
176177
}

0 commit comments

Comments
 (0)