Skip to content

Commit 407aed4

Browse files
authored
Merge pull request #1296 from mcbenjemaa/fix-xvalidation-duplication
🐛 Prevent XValidation duplication by verifying if the rule already exists
2 parents 97ea62c + 60193b9 commit 407aed4

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

pkg/crd/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func typeToSchema(ctx *schemaContext, rawType ast.Expr) *apiext.JSONSchemaProps
231231
case *ast.MapType:
232232
props = mapToSchema(ctx, expr)
233233
case *ast.StarExpr:
234-
props = typeToSchema(ctx, expr.X)
234+
props = typeToSchema(ctx.ForInfo(&markers.TypeInfo{}), expr.X)
235235
case *ast.StructType:
236236
props = structToSchema(ctx, expr)
237237
default:

pkg/crd/testdata/oneof/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type OneofSpec struct {
3636
TypeWithMultipleAtLeastOneofs *TypeWithMultipleAtLeastOneofs `json:"typeWithMultipleAtLeastOneOf,omitempty"`
3737

3838
TypeWithAllOneOf *TypeWithAllOneofs `json:"typeWithAllOneOf,omitempty"`
39+
40+
FirstCustomTypeAlias CustomTypeAlias `json:"firstCustomTypeAlias,omitempty"`
41+
42+
// This verifies if the custom type alias XValidation is not duplicated.
43+
SecondCustomTypeAlias CustomTypeAlias `json:"secondCustomTypeAlias,omitempty"`
3944
}
4045

4146
// +kubebuilder:validation:XValidation:message="only one of foo|bar may be set",rule="!(has(self.foo) && has(self.bar))"
@@ -95,6 +100,10 @@ type TypeWithAllOneofs struct {
95100
F *string `json:"f,omitempty"`
96101
}
97102

103+
// CustomTypeAlias is a custom alias
104+
// +kubebuilder:validation:XValidation:rule="self >= 100 && self <= 1000",message="invalid CustomTypeAlias value"
105+
type CustomTypeAlias *int32
106+
98107
// Oneof is the Schema for the Oneof API
99108
type Oneof struct {
100109
/*

pkg/crd/testdata/testdata.kubebuilder.io_oneofs.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ spec:
3636
spec:
3737
description: OneofSpec is the spec for the oneofs API.
3838
properties:
39+
firstCustomTypeAlias:
40+
description: CustomTypeAlias is a custom alias
41+
format: int32
42+
type: integer
43+
x-kubernetes-validations:
44+
- message: invalid CustomTypeAlias value
45+
rule: self >= 100 && self <= 1000
3946
firstTypeWithExactOneof:
4047
properties:
4148
bar:
@@ -60,6 +67,14 @@ spec:
6067
- message: at most one of the fields in [foo bar] may be set
6168
rule: '[has(self.foo),has(self.bar)].filter(x,x==true).size() <=
6269
1'
70+
secondCustomTypeAlias:
71+
description: This verifies if the custom type alias XValidation is
72+
not duplicated.
73+
format: int32
74+
type: integer
75+
x-kubernetes-validations:
76+
- message: invalid CustomTypeAlias value
77+
rule: self >= 100 && self <= 1000
6378
secondTypeWithExactOneof:
6479
properties:
6580
a:

0 commit comments

Comments
 (0)