|
|
|
@ -150,27 +150,67 @@ func (v *Validate) SetTagName(name string) { |
|
|
|
|
v.tagName = name |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const omitemptyPrefix = "__omitempty__" |
|
|
|
|
|
|
|
|
|
// ValidateMapCtx validates a map using a map of validation rules and allows passing of contextual
|
|
|
|
|
// 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{}) |
|
|
|
|
if len(data) == 0 { |
|
|
|
|
return errs |
|
|
|
|
} |
|
|
|
|
Loop: |
|
|
|
|
for field, rule := range rules { |
|
|
|
|
if strings.HasPrefix(field, omitemptyPrefix) == true { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
if ruleObj, ok := rule.(map[string]interface{}); ok { |
|
|
|
|
if dataObj, ok := data[field].(map[string]interface{}); ok { |
|
|
|
|
err := v.ValidateMapCtx(ctx, dataObj, ruleObj) |
|
|
|
|
if len(err) > 0 { |
|
|
|
|
errs[field] = err |
|
|
|
|
} |
|
|
|
|
} else if dataObjs, ok := data[field].([]map[string]interface{}); ok { |
|
|
|
|
for _, obj := range dataObjs { |
|
|
|
|
err := v.ValidateMapCtx(ctx, obj, ruleObj) |
|
|
|
|
if dataObj, ok := data[field]; ok { |
|
|
|
|
if do, ok := dataObj.(map[string]interface{}); ok { |
|
|
|
|
err := v.ValidateMapCtx(ctx, do, ruleObj) |
|
|
|
|
if len(err) > 0 { |
|
|
|
|
errs[field] = err |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
errs[field] = errors.New("The field: '" + field + "' is not a map to dive") |
|
|
|
|
} |
|
|
|
|
} 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 to dive") |
|
|
|
|
} |
|
|
|
|
} 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 + "' rule error") |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
errs[field] = errors.New("The field: '" + field + "' rule error") |
|
|
|
|
} |
|
|
|
|
} else if ruleStr, ok := rule.(string); ok { |
|
|
|
|
err := v.VarCtx(ctx, data[field], ruleStr) |
|
|
|
|
if err != nil { |
|
|
|
|