|
|
@@ -1,12 +1,14 @@
|
|
|
package check
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"github.com/go-playground/locales/zh"
|
|
|
ut "github.com/go-playground/universal-translator"
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
|
|
|
"github.com/pkg/errors"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
var validate = validator.New(validator.WithRequiredStructEnabled())
|
|
|
@@ -25,6 +27,30 @@ func init() {
|
|
|
}
|
|
|
|
|
|
translator = trans
|
|
|
+
|
|
|
+ registerCustomTags()
|
|
|
+}
|
|
|
+
|
|
|
+func registerCustomTags() {
|
|
|
+ if err := validate.RegisterValidation("timezero", func(fl validator.FieldLevel) bool {
|
|
|
+ fieldValue := fl.Field()
|
|
|
+
|
|
|
+ if !fieldValue.IsValid() {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ switch value := fieldValue.Interface().(type) {
|
|
|
+ case time.Time:
|
|
|
+ return value.IsZero()
|
|
|
+ case *time.Time:
|
|
|
+ return value.IsZero()
|
|
|
+ default:
|
|
|
+ return fieldValue.IsZero()
|
|
|
+ }
|
|
|
+ }); err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func Struct(obj any, fieldNameMap map[string]string) Result {
|