Skip to content

Commit 6c9ddb1

Browse files
authored
Merge pull request #488 from moolen/feat/min-max-props
✨ add min/max properties validation
2 parents 359a540 + b99995b commit 6c9ddb1

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

pkg/crd/markers/validation.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var ValidationMarkers = mustMakeAllWithPrefix("kubebuilder:validation", markers.
4040
ExclusiveMaximum(false),
4141
ExclusiveMinimum(false),
4242
MultipleOf(0),
43+
MinProperties(0),
44+
MaxProperties(0),
4345

4446
// string markers
4547

@@ -145,6 +147,14 @@ type MinItems int
145147
// UniqueItems specifies that all items in this list must be unique.
146148
type UniqueItems bool
147149

150+
// +controllertools:marker:generateHelp:category="CRD validation"
151+
// MaxProperties restricts the number of keys in an object
152+
type MaxProperties int
153+
154+
// +controllertools:marker:generateHelp:category="CRD validation"
155+
// MinProperties restricts the number of keys in an object
156+
type MinProperties int
157+
148158
// +controllertools:marker:generateHelp:category="CRD validation"
149159
// Enum specifies that this (scalar) field is restricted to the *exact* values specified here.
150160
type Enum []interface{}
@@ -289,6 +299,24 @@ func (m UniqueItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
289299
return nil
290300
}
291301

302+
func (m MinProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
303+
if schema.Type != "object" {
304+
return fmt.Errorf("must apply minproperties to an object")
305+
}
306+
val := int64(m)
307+
schema.MinProperties = &val
308+
return nil
309+
}
310+
311+
func (m MaxProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
312+
if schema.Type != "object" {
313+
return fmt.Errorf("must apply maxproperties to an object")
314+
}
315+
val := int64(m)
316+
schema.MaxProperties = &val
317+
return nil
318+
}
319+
292320
func (m Enum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
293321
// TODO(directxman12): this is a bit hacky -- we should
294322
// probably support AnyType better + using the schema structure

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crd/testdata/cronjob_types.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ type CronJobSpec struct {
149149
// This tests that type references are properly flattened
150150
// +kubebuilder:validation:optional
151151
JustNestedObject *JustNestedObject `json:"justNestedObject,omitempty"`
152+
153+
// This tests that min/max properties work
154+
MinMaxProperties MinMaxObject `json:"minMaxProperties,omitempty"`
152155
}
153156

154157
type NestedObject struct {
@@ -158,6 +161,14 @@ type NestedObject struct {
158161

159162
type JustNestedObject NestedObject
160163

164+
// +kubebuilder:validation:MinProperties=1
165+
// +kubebuilder:validation:MaxProperties=2
166+
type MinMaxObject struct {
167+
Foo string `json:"foo,omitempty"`
168+
Bar string `json:"bar,omitempty"`
169+
Baz string `json:"baz,omitempty"`
170+
}
171+
161172
type RootObject struct {
162173
Nested NestedObject `json:"nested"`
163174
}

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5043,6 +5043,18 @@ spec:
50435043
fields
50445044
type: object
50455045
x-kubernetes-map-type: granular
5046+
minMaxProperties:
5047+
description: This tests that min/max properties work
5048+
maxProperties: 2
5049+
minProperties: 1
5050+
properties:
5051+
bar:
5052+
type: string
5053+
baz:
5054+
type: string
5055+
foo:
5056+
type: string
5057+
type: object
50465058
noReallySuspend:
50475059
description: This flag is like suspend, but for when you really mean
50485060
it. It helps test the +kubebuilder:validation:Type marker.

0 commit comments

Comments
 (0)