Validation rules for object properties

38 views
Skip to first unread message

Patrik Polakovic

unread,
Dec 23, 2022, 5:24:57 AM12/23/22
to kubebuilder
Hi, I there a possibility to have object properties from specified enumeration set?
Something like this - not working example:
// +kubebuilder:validation:Enum=apple;banana;pear
type MyKey string

type Mapping {
    Fruit map[MyKey]string `json="fruit"`
}

Thank you

Joel Speed

unread,
Dec 23, 2022, 5:57:47 AM12/23/22
to Patrik Polakovic, kubebuilder
Hey Patrik,

What you want to achieve is possible, but you have to approach it in a slightly different way.

Kube API conventions recommend against using `map` types within APIs, instead you use a list and set some special keys on it, that make it behave like a map.

So the API type that you’d want for this would look something like:

// +kubebuilder:validation:Enum:=Apple;Pear;Banana
type FruitType string

type FruitSpec struct {
  Fruit FruitType `json:"fruit”`

  // Add other fields as required
}

type Parent struct {
  // +listType=map
  // +listMapKey=fruit
  Fruits []FruitSpec
}

Then, when you try to create items in the Fruits list, the listType will validate that each combination of `listMapKey` is unique within the list, and that your `Fruit` values match only those within your enumerated list.

Hope that helps,
Joel

Joel Speed

He/Him/His

Principal Software Engineer, OpenShift

Red Hat



--
You received this message because you are subscribed to the Google Groups "kubebuilder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kubebuilder...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kubebuilder/902f35e9-1e38-424e-ae76-707a45d9159en%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages