@@ -4,9 +4,10 @@ import (
44 "errors"
55 "github.com/0xJacky/Nginx-UI/internal/logger"
66 "github.com/gin-gonic/gin"
7- val "github.com/go-playground/validator/v10"
7+ "github.com/go-playground/validator/v10"
88 "net/http"
99 "reflect"
10+ "regexp"
1011 "strings"
1112)
1213
@@ -27,7 +28,7 @@ func BindAndValid(c *gin.Context, target interface{}) bool {
2728 if err != nil {
2829 logger .Error ("bind err" , err )
2930
30- var verrs val .ValidationErrors
31+ var verrs validator .ValidationErrors
3132 ok := errors .As (err , & verrs )
3233
3334 if ! ok {
@@ -44,7 +45,7 @@ func BindAndValid(c *gin.Context, target interface{}) bool {
4445 var path []string
4546
4647 namespace := strings .Split (value .StructNamespace (), "." )
47- logger .Debug (t .Name (), namespace )
48+ // logger.Debug(t.Name(), namespace)
4849 if t .Name () != "" && len (namespace ) > 1 {
4950 namespace = namespace [1 :]
5051 }
@@ -67,13 +68,30 @@ func BindAndValid(c *gin.Context, target interface{}) bool {
6768
6869// findField recursively finds the field in a nested struct
6970func getJsonPath (t reflect.Type , fields []string , path * []string ) {
70- f , ok := t .FieldByName (fields [0 ])
71+ field := fields [0 ]
72+ // used in case of array
73+ var index string
74+ if field [len (field )- 1 ] == ']' {
75+ re := regexp .MustCompile (`(\w+)\[(\d+)\]` )
76+ matches := re .FindStringSubmatch (field )
77+
78+ if len (matches ) > 2 {
79+ field = matches [1 ]
80+ index = matches [2 ]
81+ }
82+ }
83+
84+ f , ok := t .FieldByName (field )
7185 if ! ok {
7286 return
7387 }
7488
7589 * path = append (* path , f .Tag .Get ("json" ))
7690
91+ if index != "" {
92+ * path = append (* path , index )
93+ }
94+
7795 if len (fields ) > 1 {
7896 subFields := fields [1 :]
7997 getJsonPath (f .Type , subFields , path )
0 commit comments