1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

refactor validation

This commit is contained in:
Michael Jerger 2024-05-01 15:02:27 +02:00
parent 4b2802a6ba
commit be6e6eb96a
4 changed files with 7 additions and 7 deletions

View file

@ -45,7 +45,7 @@ func (like ForgeLike) IsNewer(compareTo time.Time) bool {
func (like ForgeLike) Validate() []string { func (like ForgeLike) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...) result = append(result, validation.ValidateNotEmpty(string(like.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"})...) result = append(result, validation.ValidateOneOf(string(like.Type), []any{"Like"}, "type")...)
if like.Actor == nil { if like.Actor == nil {
result = append(result, "Actor my not be nil.") result = append(result, "Actor my not be nil.")
} else { } else {

View file

@ -108,7 +108,7 @@ func (id PersonID) HostSuffix() string {
func (id PersonID) Validate() []string { func (id PersonID) Validate() []string {
result := id.ActorID.Validate() result := id.ActorID.Validate()
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...) result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...) result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"}, "Source")...)
switch id.Source { switch id.Source {
case "forgejo", "gitea": case "forgejo", "gitea":
if strings.ToLower(id.Path) != "api/v1/activitypub/user-id" && strings.ToLower(id.Path) != "api/activitypub/user-id" { if strings.ToLower(id.Path) != "api/v1/activitypub/user-id" && strings.ToLower(id.Path) != "api/activitypub/user-id" {
@ -147,7 +147,7 @@ func NewRepositoryID(uri, source string) (RepositoryID, error) {
func (id RepositoryID) Validate() []string { func (id RepositoryID) Validate() []string {
result := id.ActorID.Validate() result := id.ActorID.Validate()
result = append(result, validation.ValidateNotEmpty(id.Source, "source")...) result = append(result, validation.ValidateNotEmpty(id.Source, "source")...)
result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"})...) result = append(result, validation.ValidateOneOf(id.Source, []any{"forgejo", "gitea"}, "Source")...)
switch id.Source { switch id.Source {
case "forgejo", "gitea": case "forgejo", "gitea":
if strings.ToLower(id.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(id.Path) != "api/activitypub/repository-id" { if strings.ToLower(id.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(id.Path) != "api/activitypub/repository-id" {
@ -219,7 +219,7 @@ func (s *ForgePerson) UnmarshalJSON(data []byte) error {
func (s ForgePerson) Validate() []string { func (s ForgePerson) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...) result = append(result, validation.ValidateNotEmpty(string(s.Type), "type")...)
result = append(result, validation.ValidateOneOf(string(s.Type), []any{string(ap.PersonType)})...) result = append(result, validation.ValidateOneOf(string(s.Type), []any{string(ap.PersonType)}, "Type")...)
result = append(result, validation.ValidateNotEmpty(s.PreferredUsername.String(), "preferredUsername")...) result = append(result, validation.ValidateNotEmpty(s.PreferredUsername.String(), "preferredUsername")...)
return result return result

View file

@ -73,7 +73,7 @@ func (node NodeInfoWellKnown) Validate() []string {
result = append(result, "Href has to be absolute") result = append(result, "Href has to be absolute")
} }
result = append(result, validation.ValidateOneOf(parsedURL.Scheme, []any{"http", "https"})...) result = append(result, validation.ValidateOneOf(parsedURL.Scheme, []any{"http", "https"}, "parsedURL.Scheme")...)
if parsedURL.RawQuery != "" { if parsedURL.RawQuery != "" {
result = append(result, "Href may not contain query") result = append(result, "Href may not contain query")
@ -129,7 +129,7 @@ func NewNodeInfo(body []byte) (NodeInfo, error) {
func (node NodeInfo) Validate() []string { func (node NodeInfo) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(string(node.SoftwareName), "source")...) result = append(result, validation.ValidateNotEmpty(string(node.SoftwareName), "source")...)
result = append(result, validation.ValidateOneOf(node.SoftwareName, KnownSourceTypes)...) result = append(result, validation.ValidateOneOf(node.SoftwareName, KnownSourceTypes, "node.SoftwareName")...)
return result return result
} }

View file

@ -57,7 +57,7 @@ func ValidateMaxLen(value string, maxLen int, name string) []string {
return []string{} return []string{}
} }
func ValidateOneOf(value any, allowed []any) []string { func ValidateOneOf(value any, allowed []any, name string) []string {
for _, allowedElem := range allowed { for _, allowedElem := range allowed {
if value == allowedElem { if value == allowedElem {
return []string{} return []string{}