make validateNotEmpty more generic
This commit is contained in:
parent
c67be3b668
commit
9c37272ee9
1 changed files with 20 additions and 4 deletions
|
@ -7,6 +7,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Validateable interface {
|
type Validateable interface {
|
||||||
|
@ -22,11 +24,25 @@ func IsValid(v Validateable) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateNotEmpty(value string, fieldName string) []string {
|
func ValidateNotEmpty(value any, fieldName string) []string {
|
||||||
if value == "" {
|
isValid := true
|
||||||
return []string{fmt.Sprintf("Field %v may not be empty", fieldName)}
|
switch v := value.(type) {
|
||||||
|
case string:
|
||||||
|
if v == "" {
|
||||||
|
isValid = false
|
||||||
}
|
}
|
||||||
|
case timeutil.TimeStamp:
|
||||||
|
if v.IsZero() {
|
||||||
|
isValid = false
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
isValid = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if isValid {
|
||||||
return []string{}
|
return []string{}
|
||||||
|
}
|
||||||
|
return []string{fmt.Sprintf("Field %v may not be empty", fieldName)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidateMaxLen(value string, maxLen int, fieldName string) []string {
|
func ValidateMaxLen(value string, maxLen int, fieldName string) []string {
|
||||||
|
|
Loading…
Reference in a new issue