diff options
author | 2024-05-31 03:55:56 -0700 | |
---|---|---|
committer | 2024-05-31 12:55:56 +0200 | |
commit | 61a8d362557c1787d534024ed2f14e999b785cc3 (patch) | |
tree | f0ace4432170c0c88afa3233c2c327c808b7b92d /internal/validate/formvalidation.go | |
parent | [chore] little startup tweaks (#2941) (diff) | |
download | gotosocial-61a8d362557c1787d534024ed2f14e999b785cc3.tar.xz |
[feature] Implement Filter API v2 (#2936)
* Use correct entity name
* We support server-side filters now
* Document filter v1 methods that can throw a 409
* Validate v1 filter phrase as filter title
* Always check v1 filter API status codes in tests
* Document keyword minimum requirement on filter API v1
* Make it possible to specify filter keyword update columns per filter keyword
* Implement v2 filter API
* Fix lint and tests
* Update Swagger spec
* Fix filter update test
* Update Swagger spec *correctly*
* Update actual files Swagger spec was generated from
* Remove keywords_attributes and statuses_attributes
* Add test for serialization of empty filter
* More helpful messages when object is owned by wrong account
Diffstat (limited to 'internal/validate/formvalidation.go')
-rw-r--r-- | internal/validate/formvalidation.go | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/internal/validate/formvalidation.go b/internal/validate/formvalidation.go index 3c16dd86e..e8ec3380b 100644 --- a/internal/validate/formvalidation.go +++ b/internal/validate/formvalidation.go @@ -45,6 +45,7 @@ const ( maximumProfileFields = 6 maximumListTitleLength = 200 maximumFilterKeywordLength = 40 + maximumFilterTitleLength = 200 ) // Password returns a helpful error if the given password @@ -242,9 +243,16 @@ func SiteTerms(t string) error { return nil } -// ULID returns true if the passed string is a valid ULID. -func ULID(i string) bool { - return regexes.ULID.MatchString(i) +// ULID returns an error if the passed string is not a valid ULID. +// The name param is used to form error messages. +func ULID(i string, name string) error { + if i == "" { + return fmt.Errorf("%s must be provided", name) + } + if !regexes.ULID.MatchString(i) { + return fmt.Errorf("%s didn't match the expected ULID format for an ID (26 characters from the set 0123456789ABCDEFGHJKMNPQRSTVWXYZ)", name) + } + return nil } // ProfileFields validates the length of provided fields slice, @@ -308,7 +316,7 @@ func MarkerName(name string) error { return fmt.Errorf("marker timeline name '%s' was not recognized, valid options are '%s', '%s'", name, apimodel.MarkerNameHome, apimodel.MarkerNameNotifications) } -// FilterKeyword validates the title of a new or updated List. +// FilterKeyword validates a filter keyword. func FilterKeyword(keyword string) error { if keyword == "" { return fmt.Errorf("filter keyword must be provided, and must be no more than %d chars", maximumFilterKeywordLength) @@ -321,6 +329,19 @@ func FilterKeyword(keyword string) error { return nil } +// FilterTitle validates the title of a new or updated filter. +func FilterTitle(title string) error { + if title == "" { + return fmt.Errorf("filter title must be provided, and must be no more than %d chars", maximumFilterTitleLength) + } + + if length := len([]rune(title)); length > maximumFilterTitleLength { + return fmt.Errorf("filter title length must be no more than %d chars, provided title was %d chars", maximumFilterTitleLength, length) + } + + return nil +} + // FilterContexts validates the context of a new or updated filter. func FilterContexts(contexts []apimodel.FilterContext) error { if len(contexts) == 0 { @@ -349,6 +370,20 @@ func FilterContexts(contexts []apimodel.FilterContext) error { return nil } +func FilterAction(action apimodel.FilterAction) error { + switch action { + case apimodel.FilterActionWarn, + apimodel.FilterActionHide: + return nil + } + return fmt.Errorf( + "filter action '%s' was not recognized, valid options are '%s', '%s'", + action, + apimodel.FilterActionWarn, + apimodel.FilterActionHide, + ) +} + // CreateAccount checks through all the prerequisites for // creating a new account, according to the provided form. // If the account isn't eligible, an error will be returned. |