diff options
Diffstat (limited to 'internal/validate/formvalidation.go')
-rw-r--r-- | internal/validate/formvalidation.go | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/internal/validate/formvalidation.go b/internal/validate/formvalidation.go index dd3925572..20d4aa782 100644 --- a/internal/validate/formvalidation.go +++ b/internal/validate/formvalidation.go @@ -25,6 +25,7 @@ import ( apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/config" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/regexes" pwv "github.com/wagslane/go-password-validator" "golang.org/x/text/language" @@ -43,7 +44,7 @@ const ( maximumCustomCSSLength = 5000 maximumEmojiCategoryLength = 64 maximumProfileFieldLength = 255 - maximumProfileFields = 4 + maximumProfileFields = 6 ) // NewPassword returns an error if the given password is not sufficiently strong, or nil if it's ok. @@ -233,19 +234,26 @@ func ULID(i string) bool { return regexes.ULID.MatchString(i) } -func ProfileFieldsCount(fields []apimodel.UpdateField) error { - if length := len(fields); length > maximumProfileFields { +// ProfileFields validates the length of provided fields slice, +// and also iterates through the fields and trims each name + value +// to maximumProfileFieldLength, if they were above. +func ProfileFields(fields []*gtsmodel.Field) error { + if len(fields) > 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 + // Trim each field name + value to maximum allowed length. + for _, field := range fields { + n := []rune(field.Name) + if len(n) > maximumProfileFieldLength { + field.Name = string(n[:maximumProfileFieldLength]) + } + + v := []rune(field.Value) + if len(v) > maximumProfileFieldLength { + field.Value = string(v[:maximumProfileFieldLength]) + } } - return string(*f) + return nil } |