mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 04:05:42 +01:00
experiment on generalization
This commit is contained in:
parent
75cc5b900d
commit
6e4467d49d
2 changed files with 64 additions and 32 deletions
|
@ -12,11 +12,8 @@ import (
|
||||||
"code.gitea.io/gitea/modules/validation"
|
"code.gitea.io/gitea/modules/validation"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Validateable interface { // ToDo: Add this to validate helpers
|
|
||||||
Validate() []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type ActorId struct {
|
type ActorId struct {
|
||||||
|
validation.Validateable
|
||||||
Id string
|
Id string
|
||||||
Source string
|
Source string
|
||||||
Schema string
|
Schema string
|
||||||
|
@ -125,9 +122,7 @@ func (id PersonId) HostSuffix() string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Validate collects error strings in a slice and returns this
|
||||||
Validate collects error strings in a slice and returns this
|
|
||||||
*/
|
|
||||||
func (value ActorId) Validate() []string {
|
func (value ActorId) Validate() []string {
|
||||||
var result = []string{}
|
var result = []string{}
|
||||||
result = append(result, validation.ValidateNotEmpty(value.Id, "userId")...)
|
result = append(result, validation.ValidateNotEmpty(value.Id, "userId")...)
|
||||||
|
@ -145,39 +140,26 @@ func (value ActorId) Validate() []string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move valid-parts to valid package
|
func (value PersonId) Validate() []string {
|
||||||
/*
|
var result = value.ActorId.Validate()
|
||||||
IsValid concatenates the error messages with newlines and returns them if there are any
|
switch value.Source {
|
||||||
*/
|
|
||||||
func (a ActorId) IsValid() (bool, error) {
|
|
||||||
if err := a.Validate(); len(err) > 0 {
|
|
||||||
errString := strings.Join(err, "\n")
|
|
||||||
return false, fmt.Errorf(errString)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a PersonId) IsValid() (bool, error) {
|
|
||||||
switch a.Source {
|
|
||||||
case "forgejo", "gitea":
|
case "forgejo", "gitea":
|
||||||
if strings.ToLower(a.Path) != "api/v1/activitypub/user-id" && strings.ToLower(a.Path) != "api/activitypub/user-id" {
|
if strings.ToLower(value.Path) != "api/v1/activitypub/user-id" && strings.ToLower(value.Path) != "api/activitypub/user-id" {
|
||||||
err := fmt.Errorf("path: %q has to be an api path", a.Path)
|
result = append(result, fmt.Sprintf("path: %q has to be an api path", value.Path))
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true, nil
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a RepositoryId) IsValid() (bool, error) {
|
func (value RepositoryId) Validate() []string {
|
||||||
switch a.Source {
|
var result = value.ActorId.Validate()
|
||||||
|
switch value.Source {
|
||||||
case "forgejo", "gitea":
|
case "forgejo", "gitea":
|
||||||
if strings.ToLower(a.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(a.Path) != "api/activitypub/repository-id" {
|
if strings.ToLower(value.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(value.Path) != "api/activitypub/repository-id" {
|
||||||
err := fmt.Errorf("path: %q has to be an api path", a.Path)
|
result = append(result, fmt.Sprintf("path: %q has to be an api path", value.Path))
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true, nil
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func containsEmptyString(ar []string) bool {
|
func containsEmptyString(ar []string) bool {
|
||||||
|
@ -198,3 +180,21 @@ func removeEmptyStrings(ls []string) []string {
|
||||||
}
|
}
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a RepositoryId) IsValid() (bool, error) {
|
||||||
|
if err := a.Validate(); len(err) > 0 {
|
||||||
|
errString := strings.Join(err, "\n")
|
||||||
|
return false, fmt.Errorf(errString)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a PersonId) IsValid() (bool, error) {
|
||||||
|
if err := a.Validate(); len(err) > 0 {
|
||||||
|
errString := strings.Join(err, "\n")
|
||||||
|
return false, fmt.Errorf(errString)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
32
modules/validation/validateable.go
Normal file
32
modules/validation/validateable.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright 2023 The forgejo Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package validation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ValidationFunctions interface {
|
||||||
|
Validate() []string
|
||||||
|
IsValid() (bool, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Validateable struct {
|
||||||
|
ValidationFunctions
|
||||||
|
}
|
||||||
|
|
||||||
|
func IsValid(v any) (bool, error) {
|
||||||
|
if err := Validate(v); len(err) > 0 {
|
||||||
|
errString := strings.Join(err, "\n")
|
||||||
|
return false, fmt.Errorf(errString)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Validate(v any) []string {
|
||||||
|
var result = []string{}
|
||||||
|
return result
|
||||||
|
}
|
Loading…
Reference in a new issue