Skip to content

Commit 1c8af4e

Browse files
committed
fixup! maxLength: add support for k8s declarative validation markers and configurability
1 parent ce0f1e7 commit 1c8af4e

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

pkg/analysis/maxlength/analyzer.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,24 @@ func (a *analyzer) needsStringMaxLength(markerSet markershelper.MarkerSet) bool
266266
return true
267267
}
268268

269-
// needsByteSliceMaxLength is like needsStringMaxLength but also accepts +k8s:maxBytes,
270-
// which is the DV-correct marker for byte-length constraints on []byte fields.
271-
// (+k8s:maxLength counts Unicode characters; +k8s:maxBytes counts raw bytes.)
269+
// needsByteSliceMaxLength is like needsStringMaxLength but enforces that for DV markers,
270+
// +k8s:maxBytes is used instead of +k8s:maxLength (which counts characters).
272271
func (a *analyzer) needsByteSliceMaxLength(markerSet markershelper.MarkerSet) bool {
273-
if markerSet.Has(markers.K8sMaxBytesMarker) {
272+
switch {
273+
case markerSet.Has(markers.KubebuilderMaxLengthMarker),
274+
markerSet.Has(markers.K8sMaxBytesMarker),
275+
markerSet.Has(markers.KubebuilderEnumMarker),
276+
markerSet.Has(markers.K8sEnumMarker),
277+
markerSet.HasWithValue(kubebuilderFormatWithValue("date")),
278+
markerSet.HasWithValue(kubebuilderFormatWithValue("date-time")),
279+
markerSet.HasWithValue(kubebuilderFormatWithValue("duration")),
280+
markerSet.HasWithValue(k8sFormatWithValue("date")),
281+
markerSet.HasWithValue(k8sFormatWithValue("date-time")),
282+
markerSet.HasWithValue(k8sFormatWithValue("duration")):
274283
return false
275284
}
276285

277-
return a.needsStringMaxLength(markerSet)
286+
return true
278287
}
279288

280289
func (a *analyzer) needsItemsMaxLength(markerSet markershelper.MarkerSet) bool {
@@ -299,5 +308,5 @@ func kubebuilderItemsFormatWithValue(value string) string {
299308
}
300309

301310
func k8sFormatWithValue(value string) string {
302-
return fmt.Sprintf("%s:=%s", markers.K8sFormatMarker, value)
311+
return fmt.Sprintf("%s=%s", markers.K8sFormatMarker, value)
303312
}

0 commit comments

Comments
 (0)