Skip to content

Commit cd8f8f9

Browse files
committed
comments#1
1 parent 7961a9e commit cd8f8f9

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

pkg/apis/aws/validation/filter.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ var (
3333
// GatewayEndpointRegex matches one or more word characters, optionally followed by dot-separated word segments
3434
GatewayEndpointRegex = `^\w+(\.\w+)*$`
3535

36-
validateK8sResourceName = combineValidationFuncs(regex(k8sResourceNameRegex), minLength(1), maxLength(253))
37-
validateVpcID = combineValidationFuncs(regex(VpcIDRegex), maxLength(255))
36+
validateK8sResourceName = combineValidationFuncs(regex(k8sResourceNameRegex), notEmpty, maxLength(253))
37+
validateVpcID = combineValidationFuncs(regex(VpcIDRegex), notEmpty, maxLength(255))
3838
validateEipAllocationID = combineValidationFuncs(regex(EipAllocationIDRegex), maxLength(255))
3939
validateSnapshotID = combineValidationFuncs(regex(SnapshotIDRegex), maxLength(255))
40-
validateIamInstanceProfileName = combineValidationFuncs(regex(IamInstanceProfileNameRegex), minLength(1), maxLength(128))
40+
validateIamInstanceProfileName = combineValidationFuncs(regex(IamInstanceProfileNameRegex), notEmpty, maxLength(128))
4141
validateIamInstanceProfileArn = combineValidationFuncs(regex(IamInstanceProfileArnRegex), maxLength(255))
4242
validateZoneName = combineValidationFuncs(regex(ZoneNameRegex), maxLength(255))
43-
validateTagKey = combineValidationFuncs(regex(TagKeyRegex), minLength(1), maxLength(128))
43+
validateTagKey = combineValidationFuncs(regex(TagKeyRegex), notEmpty, maxLength(128))
4444
validateGatewayEndpointName = combineValidationFuncs(regex(GatewayEndpointRegex), maxLength(255))
4545
)
4646

@@ -72,22 +72,18 @@ func regex(regex string) validateFunc[string] {
7272
}
7373
}
7474

75-
// nolint:unparam
76-
func minLength(min int) validateFunc[string] {
77-
return func(name string, fld *field.Path) field.ErrorList {
78-
var allErrs field.ErrorList
79-
if utf8.RuneCountInString(name) < min {
80-
return field.ErrorList{field.Invalid(fld, name, fmt.Sprintf("must not be fewer than %d characters, got %d", min, len(name)))}
81-
}
82-
return allErrs
75+
func notEmpty(name string, fld *field.Path) field.ErrorList {
76+
if utf8.RuneCountInString(name) == 0 {
77+
return field.ErrorList{field.Required(fld, "cannot be empty")}
8378
}
79+
return nil
8480
}
8581

8682
func maxLength(max int) validateFunc[string] {
8783
return func(name string, fld *field.Path) field.ErrorList {
8884
var allErrs field.ErrorList
89-
if utf8.RuneCountInString(name) > max {
90-
return field.ErrorList{field.Invalid(fld, name, fmt.Sprintf("must not be more than %d characters, got %d", max, len(name)))}
85+
if l := utf8.RuneCountInString(name); l > max {
86+
return field.ErrorList{field.Invalid(fld, name, fmt.Sprintf("must not be more than %d characters, got %d", max, l))}
9187
}
9288
return allErrs
9389
}

pkg/apis/aws/validation/infrastructure_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ var _ = Describe("InfrastructureConfig validation", func() {
180180
Expect(errorList).To(BeEmpty())
181181
})
182182

183+
It("should reject empty ID string", func() {
184+
infrastructureConfig.Networks.VPC.ID = ptr.To("")
185+
infrastructureConfig.Networks.VPC.CIDR = nil
186+
errorList := ValidateInfrastructureConfig(infrastructureConfig, familyIPv4, &nodes, &pods, &services)
187+
Expect(errorList).To(ConsistOfFields(Fields{
188+
"Type": Equal(field.ErrorTypeRequired),
189+
"Field": Equal("networks.vpc.id"),
190+
"Detail": Equal("cannot be empty"),
191+
}))
192+
})
193+
183194
It("should reject setting both ID and CIDR", func() {
184195
infrastructureConfig.Networks.VPC.ID = ptr.To("vpc-123456")
185196
infrastructureConfig.Networks.VPC.CIDR = &vpcCIDR
@@ -771,11 +782,11 @@ var _ = Describe("InfrastructureConfig validation", func() {
771782
})
772783
Expect(errorList).To(ConsistOf(
773784
PointTo(MatchFields(IgnoreExtras, Fields{
774-
"Type": Equal(field.ErrorTypeInvalid),
785+
"Type": Equal(field.ErrorTypeRequired),
775786
"Field": Equal("ignoreTags.keys[1]"),
776787
})),
777788
PointTo(MatchFields(IgnoreExtras, Fields{
778-
"Type": Equal(field.ErrorTypeInvalid),
789+
"Type": Equal(field.ErrorTypeRequired),
779790
"Field": Equal("ignoreTags.keyPrefixes[1]"),
780791
})),
781792
))

0 commit comments

Comments
 (0)