Skip to content

Commit

Permalink
StructNonZeroFields
Browse files Browse the repository at this point in the history
  • Loading branch information
iesreza committed Aug 24, 2024
1 parent 21a98e5 commit 177dc04
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/validation/validation.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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"))
Expand Down

0 comments on commit 177dc04

Please sign in to comment.