Skip to content
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
8 changes: 8 additions & 0 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,14 @@ func (m XValidation) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
}
}

// Check if this validation rule already exists to prevent duplication
for _, existingRule := range schema.XValidations {
if existingRule.Rule == m.Rule {
// Rule already exists, don't add it again
return nil
}
}

schema.XValidations = append(schema.XValidations, apiext.ValidationRule{
Rule: m.Rule,
Message: m.Message,
Expand Down
3 changes: 3 additions & 0 deletions pkg/crd/testdata/oneof/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type OneofSpec struct {
TypeWithMultipleAtLeastOneofs *TypeWithMultipleAtLeastOneofs `json:"typeWithMultipleAtLeastOneOf,omitempty"`

TypeWithAllOneOf *TypeWithAllOneofs `json:"typeWithAllOneOf,omitempty"`

// The validation rule on TypeWithOneofs should not be duplicated.
SameTypeWithOneof *TypeWithOneofs `json:"sameTypeWithOneof,omitempty"`
}

// +kubebuilder:validation:XValidation:message="only one of foo|bar may be set",rule="!(has(self.foo) && has(self.bar))"
Expand Down
14 changes: 14 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_oneofs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ spec:
- message: at most one of the fields in [foo bar] may be set
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
1'
sameTypeWithOneof:
description: The validation rule on TypeWithOneofs should not be duplicated.
properties:
bar:
type: string
foo:
type: string
type: object
x-kubernetes-validations:
- message: only one of foo|bar may be set
rule: '!(has(self.foo) && has(self.bar))'
- message: at most one of the fields in [foo bar] may be set
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
1'
secondTypeWithExactOneof:
properties:
a:
Expand Down