Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Allow string validation on XIntOrString #1118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ func hasNumericType(schema *apiext.JSONSchemaProps) bool {
return schema.Type == "integer" || schema.Type == "number"
}

func hasTextualType(schema *apiext.JSONSchemaProps) bool {
return schema.Type == "string" || schema.XIntOrString
}

func isIntegral(value float64) bool {
return value == math.Trunc(value) && !math.IsNaN(value) && !math.IsInf(value, 0)
}
Expand Down Expand Up @@ -394,29 +398,26 @@ func (m MultipleOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
}

func (m MaxLength) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
if schema.Type != "string" {
return fmt.Errorf("must apply maxlength to a string")
if !hasTextualType(schema) {
return fmt.Errorf("must apply maxlength to a textual value, found type %q", schema.Type)
}
val := int64(m)
schema.MaxLength = &val
return nil
}

func (m MinLength) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
if schema.Type != "string" {
return fmt.Errorf("must apply minlength to a string")
if !hasTextualType(schema) {
return fmt.Errorf("must apply minlength to a textual value, found type %q", schema.Type)
}
val := int64(m)
schema.MinLength = &val
return nil
}

func (m Pattern) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
// Allow string types or IntOrStrings. An IntOrString will still
// apply the pattern validation when a string is detected, the pattern
// will not apply to ints though.
if schema.Type != "string" && !schema.XIntOrString {
return fmt.Errorf("must apply pattern to a `string` or `IntOrString`")
if !hasTextualType(schema) {
return fmt.Errorf("must apply pattern to a textual value, found type %q", schema.Type)
}
schema.Pattern = string(m)
return nil
Expand Down
9 changes: 6 additions & 3 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,12 @@ type CronJobSpec struct {
// +kubebuilder:validation:Schemaless
Schemaless []byte `json:"schemaless,omitempty"`

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

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

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

Expand Down
9 changes: 6 additions & 3 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ spec:
type: integer
intOrStringArrayWithAPattern:
description: |-
This tests that a slice of IntOrString can also have a pattern attached to it.
This tests that a slice of IntOrString can also have string validation.
This can be useful if you want to limit the string to a percentage or integer.
The XIntOrString marker is a requirement for having a pattern on this type.
items:
anyOf:
- type: integer
- type: string
maxLength: 10
minLength: 1
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$
x-kubernetes-int-or-string: true
type: array
Expand All @@ -271,10 +273,11 @@ spec:
- type: integer
- type: string
description: |-
This tests that an IntOrString can also have a pattern attached
to it.
This tests that an IntOrString can also have string validation.
This can be useful if you want to limit the string to a percentage or integer.
The XIntOrString marker is a requirement for having a pattern on this type.
maxLength: 11
minLength: 2
pattern: ^((100|[0-9]{1,2})%|[0-9]+)$
x-kubernetes-int-or-string: true
intWithValidations:
Expand Down
Loading