chore: Add validation error for unsupported map of slices in validation-gen

This update introduces error handling for cases where a map of slices is encountered during validation. The validation process now explicitly returns an error message indicating that this type of validation is not supported, enhancing the robustness of the validation-gen tool.
This commit is contained in:
yongruilin
2025-06-09 19:03:11 +00:00
parent 1876c54451
commit 07151bb0f4

View File

@@ -408,6 +408,13 @@ func (td *typeDiscoverer) discoverType(t *types.Type, fldPath *field.Path) (*typ
} else if validations.Empty() {
klog.V(6).InfoS("no type-attached validations", "type", t)
} else {
// TODO: This check is placed here to allow map-of-slices fields to exist in the type
// system, while preventing validation on them since that functionality is not yet
// implemented. Once validation support for map-of-slices is added, this check should
// be removed.
if unalias(t).Kind == types.Map && unalias(t).Elem.Kind == types.Slice {
return nil, fmt.Errorf("field %s: validation for map of slices is not supported", fldPath)
}
klog.V(5).InfoS("found type-attached validations", "n", validations.Len(), "type", t)
thisNode.typeValidations.Add(validations)
}
@@ -575,6 +582,13 @@ func (td *typeDiscoverer) discoverStruct(thisNode *typeNode, fldPath *field.Path
klog.V(6).InfoS("no field-attached validations", "field", childPath)
} else {
klog.V(5).InfoS("found field-attached validations", "n", validations.Len(), "field", childPath)
// TODO: This check is placed here to allow map-of-slices fields to exist in the type
// system, while preventing validation on them since that functionality is not yet
// implemented. Once validation support for map-of-slices is added, this check should
// be removed.
if unalias(childType).Kind == types.Map && unalias(childType).Elem.Kind == types.Slice {
return fmt.Errorf("field %s: validation for map of slices is not supported", childPath)
}
child.fieldValidations.Add(validations)
if len(validations.Variables) > 0 {
return fmt.Errorf("%v: variable generation is not supported for field validations", childPath)