mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-11 04:11:18 +01:00
factory instead of parse & validate
This commit is contained in:
parent
6fef54ed1c
commit
afcc7f0def
2 changed files with 27 additions and 2 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/validation"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Validatable interface { // ToDo: What is the right package for this interface?
|
type Validatable interface { // ToDo: What is the right package for this interface?
|
||||||
|
@ -40,8 +41,8 @@ func (a ActorID) Validate() []string {
|
||||||
|
|
||||||
var err = []string{}
|
var err = []string{}
|
||||||
|
|
||||||
if res := validate_is_not_empty(a.schema); res != nil {
|
if res := validation.ValidateNotEmpty(a.schema, "schema"); res != nil {
|
||||||
err = append(err, strings.Join([]string{res.Error(), "for schema field"}, " "))
|
err = append(err, res.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if res := validate_is_not_empty(a.host); res != nil {
|
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(),
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue