summaryrefslogtreecommitdiff
path: root/internal/util/validation.go
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-06-23 16:35:57 +0200
committerLibravatar GitHub <noreply@github.com>2021-06-23 16:35:57 +0200
commit8c9a8533434ec6e159541896d5f43e39303d42e4 (patch)
tree3da3cbf9aa33a160cd076f2b9f8d56396ae3bdfa /internal/util/validation.go
parentOpengraph meta tags (#55) (diff)
downloadgotosocial-8c9a8533434ec6e159541896d5f43e39303d42e4.tar.xz
Instance settings updates (#59)
Allow admins to set instance settings through a PATCH to /api/v1/instance Update templates to reflect some of the new fields
Diffstat (limited to 'internal/util/validation.go')
-rw-r--r--internal/util/validation.go57
1 files changed, 48 insertions, 9 deletions
diff --git a/internal/util/validation.go b/internal/util/validation.go
index d392231bb..446f7a70e 100644
--- a/internal/util/validation.go
+++ b/internal/util/validation.go
@@ -27,6 +27,17 @@ import (
"golang.org/x/text/language"
)
+const (
+ maximumPasswordLength = 64
+ minimumPasswordEntropy = 60 // dictates password strength. See https://github.com/wagslane/go-password-validator
+ minimumReasonLength = 40
+ maximumReasonLength = 500
+ maximumSiteTitleLength = 40
+ maximumShortDescriptionLength = 500
+ maximumDescriptionLength = 5000
+ maximumSiteTermsLength = 5000
+)
+
// ValidateNewPassword returns an error if the given password is not sufficiently strong, or nil if it's ok.
func ValidateNewPassword(password string) error {
if password == "" {
@@ -47,12 +58,8 @@ func ValidateUsername(username string) error {
return errors.New("no username provided")
}
- if len(username) > maximumUsernameLength {
- return fmt.Errorf("username should be no more than %d chars but '%s' was %d", maximumUsernameLength, username, len(username))
- }
-
if !usernameValidationRegex.MatchString(username) {
- return fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores", username)
+ return fmt.Errorf("given username %s was invalid: must contain only lowercase letters, numbers, and underscores, max %d characters", username, maximumUsernameLength)
}
return nil
@@ -65,10 +72,6 @@ func ValidateEmail(email string) error {
return errors.New("no email provided")
}
- if len(email) > maximumEmailLength {
- return fmt.Errorf("email address should be no more than %d chars but '%s' was %d", maximumEmailLength, email, len(email))
- }
-
_, err := mail.ParseAddress(email)
return err
}
@@ -132,3 +135,39 @@ func ValidateEmojiShortcode(shortcode string) error {
}
return nil
}
+
+// ValidateSiteTitle ensures that the given site title is within spec.
+func ValidateSiteTitle(siteTitle string) error {
+ if len(siteTitle) > maximumSiteTitleLength {
+ return fmt.Errorf("site title should be no more than %d chars but given title was %d", maximumSiteTitleLength, len(siteTitle))
+ }
+
+ return nil
+}
+
+// ValidateSiteShortDescription ensures that the given site short description is within spec.
+func ValidateSiteShortDescription(d string) error {
+ if len(d) > maximumShortDescriptionLength {
+ return fmt.Errorf("short description should be no more than %d chars but given description was %d", maximumShortDescriptionLength, len(d))
+ }
+
+ return nil
+}
+
+// ValidateSiteDescription ensures that the given site description is within spec.
+func ValidateSiteDescription(d string) error {
+ if len(d) > maximumDescriptionLength {
+ return fmt.Errorf("description should be no more than %d chars but given description was %d", maximumDescriptionLength, len(d))
+ }
+
+ return nil
+}
+
+// ValidateSiteTerms ensures that the given site terms string is within spec.
+func ValidateSiteTerms(t string) error {
+ if len(t) > maximumSiteTermsLength {
+ return fmt.Errorf("terms should be no more than %d chars but given terms was %d", maximumSiteTermsLength, len(t))
+ }
+
+ return nil
+}