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:
yusing
2026-01-10 15:49:58 +08:00
parent 71f6636cc3
commit 12b784d126

View File

@@ -480,6 +480,14 @@ func ConvertSlice(src reflect.Value, dst reflect.Value, checkValidateTag bool) g
} }
numValid++ 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 { if err := sliceErrs.Error(); err != nil {
dst.SetLen(numValid) // shrink to number of elements that were successfully converted dst.SetLen(numValid) // shrink to number of elements that were successfully converted
return err return err