Skip to content

Commit

Permalink
Add min/max properties validation
Browse files Browse the repository at this point in the history
  • Loading branch information
moolen committed Sep 11, 2020
1 parent 2f203e4 commit b99995b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var ValidationMarkers = mustMakeAllWithPrefix("kubebuilder:validation", markers.
ExclusiveMaximum(false),
ExclusiveMinimum(false),
MultipleOf(0),
MinProperties(0),
MaxProperties(0),

// string markers

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

// +controllertools:marker:generateHelp:category="CRD validation"
// MaxProperties restricts the number of keys in an object
type MaxProperties int

// +controllertools:marker:generateHelp:category="CRD validation"
// MinProperties restricts the number of keys in an object
type MinProperties int

// +controllertools:marker:generateHelp:category="CRD validation"
// Enum specifies that this (scalar) field is restricted to the *exact* values specified here.
type Enum []interface{}
Expand Down Expand Up @@ -289,6 +299,24 @@ func (m UniqueItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
return nil
}

func (m MinProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
if schema.Type != "object" {
return fmt.Errorf("must apply minproperties to an object")
}
val := int64(m)
schema.MinProperties = &val
return nil
}

func (m MaxProperties) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
if schema.Type != "object" {
return fmt.Errorf("must apply maxproperties to an object")
}
val := int64(m)
schema.MaxProperties = &val
return nil
}

func (m Enum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
// TODO(directxman12): this is a bit hacky -- we should
// probably support AnyType better + using the schema structure
Expand Down
22 changes: 22 additions & 0 deletions pkg/crd/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ type CronJobSpec struct {
// This tests that type references are properly flattened
// +kubebuilder:validation:optional
JustNestedObject *JustNestedObject `json:"justNestedObject,omitempty"`

// This tests that min/max properties work
MinMaxProperties MinMaxObject `json:"minMaxProperties,omitempty"`
}

type NestedObject struct {
Expand All @@ -158,6 +161,14 @@ type NestedObject struct {

type JustNestedObject NestedObject

// +kubebuilder:validation:MinProperties=1
// +kubebuilder:validation:MaxProperties=2
type MinMaxObject struct {
Foo string `json:"foo,omitempty"`
Bar string `json:"bar,omitempty"`
Baz string `json:"baz,omitempty"`
}

type RootObject struct {
Nested NestedObject `json:"nested"`
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5043,6 +5043,18 @@ spec:
fields
type: object
x-kubernetes-map-type: granular
minMaxProperties:
description: This tests that min/max properties work
maxProperties: 2
minProperties: 1
properties:
bar:
type: string
baz:
type: string
foo:
type: string
type: object
noReallySuspend:
description: This flag is like suspend, but for when you really mean
it. It helps test the +kubebuilder:validation:Type marker.
Expand Down

0 comments on commit b99995b

Please sign in to comment.