map array validate fix

local
hzy 1 year ago
parent e3e24a10a9
commit 430edf2247
  1. 24
      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 len(arrRuleObj) > 0 {
if arrRuleItem, ok := arrRuleObj[0].(map[string]interface{}); ok {
if dataObjs, ok := data[field]; ok { if dataObjs, ok := data[field]; ok {
if do, ok := dataObjs.([]map[string]interface{}); ok && len(arrRuleObj) > 0 { if do, ok := dataObjs.([]interface{}); ok {
for _, obj := range do { for _, obj := range do {
err := v.ValidateMapCtx(ctx, obj, arrRuleObj[0]) if objItem, ok := obj.(map[string]interface{}); ok {
err := v.ValidateMapCtx(ctx, objItem, arrRuleItem)
if len(err) > 0 { if len(err) > 0 {
errs[field] = err errs[field] = err
} }
}
} else { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map array") 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 { } else if _, ok := rules[fmt.Sprintf("%s%s", omitemptyPrefix, field)].(string); ok {
// can be null // can be null
continue continue
} else { } else {
errs[field] = errors.New("The field: '" + field + "' is not a map array") errs[field] = errors.New("The field: '" + field + "' is empty")
}
} else {
errs[field] = errors.New("The field: '" + field + "' rule error")
}
} else {
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