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

✨ feat(crdvalidation): add messageExpression support to XValidation marker #927

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ func isIntegral(value float64) bool {
// This marker may be repeated to specify multiple expressions, all of
// which must evaluate to true.
type XValidation struct {
Rule string
Message string `marker:",optional"`
Rule string
Message string `marker:",optional"`
MessageExpression string `marker:"messageExpression,optional"`
}

func (m Maximum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
Expand Down Expand Up @@ -534,8 +535,9 @@ func (m XIntOrString) ApplyPriority() ApplyPriority {

func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
schema.XValidations = append(schema.XValidations, apiext.ValidationRule{
Rule: m.Rule,
Message: m.Message,
Rule: m.Rule,
Message: m.Message,
MessageExpression: m.MessageExpression,
})
return nil
}
106 changes: 55 additions & 51 deletions pkg/crd/markers/zz_generated.markerhelp.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ type CronJobSpec struct {
// +kubebuilder:validation:XValidation:rule="true"
StringWithEvenLength string `json:"stringWithEvenLength,omitempty"`

// Test of the expression-based validation with messageExpression marker.
// +kubebuilder:validation:XValidation:rule="self.size() % 2 == 0",messageExpression="'Length has to be even but is ' + len(self.stringWithEvenLengthAndMessageExpression) + ' instead'"
StringWithEvenLengthAndMessageExpression string `json:"stringWithEvenLengthAndMessageExpression,omitempty"`

// Checks that fixed-length arrays work
Array [3]int `json:"array,omitempty"`

Expand Down Expand Up @@ -336,7 +340,6 @@ type MinMaxObject struct {
}

type EmpiableObject struct {

// +kubebuilder:default=forty-two
Foo string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6805,6 +6805,13 @@ spec:
- message: must have even length
rule: self.size() % 2 == 0
- rule: "true"
stringWithEvenLengthAndMessageExpression:
description: Test of the expression-based validation with
messageExpression marker.
type: string
x-kubernetes-validations:
- messageExpression: "'Length has to be even but is ' + len(self.stringWithEvenLengthAndMessageExpression) + ' instead'"
rule: self.size() % 2 == 0
structWithSeveralFields:
description: A struct that can only be entirely replaced
properties:
Expand Down
18 changes: 11 additions & 7 deletions pkg/crd/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/deepcopy/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/genall/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/rbac/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/schemapatcher/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 26 additions & 22 deletions pkg/webhook/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.