mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-19 15:01:22 +02:00
fix(serialization): correct validation parameter
- Fix bug in mapUnmarshalValidate where checkValidateTag parameter was incorrectly negated when passed to Convert() - Remove obsolete validateWithValidator helper function
This commit is contained in:
@@ -315,7 +315,7 @@ func mapUnmarshalValidate(src SerializedObject, dstV reflect.Value, checkValidat
|
|||||||
info := getTypeInfo(dstT)
|
info := getTypeInfo(dstT)
|
||||||
for k, v := range src {
|
for k, v := range src {
|
||||||
if field, ok := info.getField(dstV, k); ok {
|
if field, ok := info.getField(dstV, k); ok {
|
||||||
err := Convert(reflect.ValueOf(v), field, !info.hasValidateTag)
|
err := Convert(reflect.ValueOf(v), field, checkValidateTag)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs.Add(err.Subject(k))
|
errs.Add(err.Subject(k))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,17 +29,19 @@ type CustomValidator interface {
|
|||||||
var validatorType = reflect.TypeFor[CustomValidator]()
|
var validatorType = reflect.TypeFor[CustomValidator]()
|
||||||
|
|
||||||
func ValidateWithCustomValidator(v reflect.Value) gperr.Error {
|
func ValidateWithCustomValidator(v reflect.Value) gperr.Error {
|
||||||
|
vt := v.Type()
|
||||||
if v.Kind() == reflect.Pointer {
|
if v.Kind() == reflect.Pointer {
|
||||||
if v.IsNil() {
|
elemType := vt.Elem()
|
||||||
// return nil
|
if vt.Implements(validatorType) {
|
||||||
return validateWithValidator(reflect.New(v.Type().Elem()))
|
if v.IsNil() {
|
||||||
}
|
return reflect.New(elemType).Interface().(CustomValidator).Validate()
|
||||||
if v.Type().Implements(validatorType) {
|
}
|
||||||
return v.Interface().(CustomValidator).Validate()
|
return v.Interface().(CustomValidator).Validate()
|
||||||
}
|
}
|
||||||
return validateWithValidator(v.Elem())
|
if elemType.Implements(validatorType) {
|
||||||
|
return v.Elem().Interface().(CustomValidator).Validate()
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
vt := v.Type()
|
|
||||||
if vt.PkgPath() != "" { // not a builtin type
|
if vt.PkgPath() != "" { // not a builtin type
|
||||||
// prioritize pointer method
|
// prioritize pointer method
|
||||||
if v.CanAddr() {
|
if v.CanAddr() {
|
||||||
@@ -56,10 +58,3 @@ func ValidateWithCustomValidator(v reflect.Value) gperr.Error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateWithValidator(v reflect.Value) gperr.Error {
|
|
||||||
if v.Type().Implements(validatorType) {
|
|
||||||
return v.Interface().(CustomValidator).Validate()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user