mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-23 06:43:34 +01:00
WIP Generic IsValid for *Id structs
This commit is contained in:
parent
e69e5df089
commit
2e031a9763
3 changed files with 27 additions and 6 deletions
|
@ -12,6 +12,11 @@ import (
|
|||
"code.gitea.io/gitea/modules/validation"
|
||||
)
|
||||
|
||||
type Validateables interface {
|
||||
validation.Validateable
|
||||
ActorId | PersonId | RepositoryId
|
||||
}
|
||||
|
||||
type ActorId struct {
|
||||
validation.Validateable
|
||||
Id string
|
||||
|
@ -51,7 +56,7 @@ func newActorId(validatedUri *url.URL, source string) (ActorId, error) {
|
|||
result.Port = validatedUri.Port()
|
||||
result.UnvalidatedInput = validatedUri.String()
|
||||
|
||||
if valid, err := result.IsValid(); !valid {
|
||||
if valid, err := IsValid(result); !valid {
|
||||
return ActorId{}, err
|
||||
}
|
||||
|
||||
|
@ -75,7 +80,7 @@ func NewPersonId(uri string, source string) (PersonId, error) {
|
|||
|
||||
// validate Person specific path
|
||||
personId := PersonId{actorId}
|
||||
if valid, outcome := personId.IsValid(); !valid {
|
||||
if valid, outcome := IsValid(personId); !valid {
|
||||
return PersonId{}, outcome
|
||||
}
|
||||
|
||||
|
@ -100,7 +105,7 @@ func NewRepositoryId(uri string, source string) (RepositoryId, error) {
|
|||
|
||||
// validate Person specific path
|
||||
repoId := RepositoryId{actorId}
|
||||
if valid, outcome := repoId.IsValid(); !valid {
|
||||
if valid, outcome := IsValid(repoId); !valid {
|
||||
return RepositoryId{}, outcome
|
||||
}
|
||||
|
||||
|
@ -155,7 +160,7 @@ func (value PersonId) Validate() []string {
|
|||
switch value.Source {
|
||||
case "forgejo", "gitea":
|
||||
if strings.ToLower(value.Path) != "api/v1/activitypub/user-id" && strings.ToLower(value.Path) != "api/activitypub/user-id" {
|
||||
result = append(result, fmt.Sprintf("path: %q has to be an api path", value.Path))
|
||||
result = append(result, fmt.Sprintf("path: %q has to be a person specific api path", value.Path))
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
@ -166,7 +171,7 @@ func (value RepositoryId) Validate() []string {
|
|||
switch value.Source {
|
||||
case "forgejo", "gitea":
|
||||
if strings.ToLower(value.Path) != "api/v1/activitypub/repository-id" && strings.ToLower(value.Path) != "api/activitypub/repository-id" {
|
||||
result = append(result, fmt.Sprintf("path: %q has to be an api path", value.Path))
|
||||
result = append(result, fmt.Sprintf("path: %q has to be a repo specific api path", value.Path))
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
@ -191,6 +196,15 @@ func removeEmptyStrings(ls []string) []string {
|
|||
return rs
|
||||
}
|
||||
|
||||
func IsValid[T Validateables](value T) (bool, error) {
|
||||
if err := value.Validate(); len(err) > 0 {
|
||||
errString := strings.Join(err, "\n")
|
||||
return false, fmt.Errorf(errString)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (a RepositoryId) IsValid() (bool, error) {
|
||||
if err := a.Validate(); len(err) > 0 {
|
||||
errString := strings.Join(err, "\n")
|
||||
|
@ -208,3 +222,4 @@ func (a PersonId) IsValid() (bool, error) {
|
|||
|
||||
return true, nil
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -99,7 +99,7 @@ func TestPersonIdValidation(t *testing.T) {
|
|||
sut.Host = "an.other.host"
|
||||
sut.Port = ""
|
||||
sut.UnvalidatedInput = "https://an.other.host/path/1"
|
||||
if _, err := sut.IsValid(); err.Error() != "path: \"path\" has to be an api path" {
|
||||
if _, err := IsValid(sut); err.Error() != "path: \"path\" has to be a person specific api path" {
|
||||
t.Errorf("validation error expected but was: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
/*
|
||||
type ValidationFunctions interface {
|
||||
Validate() []string
|
||||
IsValid() (bool, error)
|
||||
|
@ -16,6 +17,11 @@ type ValidationFunctions interface {
|
|||
type Validateable struct {
|
||||
ValidationFunctions
|
||||
}
|
||||
*/
|
||||
|
||||
type Validateable interface {
|
||||
Validate() []string
|
||||
}
|
||||
|
||||
func IsValid(v any) (bool, error) {
|
||||
if err := Validate(v); len(err) > 0 {
|
||||
|
|
Loading…
Reference in a new issue