Added arn to the main repository

This commit is contained in:
2019-06-03 18:32:43 +09:00
parent cf258573a8
commit 29a48d94a5
465 changed files with 15968 additions and 288 deletions

68
arn/validate/Validate.go Normal file
View File

@ -0,0 +1,68 @@
package validate
import (
"net/url"
"regexp"
"strings"
"time"
"github.com/animenotifier/notify.moe/arn/autocorrect"
)
const (
// DateFormat is the format used for short dates that don't include the time.
DateFormat = "2006-01-02"
// DateTimeFormat is the format used for long dates that include the time.
DateTimeFormat = time.RFC3339
)
var (
discordNickRegex = regexp.MustCompile(`^([^#]{2,32})#(\d{4})$`)
)
// Nick tests if the given nickname is valid.
func Nick(nick string) bool {
if len(nick) < 2 {
return false
}
return nick == autocorrect.UserNick(nick)
}
// DiscordNick tests if the given Discord nickname is valid.
func DiscordNick(nick string) bool {
return discordNickRegex.MatchString(nick)
}
// DateTime tells you whether the datetime is valid.
func DateTime(date string) bool {
if date == "" || strings.HasPrefix(date, "0001") {
return false
}
_, err := time.Parse(DateTimeFormat, date)
return err == nil
}
// Date tells you whether the datetime is valid.
func Date(date string) bool {
if date == "" || strings.HasPrefix(date, "0001") {
return false
}
_, err := time.Parse(DateFormat, date)
return err == nil
}
// Email tests if the given email address is valid.
func Email(email string) bool {
// TODO: Add email check
return email != ""
}
// URI validates a URI.
func URI(uri string) bool {
_, err := url.ParseRequestURI(uri)
return err == nil
}

View File

@ -0,0 +1,49 @@
package validate_test
import (
"testing"
"github.com/animenotifier/notify.moe/arn/validate"
"github.com/stretchr/testify/assert"
)
func TestIsValidNick(t *testing.T) {
// Invalid nicknames
assert.False(t, validate.Nick(""))
assert.False(t, validate.Nick("A"))
assert.False(t, validate.Nick("AB CD"))
assert.False(t, validate.Nick("A123"))
assert.False(t, validate.Nick("A!§$%&/()=?`"))
assert.False(t, validate.Nick("__"))
assert.False(t, validate.Nick("Tsun.Dere"))
assert.False(t, validate.Nick("Tsun Dere"))
assert.False(t, validate.Nick("さとう"))
// Valid nicknames
assert.True(t, validate.Nick("Tsundere"))
assert.True(t, validate.Nick("TsunDere"))
assert.True(t, validate.Nick("Tsun_Dere"))
assert.True(t, validate.Nick("Akyoto"))
}
func TestIsValidEmail(t *testing.T) {
assert.False(t, validate.Email(""))
assert.True(t, validate.Email("support@notify.moe"))
}
func TestIsValidDate(t *testing.T) {
assert.False(t, validate.DateTime(""))
assert.False(t, validate.DateTime("0001-01-01T01:01:00Z"))
assert.False(t, validate.DateTime("292277026596-12-04T15:30:07Z"))
assert.True(t, validate.DateTime("2017-03-09T10:25:00Z"))
}
func TestIsValidURI(t *testing.T) {
assert.False(t, validate.URI(""))
assert.False(t, validate.URI("a"))
assert.False(t, validate.URI("google.com"))
assert.True(t, validate.URI("https://google.com"))
assert.True(t, validate.URI("https://google.com/"))
assert.True(t, validate.URI("https://google.com/images"))
assert.True(t, validate.URI("https://google.com/images/"))
}