| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 | 
							- package ii
 
- import (
 
- 	"fmt"
 
- 	"reflect"
 
- )
 
- // Validate 只能传入底层类型为 map 的数据
 
- // 检查 map 的 key 是否存在于 ItemInfo.Fields 内
 
- // 检查 val 的类型是否与 FieldInfo.Type 所要求的类型一致
 
- // Validate 仅用于检测且不更改原始数据
 
- func (c *ItemInfo) Validate(data any) error {
 
- 	rv := reflect.ValueOf(data)
 
- 	if rv.Type().Kind() != reflect.Map {
 
- 		return fmt.Errorf("%s: %s: value type not be map. data type: %s", getCallerName(), c.Name, valueType(data))
 
- 	}
 
- 	for fKey, idx := range c.fieldMap {
 
- 		rvMapV := rv.MapIndex(reflect.ValueOf(fKey))
 
- 		if rvMapV.IsZero() && c.Fields[idx].Required {
 
- 			return errRequired(fKey, nil)
 
- 		}
 
- 		if err := c.Fields[idx].Validate(rvMapV.Interface()); err != nil {
 
- 			return err
 
- 		}
 
- 	}
 
- 	return nil
 
- 	// rv := reflect.ValueOf(data)
 
- 	// switch rv.Type().Kind() {
 
- 	// case reflect.Map:
 
- 	//
 
- 	// case reflect.Slice:
 
- 	// 	// []map[string]any, []mo.M, []any{}, mo.D
 
- 	// 	if rv.Type().Elem().Kind() != reflect.Map {
 
- 	// 		return fmt.Errorf("validate: the element type(%s) does not support", rv.Type().Elem().Kind())
 
- 	// 	}
 
- 	// 	// 循环数组
 
- 	// 	for i := 0; i < rv.Len(); i++ {
 
- 	// 		// 如果元素为 map 时
 
- 	// 		if err := c.Validate(rv.Index(i).Interface()); err != nil {
 
- 	// 			return err
 
- 	// 		}
 
- 	// 	}
 
- 	// 	return nil
 
- 	// default:
 
- 	// 	return fmt.Errorf("unsupport type: %s", valueType(data))
 
- 	// }
 
- }
 
 
  |