Skip to content

Update enum generation to generate a map to validate enums.#2181

Closed
AndreasAbdi wants to merge 1 commit into
oapi-codegen:mainfrom
AndreasAbdi:pins
Closed

Update enum generation to generate a map to validate enums.#2181
AndreasAbdi wants to merge 1 commit into
oapi-codegen:mainfrom
AndreasAbdi:pins

Conversation

@AndreasAbdi

Copy link
Copy Markdown

Customers often need to validate whether a value is a legit enum value.
We implement that validation by creating helper functions.

With the helper function, customers no longer have to manually write their own version of the helper.

i.e.

const (
	ClientTypeWithNamesExtensionActive  ClientTypeWithNamesExtension = "ACT"
	ClientTypeWithNamesExtensionExpired ClientTypeWithNamesExtension = "EXP"
)

var ClientTypeWithNamesExtensionValues = map[ClientTypeWithNamesExtension]struct{}{
	ClientTypeWithNamesExtensionActive:  {},
	ClientTypeWithNamesExtensionExpired: {},
}

func (s ClientTypeWithNamesExtension) IsValid() bool {
	_, ok := ClientTypeWithNamesExtensionValues[s]
	return ok
}

and then the customer uses it like:

func legit() {
	k := ClientTypeWithNamesExtension("")
	isValid := k.IsValid()
	fmt.Print(isValid)
}

Customers often need to validate whether a value is a legit enum value.
We implement that validation by creating helper functions.

With the helper function, customers no longer have to manually
write their own version of the helper.
@AndreasAbdi AndreasAbdi requested a review from a team as a code owner January 9, 2026 07:43
@AndreasAbdi AndreasAbdi changed the title Update enum generation to generate a map. Update enum generation to generate a map to validate enums. Jan 9, 2026

@jamietanna jamietanna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we probably shouldn't export the map as then it's mutable outside of the package?

It's probably also worth making this opt-in, via output-options, so it's something that reduces generated code changes

@mromaszewicz

Copy link
Copy Markdown
Member

Can your problem be solved via request validation middleware? For example,

In all the servers that I've written, I've left the problem of validating inputs against schemas to other, excellent projects that already exist.

I think oapi-codegen should be as narrowly focused as a code generator as possible.

@jamietanna

Copy link
Copy Markdown
Member

While it could be, Marcin, I've found myself wanting something like this in the past - i.e. to add a unit test to clarify that all of my business layer states can correctly map to the state in the HTTP layer's enums

Related: #1537 / #1721

@mromaszewicz

Copy link
Copy Markdown
Member

What do you guys think of doing it like this? #2227

We don't need a map, we've got a code generator that can generate a nice Valid() function.

@mromaszewicz

Copy link
Copy Markdown
Member

Thanks for this PR, but I am closing it, because we have an alternate fix already merged. The Enum types have a Validate function now, which just uses a switch statement to check for valid values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants