Skip to content

Commit

Permalink
feat(crdvalidation): add messageExpression support to XValidation marker
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Apr 21, 2024
1 parent e159968 commit 04e6d44
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 101 deletions.
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.

0 comments on commit 04e6d44

Please sign in to comment.