feat: update AIConfig route handler and add validation utility function with tests

This commit is contained in:
mlogclub
2026-05-24 19:57:36 +08:00
parent b1bc1580d9
commit 467759d547
4 changed files with 53 additions and 4 deletions
+19 -1
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"net/http"
"reflect"
"strconv"
"strings"
"time"
@@ -54,7 +55,7 @@ func ReadForm(ctx *gin.Context, obj any) error {
if err := decoder.Decode(obj, values); err != nil {
return err
}
return validate.Struct(obj)
return validateStruct(obj)
}
func ReadJSON(ctx *gin.Context, obj any) error {
@@ -64,6 +65,23 @@ func ReadJSON(ctx *gin.Context, obj any) error {
if err := ctx.ShouldBindJSON(obj); err != nil {
return err
}
return validateStruct(obj)
}
func validateStruct(obj any) error {
if obj == nil {
return nil
}
value := reflect.ValueOf(obj)
for value.Kind() == reflect.Pointer {
if value.IsNil() {
return validate.Struct(obj)
}
value = value.Elem()
}
if value.Kind() != reflect.Struct {
return nil
}
return validate.Struct(obj)
}