@@ -32,8 +32,22 @@ func GetValidationsFromStruct(in any, tagType string) ([]model.Validation, error
3232 continue
3333 }
3434
35- if len (validation .Requirement ) > 0 {
36- validations = append (validations , * validation )
35+ isInline := false
36+ if len (fieldType .Tag .Get ("json" )) > 0 {
37+ parts := strings .Split (fieldType .Tag .Get ("json" ), "," )
38+ for _ , part := range parts [1 :] {
39+ if part == "inline" {
40+ isInline = true
41+ }
42+ }
43+ }
44+
45+ if isInline {
46+ validations = append (validations , validation .InnerValidation ... )
47+ } else {
48+ if len (validation .Requirement ) > 0 || len (validation .InnerValidation ) > 0 {
49+ validations = append (validations , * validation )
50+ }
3751 }
3852 }
3953 return validations , nil
@@ -46,11 +60,16 @@ func GetValidationFromStructField(tagType string, fieldValue reflect.Value, fiel
4660 validation := & model.Validation {}
4761 validation .Key = fieldType .Name
4862 if len (fieldType .Tag .Get ("json" )) > 0 {
49- jsonKey := fieldType .Tag .Get ("json" )
50- // Split on comma to handle omitempty and other options
51- jsonKey = strings .Split (jsonKey , "," )[0 ]
52- if jsonKey != "-" {
53- validation .Key = jsonKey
63+ jsonKeyFull := fieldType .Tag .Get ("json" )
64+ jsonKeyParts := strings .Split (jsonKeyFull , "," )
65+ jsonKeyPrefix := jsonKeyParts [0 ]
66+ if jsonKeyPrefix != "-" && jsonKeyPrefix != "" {
67+ validation .Key = jsonKeyPrefix
68+ }
69+ for _ , part := range jsonKeyParts [1 :] {
70+ if part == "omitempty" {
71+ validation .OmitEmpty = true
72+ }
5473 }
5574 }
5675 validation .Type = model .ReflectKindToValidatorType (fieldValue .Type ().Kind ())
@@ -92,8 +111,12 @@ func GetValidationFromStructField(tagType string, fieldValue reflect.Value, fiel
92111 return nil , fmt .Errorf ("error getting inner validation from array: %v" , err )
93112 }
94113 validation .InnerValidation = append (validation .InnerValidation , innerValidation ... )
95- } else if helper .IsStruct (fieldValue .Interface ()) {
96- innerStruct := reflect .New (fieldValue .Type ()).Interface ()
114+ } else if helper .IsStruct (fieldValue .Interface ()) || (fieldValue .Type ().Kind () == reflect .Ptr && fieldValue .Type ().Elem ().Kind () == reflect .Struct ) {
115+ fieldTypeElem := fieldValue .Type ()
116+ if fieldTypeElem .Kind () == reflect .Ptr {
117+ fieldTypeElem = fieldTypeElem .Elem ()
118+ }
119+ innerStruct := reflect .New (fieldTypeElem ).Interface ()
97120 innerValidation , err := GetValidationsFromStruct (innerStruct , string (tagType ))
98121 if err != nil {
99122 return nil , fmt .Errorf ("error getting inner validation from struct: %v" , err )
0 commit comments