controller-tools icon indicating copy to clipboard operation
controller-tools copied to clipboard

Support for enum within an array

Open braghettos opened this issue 5 months ago • 3 comments

Hi everyone,

I was looking for a way to support the following json schema:

          "properties": {
            "allowedResources": {
              "description": "the list of resources that are allowed to be children of this widget or referenced by it",
              "type": "array",
              "items": {
                "type": "string",
                "enum": [
                  "barcharts",
                  "buttons",
                  "eventlists",
                  "filters",
                  "flowcharts",
                  "forms",
                  "linecharts",
                  "markdowns",
                  "panels",
                  "paragraphs",
                  "piecharts",
                  "tables",
                  "tablists",
                  "yamlviewers"
                ]
              }
            }

Is there a marker that allows this use case?

Thanks for any feedback!

braghettos avatar Sep 16 '25 08:09 braghettos

You should be able to create a string type alias and then use that as the list element type

// +kubebuilder:validation:Enum:=A;B;C
type Foo string

...
// foos is a list of Foos.
// Valid options are A, B and C.
Foos []Foo `json:"foo,omitempty"

JoelSpeed avatar Sep 16 '25 10:09 JoelSpeed

@JoelSpeed is there another way to avoid alias in code? I would like to avoid this manual approach.

braghettos avatar Sep 25 '25 12:09 braghettos

Not presently that I'm aware of no. In upstream/core Kube types, all enums are type aliases like this. It's useful when coding, for example, when you build a switch, linters can check you've handled all potential values

JoelSpeed avatar Oct 03 '25 16:10 JoelSpeed

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Jan 01 '26 17:01 k8s-triage-robot