-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from rezakhademix/add-in-validation-function
added: in validation generic function
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package validator | ||
|
||
// In checks value under validation must be included in the given list of values | ||
func In[T comparable](value T, acceptableValues ...T) bool { | ||
for i := range acceptableValues { | ||
if value == acceptableValues[i] { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package validator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_In(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
acceptableValues []any | ||
expectedResult bool | ||
}{ | ||
{ | ||
name: "test integer value does not exist in acceptableValues", | ||
value: 4, | ||
acceptableValues: []any{1, 2, 3}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "test string value does not exist in acceptable values", | ||
value: "redis", | ||
acceptableValues: []any{"mysql", "mariadb", "postgres"}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "test empty string value does not exist in acceptable values", | ||
value: "", | ||
acceptableValues: []any{"pen", "pencil", "pipe"}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "test empty space string value does not exist in acceptable values", | ||
value: " ", | ||
acceptableValues: []any{"joe", "jane", "john"}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "test integer value exists in acceptable values", | ||
value: 20, | ||
acceptableValues: []any{10, 20, 30}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
name: "test string value exists in acceptable values", | ||
value: "go", | ||
acceptableValues: []any{"go", "php", "java"}, | ||
expectedResult: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
result := In(test.value, test.acceptableValues...) | ||
|
||
assert.Equalf( | ||
t, | ||
test.expectedResult, | ||
result, | ||
"test case %q failed: expected %v, got %v", | ||
test.name, test.expectedResult, result, | ||
) | ||
} | ||
} |