map array validate fix

local
hzy 1 year ago
parent e3e24a10a9
commit 430edf2247
  1. 38
      validator_instance.go

@ -156,6 +156,7 @@ const omitemptyPrefix = "__omitempty__"
// validation validation information via context.Context. // validation validation information via context.Context.
func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{}, rules map[string]interface{}) map[string]interface{} { func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{}, rules map[string]interface{}) map[string]interface{} {
errs := make(map[string]interface{}) errs := make(map[string]interface{})
Loop:
for field, rule := range rules { for field, rule := range rules {
if strings.HasPrefix(field, omitemptyPrefix) == true { if strings.HasPrefix(field, omitemptyPrefix) == true {
continue continue
@ -176,23 +177,36 @@ func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{
} else { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map to dive") errs[field] = errors.New("The field: '" + field + "' is not a map to dive")
} }
} else if arrRuleObj, ok := rule.([]map[string]interface{}); ok { } else if arrRuleObj, ok := rule.([]interface{}); ok {
if dataObjs, ok := data[field]; ok { if len(arrRuleObj) > 0 {
if do, ok := dataObjs.([]map[string]interface{}); ok && len(arrRuleObj) > 0 { if arrRuleItem, ok := arrRuleObj[0].(map[string]interface{}); ok {
for _, obj := range do { if dataObjs, ok := data[field]; ok {
err := v.ValidateMapCtx(ctx, obj, arrRuleObj[0]) if do, ok := dataObjs.([]interface{}); ok {
if len(err) > 0 { for _, obj := range do {
errs[field] = err if objItem, ok := obj.(map[string]interface{}); ok {
err := v.ValidateMapCtx(ctx, objItem, arrRuleItem)
if len(err) > 0 {
errs[field] = err
}
} else {
errs[field] = errors.New("The field: '" + field + "' is not a map array")
continue Loop
}
}
} else {
errs[field] = errors.New("The field: '" + field + "' is not a array")
} }
} else if _, ok := rules[fmt.Sprintf("%s%s", omitemptyPrefix, field)].(string); ok {
// can be null
continue
} else {
errs[field] = errors.New("The field: '" + field + "' is empty")
} }
} else { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map array") errs[field] = errors.New("The field: '" + field + "' rule error")
} }
} else if _, ok := rules[fmt.Sprintf("%s%s", omitemptyPrefix, field)].(string); ok {
// can be null
continue
} else { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map array") errs[field] = errors.New("The field: '" + field + "' rule error")
} }
} else if ruleStr, ok := rule.(string); ok { } else if ruleStr, ok := rule.(string); ok {
err := v.VarCtx(ctx, data[field], ruleStr) err := v.VarCtx(ctx, data[field], ruleStr)

Loading…
Cancel
Save