-
Notifications
You must be signed in to change notification settings - Fork 0
adding isInArray and the isNotInArray and IsNotIn #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -113,7 +113,62 @@ func TestIsIn(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestIsNotIn(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isNotIn := IsNotIn("apple", "banana", "cherry") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input interface{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"valid value", "grape", nil}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"invalid value", "apple", errors.New("value must not be one of [apple banana cherry]")}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"wrong type", 123, nil}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, test := range tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Run(test.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := isNotIn(test.input) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.Equal(t, test.error, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestIsInArray(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isInArray := IsInArray([]string{"apple", "banana", "cherry"}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input interface{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"valid value", "apple", nil}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"invalid value", "grape", errors.New("value must be one of [apple banana cherry]")}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"wrong type", 123, errors.New("value must be one of [apple banana cherry]")}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, test := range tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Run(test.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := isInArray(test.input) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.Equal(t, test.error, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+135
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix error message assertion in tests The test assertions don't match the current implementation's error messages, leading to test failures. Either update the tests to match the implementation or modify the implementation to match the tests. func TestIsInArray(t *testing.T) {
isInArray := IsInArray([]string{"apple", "banana", "cherry"})
tests := []struct {
name string
input interface{}
error error
}{
{"valid value", "apple", nil},
- {"invalid value", "grape", errors.New("value must be one of [apple banana cherry]")},
- {"wrong type", 123, errors.New("value must be one of [apple banana cherry]")},
+ {"invalid value", "grape", errors.New("value is not in the array")},
+ {"wrong type", 123, nil}, // Current implementation returns nil for type mismatches
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err := isInArray(test.input)
require.Equal(t, test.error, err)
})
}
}Note: The better approach is to fix the implementation as suggested in the 📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Actions: Go Tests[error] 149-149: TestIsInArray/invalid_value failed: expected error 'value must be one of [apple banana cherry]', got 'value is not in the array' [error] 149-149: TestIsInArray/wrong_type failed: expected error 'value must be one of [apple banana cherry]', got |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestIsNotInArray(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isNotInArray := IsNotInArray([]string{"apple", "banana", "cherry"}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input interface{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| error error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"valid value", "grape", nil}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"invalid value", "apple", errors.New("value must not be one of [apple banana cherry]")}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {"wrong type", 123, nil}, // Or errors.New("value must not be one of [apple banana cherry]") if strict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, test := range tests { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Run(test.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := isNotInArray(test.input) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| require.Equal(t, test.error, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestIsString(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tests := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation needs type handling improvement
The current implementation doesn't properly handle the "wrong type" case according to the failing test. When comparing values of different types using
==, Go returns false rather than an error.func IsNotIn(disallowedValues ...interface{}) ValidatorFunc { return func(value interface{}) error { for _, disallowed := range disallowedValues { - if value == disallowed { + // Use reflect to better handle different types + if reflect.DeepEqual(value, disallowed) { return fmt.Errorf("value must not be one of %v", disallowedValues) } } return nil } }📝 Committable suggestion