1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

make validateNotEmpty more generic

This commit is contained in:
Clemens 2024-01-12 14:57:22 +01:00 committed by Michael Jerger
parent c67be3b668
commit 9c37272ee9

View file

@ -7,6 +7,8 @@ import (
"fmt"
"strings"
"unicode/utf8"
"code.gitea.io/gitea/modules/timeutil"
)
type Validateable interface {
@ -22,12 +24,26 @@ func IsValid(v Validateable) (bool, error) {
return true, nil
}
func ValidateNotEmpty(value string, fieldName string) []string {
if value == "" {
return []string{fmt.Sprintf("Field %v may not be empty", fieldName)}
func ValidateNotEmpty(value any, fieldName string) []string {
isValid := true
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{fmt.Sprintf("Field %v may not be empty", fieldName)}
}
func ValidateMaxLen(value string, maxLen int, fieldName string) []string {
if utf8.RuneCountInString(value) > maxLen {