map array validate fix

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

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

Loading…
Cancel
Save