Skip to content

Commit

Permalink
fix: dont validate when field is optional and dont have value
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarfh committed Sep 14, 2023
1 parent 310fac2 commit d926784
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package god

import (
"fmt"
"reflect"
"sort"
)

Expand All @@ -28,6 +29,10 @@ func CommonPlaygroundValidation(tag string, messages []string, customFunc ...fun
}
}

func isZeroOfUnderlyingType(x interface{}) bool {
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}

func CommonTypeValidation(v []Validation, value interface{}, typeName string, typeAssertFunc typeAssertFunc) Schema {
schema := Schema{}
// Ordena as validações por peso
Expand All @@ -42,9 +47,10 @@ func CommonTypeValidation(v []Validation, value interface{}, typeName string, ty

// Valida se é um campo opcional
// Caso tenha nenhum valor deve ignorar as validações
_, ok := typeAssertFunc(value)
if !isRequired && !ok {
out, ok := typeAssertFunc(value)
if !isRequired && !ok || !isRequired && isZeroOfUnderlyingType(out) {
schema.Error = nil
return schema
}

// Primeiro, verifica se o valor é do tipo correto
Expand Down

0 comments on commit d926784

Please sign in to comment.