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 5, 2020
1 parent 2f203e4 commit c23e2dc
Show file tree
Hide file tree
Showing 2 changed files with 50 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.

0 comments on commit c23e2dc

Please sign in to comment.