summaryrefslogtreecommitdiff
path: root/internal/validate
diff options
context:
space:
mode:
Diffstat (limited to 'internal/validate')
-rw-r--r--internal/validate/formvalidation.go19
-rw-r--r--internal/validate/formvalidation_test.go34
2 files changed, 53 insertions, 0 deletions
diff --git a/internal/validate/formvalidation.go b/internal/validate/formvalidation.go
index 32aa1fd1e..e7839d1a3 100644
--- a/internal/validate/formvalidation.go
+++ b/internal/validate/formvalidation.go
@@ -43,6 +43,8 @@ const (
maximumUsernameLength = 64
maximumCustomCSSLength = 5000
maximumEmojiCategoryLength = 64
+ maximumProfileFieldLength = 255
+ maximumProfileFields = 4
)
// NewPassword returns an error if the given password is not sufficiently strong, or nil if it's ok.
@@ -231,3 +233,20 @@ func SiteTerms(t string) error {
func ULID(i string) bool {
return regexes.ULID.MatchString(i)
}
+
+func ProfileFieldsCount(fields []apimodel.UpdateField) error {
+ if length := len(fields); length > maximumProfileFields {
+ return fmt.Errorf("cannot have more than %d profile fields", maximumProfileFields)
+ }
+
+ return nil
+}
+
+func ProfileField(f *string) string {
+ s := []rune(*f)
+ if len(s) > maximumProfileFieldLength {
+ return string(s[:maximumProfileFieldLength]) // trim profile field to maximum allowed length
+ }
+
+ return string(*f)
+}
diff --git a/internal/validate/formvalidation_test.go b/internal/validate/formvalidation_test.go
index 61f505412..f59bbf753 100644
--- a/internal/validate/formvalidation_test.go
+++ b/internal/validate/formvalidation_test.go
@@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/validate"
)
@@ -284,6 +285,39 @@ func (suite *ValidationTestSuite) TestValidateReason() {
}
}
+func (suite *ValidationTestSuite) TestValidateProfileFieldsCount() {
+ noFields := []model.UpdateField{}
+ fewFields := []model.UpdateField{{}, {}}
+ tooManyFields := []model.UpdateField{{}, {}, {}, {}, {}}
+ err := validate.ProfileFieldsCount(tooManyFields)
+ if assert.Error(suite.T(), err) {
+ assert.Equal(suite.T(), errors.New("cannot have more than 4 profile fields"), err)
+ }
+
+ err = validate.ProfileFieldsCount(noFields)
+ assert.NoError(suite.T(), err)
+
+ err = validate.ProfileFieldsCount(fewFields)
+ assert.NoError(suite.T(), err)
+}
+
+func (suite *ValidationTestSuite) TestValidateProfileField() {
+ shortProfileField := "pronouns"
+ tooLongProfileField := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui eleifend massa nunc."
+ trimmedProfileField := "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer eu bibendum elit. Sed ac interdum nisi. Vestibulum vulputate eros quis euismod imperdiet. Nulla sit amet dui sit amet lorem consectetur iaculis. Mauris eget lacinia metus. Curabitur nec dui "
+
+ validated := validate.ProfileField(&shortProfileField)
+ assert.Equal(suite.T(), shortProfileField, validated)
+
+ validated = validate.ProfileField(&tooLongProfileField)
+ assert.Len(suite.T(), validated, 255)
+ assert.Equal(suite.T(), trimmedProfileField, validated)
+
+ validated = validate.ProfileField(&trimmedProfileField)
+ assert.Len(suite.T(), validated, 255)
+ assert.Equal(suite.T(), trimmedProfileField, validated)
+}
+
func TestValidationTestSuite(t *testing.T) {
suite.Run(t, new(ValidationTestSuite))
}