-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added user first/last/nick name validation (#42)
* feat: added user first/last/nick name validation * fix: added top level t.Parallel() call --------- Co-authored-by: hrvadl <[email protected]>
- Loading branch information
Showing
4 changed files
with
157 additions
and
2 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
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,7 @@ | ||
package validator | ||
|
||
var DefaultForbiddenList = []string{ | ||
"🇷🇺", | ||
"🪆", | ||
"Russia", | ||
} |
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,29 @@ | ||
package validator | ||
|
||
import "strings" | ||
|
||
func New(forbiddenWords []string) *ByName { | ||
for i, w := range forbiddenWords { | ||
forbiddenWords[i] = strings.ToLower(w) | ||
} | ||
|
||
return &ByName{ | ||
forbiddenWords: forbiddenWords, | ||
} | ||
} | ||
|
||
type ByName struct { | ||
forbiddenWords []string | ||
} | ||
|
||
func (v *ByName) Validate(names ...string) bool { | ||
for _, name := range names { | ||
lowerName := strings.ToLower(name) | ||
for _, word := range v.forbiddenWords { | ||
if strings.Contains(lowerName, word) { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} |
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,84 @@ | ||
package validator | ||
|
||
import "testing" | ||
|
||
func TestValidateByName(t *testing.T) { | ||
t.Parallel() | ||
tc := []struct { | ||
name string | ||
forbiddenWords []string | ||
words []string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Should forbid russian flag emoji", | ||
forbiddenWords: []string{"🇷🇺"}, | ||
words: []string{"🇷🇺"}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "Should forbid russian matryoshka emoji", | ||
forbiddenWords: []string{"🪆"}, | ||
words: []string{"🪆"}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "Should forbid word 'Russia'", | ||
forbiddenWords: []string{"Russia"}, | ||
words: []string{"Russia"}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "Should forbid word 'russia'", | ||
forbiddenWords: []string{"russia"}, | ||
words: []string{"russia"}, | ||
expected: false, | ||
}, | ||
{ | ||
name: "Should allow 'Vadym' name to bypass", | ||
forbiddenWords: []string{"russia"}, | ||
words: []string{"Vadym"}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Should allow 'Ruslan' name to bypass", | ||
forbiddenWords: []string{"russia"}, | ||
words: []string{"Ruslan"}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Should allow anything when forbidden words are empty", | ||
forbiddenWords: []string{}, | ||
words: []string{"russia", "Russia"}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Should allow anything when nil slice of forbidden words are passed", | ||
forbiddenWords: nil, | ||
words: []string{"russia", "Russia"}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Should allow when empty slice of words are passed", | ||
forbiddenWords: []string{"russia"}, | ||
words: []string{}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "Should allow when nil slice of words are passed", | ||
forbiddenWords: []string{"russia"}, | ||
words: nil, | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tc { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
v := New(tt.forbiddenWords) | ||
if got := v.Validate(tt.words...); got != tt.expected { | ||
t.Errorf("Expected to get: %v, but got: %v", tt.expected, got) | ||
} | ||
}) | ||
} | ||
} |