ValidateMapCtx validates a map using a map of validation rules and allows passing of contextual validation information via context.Context.
(ctx context.Context, data map[string]interface{}, rules map[string]interface{})
| 163 | // ValidateMapCtx validates a map using a map of validation rules and allows passing of contextual |
| 164 | // validation information via context.Context. |
| 165 | func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{}, rules map[string]interface{}) map[string]interface{} { |
| 166 | errs := make(map[string]interface{}) |
| 167 | for field, rule := range rules { |
| 168 | if ruleObj, ok := rule.(map[string]interface{}); ok { |
| 169 | if dataObj, ok := data[field].(map[string]interface{}); ok { |
| 170 | err := v.ValidateMapCtx(ctx, dataObj, ruleObj) |
| 171 | if len(err) > 0 { |
| 172 | errs[field] = err |
| 173 | } |
| 174 | } else if dataObjs, ok := data[field].([]map[string]interface{}); ok { |
| 175 | for _, obj := range dataObjs { |
| 176 | err := v.ValidateMapCtx(ctx, obj, ruleObj) |
| 177 | if len(err) > 0 { |
| 178 | errs[field] = err |
| 179 | } |
| 180 | } |
| 181 | } else { |
| 182 | errs[field] = errors.New("The field: '" + field + "' is not a map to dive") |
| 183 | } |
| 184 | } else if ruleStr, ok := rule.(string); ok { |
| 185 | err := v.VarWithKeyCtx(ctx, field, data[field], ruleStr) |
| 186 | if err != nil { |
| 187 | errs[field] = err |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | return errs |
| 192 | } |
| 193 | |
| 194 | // ValidateMap validates map data from a map of tags |
| 195 | func (v *Validate) ValidateMap(data map[string]interface{}, rules map[string]interface{}) map[string]interface{} { |