Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 09:54:24 +01:00

factory instead of parse & validate

This commit is contained in:
Michael Jerger 2023-12-08 18:08:54 +01:00
parent 6fef54ed1c
commit afcc7f0def
2 changed files with 27 additions and 2 deletions

View file

@ -7,6 +7,7 @@ import (
"strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/validation"
)
type Validatable interface { // ToDo: What is the right package for this interface?
@ -40,8 +41,8 @@ func (a ActorID) Validate() []string {
var err = []string{}
if res := validate_is_not_empty(a.schema); res != nil {
err = append(err, strings.Join([]string{res.Error(), "for schema field"}, " "))
if res := validation.ValidateNotEmpty(a.schema, "schema"); res != nil {
err = append(err, res.Error())
}
if res := validate_is_not_empty(a.host); res != nil {
@ -167,3 +168,10 @@ func ParseActorID(validatedURL url.URL, source string) ActorID { // ToDo: Turn t
port: validatedURL.Port(),
}
}
func NewActorId(uri string, source string) (ActorID, error) {
if !validation.IsValidExternalURL(uri) {
return ActorID{}, fmt.Errorf("uri %s is not a valid external url.", uri)
}
return ActorID{}, nil
}

View file

@ -117,3 +117,20 @@ func TestGetHostAndPort(t *testing.T) {
}
}
func TestShouldThrowErrorOnInvalidInput(t *testing.T) {
_, err := NewActorId("", "forgejo")
if err == nil {
t.Errorf("empty input should be invalid.")
}
_, err = NewActorId("http://localhost:3000/api/v1/something", "forgejo")
if err == nil {
t.Errorf("localhost uris are not external")
}
_, err = NewActorId("https://an.other.host/api/v1/activitypub/person-id/1", "forgejo")
if err != nil {
t.Errorf("this uri should be valid")
}
}