Skip to content

Commit

Permalink
Add integration test for xvalidation on field and struct
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Jul 11, 2024
1 parent 1438f66 commit 7e435ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ type CronJobSpec struct {
// +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"`

// Test of the expression-based validation on both field and type.
// +kubebuilder:validation:XValidation:rule="self.startsWith('good-')",message="must have good prefix"
StringWithEvenAndGoodPrefix StringEvenType `json:"goodStringEven,omitempty"`

// Test that we can add a forbidden field using XValidation Reason and FieldPath.
// The validation is applied to the spec struct itself and not the field.
ForbiddenInt int `json:"forbiddenInt,omitempty"`
Expand Down Expand Up @@ -585,6 +589,10 @@ const (
ReplaceConcurrent ConcurrencyPolicy = "Replace"
)

// StringEvenType is a type that includes an expression-based validation.
// +kubebuilder:validation:XValidation:rule="self.size() % 2 == 0",message="must have even length"
type StringEvenType string

// CronJobStatus defines the observed state of CronJob
type CronJobStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
9 changes: 9 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6840,6 +6840,15 @@ spec:
- messageExpression: '''Length has to be even but is '' + len(self.stringWithEvenLengthAndMessageExpression)
+ '' instead'''
rule: self.size() % 2 == 0
goodStringEven:
description: Test of the expression-based validation on both field
and type.
type: string
x-kubernetes-validations:
- message: must have even length
rule: self.size() % 2 == 0
- message: must have good prefix
rule: self.startsWith('good-')
structWithSeveralFields:
description: A struct that can only be entirely replaced
properties:
Expand Down

0 comments on commit 7e435ac

Please sign in to comment.