mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 14:20:32 +01:00
feat(serialization): add validation support for custom slice types
Enhanced the ConvertSlice function to include validation for destination slices that implement the CustomValidator interface. If validation fails, errors are collected and returned, ensuring data integrity during slice conversion.
This commit is contained in:
@@ -480,6 +480,14 @@ func ConvertSlice(src reflect.Value, dst reflect.Value, checkValidateTag bool) g
|
||||
}
|
||||
numValid++
|
||||
}
|
||||
|
||||
if dst.Type().Implements(reflect.TypeFor[CustomValidator]()) {
|
||||
err := dst.Interface().(CustomValidator).Validate()
|
||||
if err != nil {
|
||||
sliceErrs.Add(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := sliceErrs.Error(); err != nil {
|
||||
dst.SetLen(numValid) // shrink to number of elements that were successfully converted
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user