mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-26 19:11:08 +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:
committed by
github-actions[bot]
parent
fd7f7081bc
commit
a0c43ee93e
@@ -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