feat(app): 🦺 add new validators

master
Xeronith 2023-07-05 18:31:04 +03:30
rodzic d28e95bccd
commit a107dcd600
4 zmienionych plików z 162 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,11 @@
package validators
import (
"regexp"
. "github.com/reiver/greatape/components/constants"
)
func EmailIsValid(input string) bool {
return regexp.MustCompile(EMAIL).MatchString(input)
}

Wyświetl plik

@ -0,0 +1,84 @@
package validators_test
import (
"testing"
"github.com/reiver/greatape/app/validators"
)
func TestEmailValidator(test *testing.T) {
type arguments struct {
email string
}
testCases := []struct {
name string
expectation bool
arguments arguments
}{
{
"Case1",
false,
arguments{
email: "",
},
},
{
"Case2",
false,
arguments{
email: "user",
},
},
{
"Case3",
true,
arguments{
email: "user@domain.com",
},
},
{
"Case4",
true,
arguments{
email: "user+plus@gmail.com",
},
},
{
"Case5",
true,
arguments{
email: "susan235@gmail.com",
},
},
{
"Case6",
true,
arguments{
email: "new_user@icloud.com",
},
},
{
"Case7",
true,
arguments{
email: "someone@somewhere.co.uk",
},
},
{
"Case8",
true,
arguments{
email: "north.star@space.social",
},
},
}
for _, testCase := range testCases {
test.Run(testCase.name, func(test *testing.T) {
if result := validators.EmailIsValid(testCase.arguments.email); result != testCase.expectation {
test.Errorf("EmailIsValid() = %v, expected %v", result, testCase.expectation)
}
})
}
}

Wyświetl plik

@ -0,0 +1,11 @@
package validators
import (
"regexp"
. "github.com/reiver/greatape/components/constants"
)
func WebfingerIsValid(webfinger string) bool {
return regexp.MustCompile(WEBFINGER).MatchString(webfinger)
}

Wyświetl plik

@ -0,0 +1,56 @@
package validators_test
import (
"testing"
"github.com/reiver/greatape/app/validators"
)
func TestWebfingerValidator(test *testing.T) {
type arguments struct {
webfinger string
}
testCases := []struct {
name string
expectation bool
arguments arguments
}{
{
"Case1",
false,
arguments{
webfinger: "somebody@somewhere.xyz",
},
},
{
"Case2",
true,
arguments{
webfinger: "acct:somebody@somewhere.xyz",
},
},
{
"Case3",
true,
arguments{
webfinger: "acct:user@sub.domain.com",
},
},
{
"Case4",
false,
arguments{
webfinger: "acct:user",
},
},
}
for _, testCase := range testCases {
test.Run(testCase.name, func(test *testing.T) {
if result := validators.WebfingerIsValid(testCase.arguments.webfinger); result != testCase.expectation {
test.Errorf("WebfingerIsValid() = %v, expected %v", result, testCase.expectation)
}
})
}
}