Skip to content

Commit

Permalink
bug: fix required rule for map with nested slice (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
sujit-baniya committed Jun 6, 2023
1 parent 79adbf3 commit 97df2bd
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ func (r *Rule) valueValidate(field, name string, val interface{}, v *Validation)
// rftVal := reflect.Indirect(reflect.ValueOf(val))
rftVal := reflect.ValueOf(val)
valKind := rftVal.Kind()
isRequired := fm.name == "required"
arrField := ""
if strings.Contains(field, ".*.") {
arrField = strings.Split(field, ".*.")[1]
}

// feat: support check sub element in a slice list. eg: field=names.*
hasSliceSuffix := len(strings.Split(field, ".*")) > 1
Expand All @@ -268,6 +273,14 @@ func (r *Rule) valueValidate(field, name string, val interface{}, v *Validation)
} else {
subVal = subRv.Interface()
}
switch subVal := subVal.(type) {
case map[string]any:
if arrField != "" {
if _, exists := subVal[arrField]; !exists && isRequired {
return false
}
}
}

// 2. call built in validator
if !callValidator(v, fm, field, subVal, r.arguments) {
Expand Down

0 comments on commit 97df2bd

Please sign in to comment.