feat: add Valid() method to generated enum types#2227
Merged
mromaszewicz merged 2 commits intooapi-codegen:mainfrom Feb 17, 2026
Merged
feat: add Valid() method to generated enum types#2227mromaszewicz merged 2 commits intooapi-codegen:mainfrom
mromaszewicz merged 2 commits intooapi-codegen:mainfrom
Conversation
Generate a Valid() bool method on each enum type that returns true when the receiver matches one of the defined enum constants and false otherwise. This lets callers validate enum values at runtime with a simple method call instead of hand-writing switch statements. This is default-on because it only adds a new method to an already generated type -- existing code that does not call Valid() is completely unaffected, so this should be very unlikely to break anything. Co-Authored-By: Claude Opus 4.6 <[email protected]>
58e61de to
2615c6e
Compare
jamietanna
reviewed
Feb 17, 2026
Member
jamietanna
left a comment
There was a problem hiding this comment.
From #2181 I do wonder if it'd be useful to be able to get the list of valid enum names, though, i.e. as a slice
But maybe we can see if this solves the underlying need folks have
It turns out we don't need a sorted map of names, since we have the same thing already present on the template context in a different way. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Member
Author
I think it does, because you can always validate in your own code with something like |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Generate a Valid() bool method on each enum type that returns true when the receiver matches one of the defined enum constants and false otherwise. This lets callers validate enum values at runtime with a simple method call instead of hand-writing switch statements.
This is default-on because it only adds a new method to an already generated type -- existing code that does not call Valid() is completely unaffected, so this should be very unlikely to break anything.