diff --git a/lib/validation/validation.go b/lib/validation/validation.go index c3bd701..fa4c0de 100644 --- a/lib/validation/validation.go +++ b/lib/validation/validation.go @@ -1,10 +1,12 @@ package validation import ( + "context" "fmt" "github.com/getevo/evo/v2/lib/db" "github.com/getevo/evo/v2/lib/generic" "gorm.io/gorm/schema" + "reflect" ) func Struct(input interface{}, fields ...string) []error { @@ -28,6 +30,29 @@ func Struct(input interface{}, fields ...string) []error { return errors } +func StructNonZeroFields(input interface{}, fields ...string) []error { + var errors []error + var g = generic.Parse(input) + + var stmt = db.Model(input).Statement + _ = stmt.Parse(input) + var ref = reflect.ValueOf(input) + for idx, _ := range stmt.Schema.Fields { + field := stmt.Schema.Fields[idx] + _, zero := field.ValueOf(context.Background(), ref) + + if !zero && field.Tag.Get("validation") != "" { + var err = validateField(&g, field) + if err != nil { + errors = append(errors, err) + } + } + + } + + return errors +} + func validateField(g *generic.Value, field *schema.Field) error { var value = g.Prop(field.Name) validators := parseValidators(field.Tag.Get("validation"))