From 9c37272ee9493eba835809f21384da51f97b8dd1 Mon Sep 17 00:00:00 2001 From: Clemens Date: Fri, 12 Jan 2024 14:57:22 +0100 Subject: [PATCH] make validateNotEmpty more generic --- modules/validation/validateable.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/modules/validation/validateable.go b/modules/validation/validateable.go index 212e79352c..27317bdd91 100644 --- a/modules/validation/validateable.go +++ b/modules/validation/validateable.go @@ -7,6 +7,8 @@ import ( "fmt" "strings" "unicode/utf8" + + "code.gitea.io/gitea/modules/timeutil" ) type Validateable interface { @@ -22,11 +24,25 @@ 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 } - return []string{} + + if isValid { + return []string{} + } + return []string{fmt.Sprintf("Field %v may not be empty", fieldName)} } func ValidateMaxLen(value string, maxLen int, fieldName string) []string {