mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-30 05:41:50 +02:00
fix(validation): correct CustomValidator and strutils.Parser handling, add tests
This commit is contained in:
@@ -90,37 +90,6 @@ func ValidateWithFieldTags(s any) gperr.Error {
|
||||
return errs.Error()
|
||||
}
|
||||
|
||||
var validatorType = reflect.TypeFor[CustomValidator]()
|
||||
|
||||
func ValidateWithCustomValidator(v reflect.Value) gperr.Error {
|
||||
if v.Kind() == reflect.Struct {
|
||||
if v.Type().Implements(validatorType) {
|
||||
return v.Interface().(CustomValidator).Validate()
|
||||
}
|
||||
if v.CanAddr() {
|
||||
return validateWithValidator(v.Addr())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if v.Kind() == reflect.Pointer {
|
||||
if v.IsNil() {
|
||||
return nil
|
||||
}
|
||||
if v.Type().Implements(validatorType) {
|
||||
return v.Interface().(CustomValidator).Validate()
|
||||
}
|
||||
return validateWithValidator(v.Elem())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateWithValidator(v reflect.Value) gperr.Error {
|
||||
if v.Type().Implements(validatorType) {
|
||||
return v.Interface().(CustomValidator).Validate()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func dive(dst reflect.Value) (v reflect.Value, t reflect.Type, err gperr.Error) {
|
||||
dstT := dst.Type()
|
||||
for {
|
||||
@@ -529,6 +498,12 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
|
||||
default:
|
||||
}
|
||||
|
||||
// check if (*T).Convertor is implemented
|
||||
if dst.Addr().Type().Implements(parserType) {
|
||||
parser := dst.Addr().Interface().(strutils.Parser)
|
||||
return true, gperr.Wrap(parser.Parse(src))
|
||||
}
|
||||
|
||||
if gi.ReflectIsNumeric(dst) || dst.Kind() == reflect.Bool {
|
||||
err := gi.ReflectStrToNumBool(dst, src)
|
||||
if err != nil {
|
||||
@@ -537,12 +512,6 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// check if (*T).Convertor is implemented
|
||||
if dst.Addr().Type().Implements(parserType) {
|
||||
parser := dst.Addr().Interface().(strutils.Parser)
|
||||
return true, gperr.Wrap(parser.Parse(src))
|
||||
}
|
||||
|
||||
// yaml like
|
||||
var tmp any
|
||||
switch dst.Kind() {
|
||||
|
||||
Reference in New Issue
Block a user