|
|
@ -82,7 +82,7 @@ type Validate struct { |
|
|
|
hasTagNameFunc bool |
|
|
|
hasTagNameFunc bool |
|
|
|
tagNameFunc TagNameFunc |
|
|
|
tagNameFunc TagNameFunc |
|
|
|
structLevelFuncs map[reflect.Type]StructLevelFuncCtx |
|
|
|
structLevelFuncs map[reflect.Type]StructLevelFuncCtx |
|
|
|
customFuncs map[reflect.Type]CustomTypeFunc |
|
|
|
customFuncs sync.Map |
|
|
|
aliases map[string]string |
|
|
|
aliases map[string]string |
|
|
|
validations map[string]internalValidationFuncWrapper |
|
|
|
validations map[string]internalValidationFuncWrapper |
|
|
|
transTagFunc map[ut.Translator]map[string]TranslationFunc // map[<locale>]map[<tag>]TranslationFunc
|
|
|
|
transTagFunc map[ut.Translator]map[string]TranslationFunc // map[<locale>]map[<tag>]TranslationFunc
|
|
|
@ -152,7 +152,7 @@ func (v *Validate) SetTagName(name string) { |
|
|
|
|
|
|
|
|
|
|
|
// ValidateMapCtx validates a map using a map of validation rules and allows passing of contextual
|
|
|
|
// ValidateMapCtx validates a map using a map of validation rules and allows passing of contextual
|
|
|
|
// 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{}) |
|
|
|
for field, rule := range rules { |
|
|
|
for field, rule := range rules { |
|
|
|
if ruleObj, ok := rule.(map[string]interface{}); ok { |
|
|
|
if ruleObj, ok := rule.(map[string]interface{}); ok { |
|
|
@ -317,12 +317,17 @@ func (v *Validate) RegisterStructValidationMapRules(rules map[string]string, typ |
|
|
|
// NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
|
|
|
|
// NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation
|
|
|
|
func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) { |
|
|
|
func (v *Validate) RegisterCustomTypeFunc(fn CustomTypeFunc, types ...interface{}) { |
|
|
|
|
|
|
|
|
|
|
|
if v.customFuncs == nil { |
|
|
|
|
|
|
|
v.customFuncs = make(map[reflect.Type]CustomTypeFunc) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, t := range types { |
|
|
|
for _, t := range types { |
|
|
|
v.customFuncs[reflect.TypeOf(t)] = fn |
|
|
|
var rt reflect.Type |
|
|
|
|
|
|
|
switch tt := t.(type) { |
|
|
|
|
|
|
|
case reflect.Type: |
|
|
|
|
|
|
|
rt = tt |
|
|
|
|
|
|
|
case *reflect.Type: |
|
|
|
|
|
|
|
rt = *tt |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
rt = reflect.TypeOf(t) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
v.customFuncs.Store(rt, fn) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
v.hasCustomFuncs = true |
|
|
|
v.hasCustomFuncs = true |
|
|
|