mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-14 05:41:27 +01:00
internal refactoring
This commit is contained in:
parent
6c7cff4f16
commit
4b2802a6ba
2 changed files with 27 additions and 4 deletions
|
@ -25,7 +25,7 @@ func IsValid(v Validateable) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateNotEmpty(value any, fieldName string) []string {
|
func ValidateNotEmpty(value any, name string) []string {
|
||||||
isValid := true
|
isValid := true
|
||||||
switch v := value.(type) {
|
switch v := value.(type) {
|
||||||
case string:
|
case string:
|
||||||
|
@ -47,12 +47,12 @@ func ValidateNotEmpty(value any, fieldName string) []string {
|
||||||
if isValid {
|
if isValid {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
return []string{fmt.Sprintf("Field %v should not be empty", fieldName)}
|
return []string{fmt.Sprintf("%v should not be empty", name)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateMaxLen(value string, maxLen int, fieldName string) []string {
|
func ValidateMaxLen(value string, maxLen int, name string) []string {
|
||||||
if utf8.RuneCountInString(value) > maxLen {
|
if utf8.RuneCountInString(value) > maxLen {
|
||||||
return []string{fmt.Sprintf("Value in field %v was longer than %v", fieldName, maxLen)}
|
return []string{fmt.Sprintf("Value %v was longer than %v", name, maxLen)}
|
||||||
}
|
}
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,29 @@ import (
|
||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Sut struct {
|
||||||
|
valid bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sut Sut) Validate() []string {
|
||||||
|
if sut.valid {
|
||||||
|
return []string{}
|
||||||
|
} else {
|
||||||
|
return []string{"invalid"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_IsValid(t *testing.T) {
|
||||||
|
sut := Sut{valid: true}
|
||||||
|
if res, _ := IsValid(sut); !res {
|
||||||
|
t.Errorf("sut expected to be valid: %v\n", sut.Validate())
|
||||||
|
}
|
||||||
|
sut = Sut{valid: false}
|
||||||
|
if res, _ := IsValid(sut); res {
|
||||||
|
t.Errorf("sut expected to be invalid: %v\n", sut.Validate())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_ValidateNotEmpty_ForString(t *testing.T) {
|
func Test_ValidateNotEmpty_ForString(t *testing.T) {
|
||||||
sut := ""
|
sut := ""
|
||||||
if len(ValidateNotEmpty(sut, "dummyField")) == 0 {
|
if len(ValidateNotEmpty(sut, "dummyField")) == 0 {
|
||||||
|
|
Loading…
Reference in a new issue