aboutsummaryrefslogtreecommitdiff
path: root/internal/player/validate.go
blob: d836089124ae6c62cb37fd8ddb69a38aa083ca28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package player

import (
	"fmt"
	"regexp"
	"unicode"
)

var validNameRE = regexp.MustCompile(`^[A-Za-z0-9 ]{1,30}$`)

func ValidName(name string) error {
	if !validNameRE.MatchString(name) {
		return fmt.Errorf("name must be 1-30 alphanumeric characters or spaces")
	}
	return nil
}

func ValidPassword(pw string) error {
	if len(pw) > 128 {
		return fmt.Errorf("Password too long.")
	}
	return nil
}

func StripControlCharacters(input string) string {
	runes := make([]rune, 0, len(input))
	for _, r := range input {
		if r == '\n' || r == '\t' {
			continue
		}
		if unicode.IsControl(r) {
			continue
		}
		runes = append(runes, r)
	}
	return string(runes)
}