From 07151bb0f4cf2431afb99fbfdebdf9db5435f1f1 Mon Sep 17 00:00:00 2001 From: yongruilin Date: Mon, 9 Jun 2025 19:03:11 +0000 Subject: [PATCH] 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. --- .../cmd/validation-gen/validation.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go b/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go index 579a98fc5a7..6c5de32f659 100644 --- a/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go +++ b/staging/src/k8s.io/code-generator/cmd/validation-gen/validation.go @@ -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)