Skip to content

Commit a5a4cd6

Browse files
authored
Merge pull request #1118 from cbandy/intorstring-length
🌱 Allow string validation on XIntOrString
2 parents 34a0f8e + 50893de commit a5a4cd6

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

pkg/crd/markers/validation.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ func hasNumericType(schema *apiext.JSONSchemaProps) bool {
316316
return schema.Type == "integer" || schema.Type == "number"
317317
}
318318

319+
func hasTextualType(schema *apiext.JSONSchemaProps) bool {
320+
return schema.Type == "string" || schema.XIntOrString
321+
}
322+
319323
func isIntegral(value float64) bool {
320324
return value == math.Trunc(value) && !math.IsNaN(value) && !math.IsInf(value, 0)
321325
}
@@ -394,29 +398,26 @@ func (m MultipleOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
394398
}
395399

396400
func (m MaxLength) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
397-
if schema.Type != "string" {
398-
return fmt.Errorf("must apply maxlength to a string")
401+
if !hasTextualType(schema) {
402+
return fmt.Errorf("must apply maxlength to a textual value, found type %q", schema.Type)
399403
}
400404
val := int64(m)
401405
schema.MaxLength = &val
402406
return nil
403407
}
404408

405409
func (m MinLength) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
406-
if schema.Type != "string" {
407-
return fmt.Errorf("must apply minlength to a string")
410+
if !hasTextualType(schema) {
411+
return fmt.Errorf("must apply minlength to a textual value, found type %q", schema.Type)
408412
}
409413
val := int64(m)
410414
schema.MinLength = &val
411415
return nil
412416
}
413417

414418
func (m Pattern) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
415-
// Allow string types or IntOrStrings. An IntOrString will still
416-
// apply the pattern validation when a string is detected, the pattern
417-
// will not apply to ints though.
418-
if schema.Type != "string" && !schema.XIntOrString {
419-
return fmt.Errorf("must apply pattern to a `string` or `IntOrString`")
419+
if !hasTextualType(schema) {
420+
return fmt.Errorf("must apply pattern to a textual value, found type %q", schema.Type)
420421
}
421422
schema.Pattern = string(m)
422423
return nil

pkg/crd/testdata/cronjob_types.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,12 @@ type CronJobSpec struct {
249249
// +kubebuilder:validation:Schemaless
250250
Schemaless []byte `json:"schemaless,omitempty"`
251251

252-
// This tests that an IntOrString can also have a pattern attached
253-
// to it.
252+
// This tests that an IntOrString can also have string validation.
254253
// This can be useful if you want to limit the string to a percentage or integer.
255254
// The XIntOrString marker is a requirement for having a pattern on this type.
256255
// +kubebuilder:validation:XIntOrString
256+
// +kubebuilder:validation:MaxLength=11
257+
// +kubebuilder:validation:MinLength=2
257258
// +kubebuilder:validation:Pattern="^((100|[0-9]{1,2})%|[0-9]+)$"
258259
IntOrStringWithAPattern *intstr.IntOrString `json:"intOrStringWithAPattern,omitempty"`
259260

@@ -358,10 +359,12 @@ type CronJobSpec struct {
358359
// +kubebuilder:validation:MinItems=3
359360
LongerStringArray []LongerString `json:"longerStringArray,omitempty"`
360361

361-
// This tests that a slice of IntOrString can also have a pattern attached to it.
362+
// This tests that a slice of IntOrString can also have string validation.
362363
// This can be useful if you want to limit the string to a percentage or integer.
363364
// The XIntOrString marker is a requirement for having a pattern on this type.
364365
// +kubebuilder:validation:items:XIntOrString
366+
// +kubebuilder:validation:items:MaxLength=10
367+
// +kubebuilder:validation:items:MinLength=1
365368
// +kubebuilder:validation:items:Pattern="^((100|[0-9]{1,2})%|[0-9]+)$"
366369
IntOrStringArrayWithAPattern []*intstr.IntOrString `json:"intOrStringArrayWithAPattern,omitempty"`
367370

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,15 @@ spec:
266266
type: integer
267267
intOrStringArrayWithAPattern:
268268
description: |-
269-
This tests that a slice of IntOrString can also have a pattern attached to it.
269+
This tests that a slice of IntOrString can also have string validation.
270270
This can be useful if you want to limit the string to a percentage or integer.
271271
The XIntOrString marker is a requirement for having a pattern on this type.
272272
items:
273273
anyOf:
274274
- type: integer
275275
- type: string
276+
maxLength: 10
277+
minLength: 1
276278
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$
277279
x-kubernetes-int-or-string: true
278280
type: array
@@ -281,10 +283,11 @@ spec:
281283
- type: integer
282284
- type: string
283285
description: |-
284-
This tests that an IntOrString can also have a pattern attached
285-
to it.
286+
This tests that an IntOrString can also have string validation.
286287
This can be useful if you want to limit the string to a percentage or integer.
287288
The XIntOrString marker is a requirement for having a pattern on this type.
289+
maxLength: 11
290+
minLength: 2
288291
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$
289292
x-kubernetes-int-or-string: true
290293
intWithValidations:

0 commit comments

Comments
 (0)