Skip to content

feat: add Valid() method to generated enum types - #2227

Merged
mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:feat/enum-validation
Feb 17, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:feat/enum-validation

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

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.

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 <noreply@anthropic.com>

@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.

Nice!

@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.

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 <noreply@anthropic.com>
@mromaszewicz

Copy link
Copy Markdown
Member Author

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

I think it does, because you can always validate in your own code with something like MyEnum(x).Valid(). We can certainly return all the enum values in the future if needed, or even make a Stringer out of it.

@mromaszewicz
mromaszewicz merged commit 4b72bdb into oapi-codegen:main Feb 17, 2026
26 checks passed
@jamietanna jamietanna added the enhancement New feature or request label Feb 27, 2026
@rkosegi

rkosegi commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

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.

If this is default-on, how can we turn it off explicitly?
I have a case where I need to add method to generated type which has pointer receiver and generated Valid() method has value receiver. It compiles but it's discouraged and I actually don't need Valid() in this case.

@mromaszewicz
mromaszewicz deleted the feat/enum-validation branch April 23, 2026 16:57
@mromaszewicz

Copy link
Copy Markdown
Member Author

Ha! You are right, @rkosegi , I forgot to add the flag. I will do so in a subsequent commit. Whenever I add something I think is useful, it inevitably clashes with someone's code.

mromaszewicz added a commit that referenced this pull request Apr 23, 2026
Add an `output-options.skip-enum-validate` flag that suppresses the
`Valid()` method generated on enum types. The method is still emitted
by default; users whose code defines its own `Valid()` on the same type
can now opt out instead of seeing a compile-time conflict.

Relates to PR #2227, which introduced the `Valid()` method.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
render-oss-copybara-sync Bot pushed a commit to render-oss/cli that referenced this pull request Jul 7, 2026
* Regenerate client from latest public-api-schema

Catch-up regen of the generated REST client against the current
public-api-schema baseline (oapi-codegen v2.5.0). No tooling changes —
this only picks up schema drift accumulated since the last regen.

* Regenerate client with oapi-codegen v2.6.0

Regenerate the REST client with oapi-codegen v2.6.0. Each enum type now
carries a generated Valid() bool method (oapi-codegen/oapi-codegen#2227),
a native replacement for hand-maintained valid-value lists.

v2.6.0-generated code uses runtime.StyleParamWithOptions, so bump
github.com/oapi-codegen/runtime v1.1.1 -> v1.2.0. go build ./... passes.

* Fix test failure

GitOrigin-RevId: 59dd13da90f628f11a4cfc57c44f953817d1bc0c
render-oss-copybara-sync Bot pushed a commit to render-oss/sdk that referenced this pull request Jul 7, 2026
* Regenerate SDKs from latest public-api-schema

Catch-up regen of the Go, Python, and TypeScript SDKs against the current
public-api-schema baseline (oapi-codegen v2.5.0 for Go; redocly +
openapi-typescript + openapi-python-client for TS/Python), now that the
oapi-sdk.yaml import-mapping points at the correct render-oss/sdk/go
module path. The Go client gains several component subpackages it was
missing (artifactsources, autodeploy, envvar, ...).

* Regenerate Go SDK with oapi-codegen v2.6.0

Regenerate the Go SDK client with oapi-codegen v2.6.0. Each enum type now
carries a generated Valid() bool method (oapi-codegen/oapi-codegen#2227),
a native replacement for hand-maintained valid-value lists. Bump
github.com/oapi-codegen/runtime v1.1.1 -> v1.2.0 (required by
v2.6.0-generated code); go mod tidy also pulls testify to v1.9.0.

Only the Go SDK changes: the TypeScript and Python SDKs are generated by
openapi-typescript / openapi-python-client (unaffected by the Go codegen
bump) and regenerate identically.

* Python lint fixes

Run tox -e format-fix to fix lint warnings in python generated code. Add
E501 ignore rule to prevent noisy line-length warnings for long string
literals that black/ruff-format do not wrap.

GitOrigin-RevId: b888c72
render-oss-copybara-sync Bot pushed a commit to render-oss/render-mcp-server that referenced this pull request Jul 7, 2026
* Regenerate client from latest public-api-schema

Catch-up regen of the generated REST client against the current
public-api-schema baseline (oapi-codegen v2.5.0). Pulls in several
new component packages (artifactsources, autodeploy, envvar,
eventstatuses, sandboxes, storage, workflows).

* Fix hand-written code drift after client regen

Resolve go vet failures from regenerating the client against the latest
public-api-schema. These are hand-written call sites that referenced
symbols the schema/codegen has since moved or retyped:

- logs: LogDirectionParam -> LogDirection (parameter component renamed
  logDirectionParam -> directionParam upstream)
- postgres/keyvalue: region create field is now the typed client.Region
  enum, not a string; cast via (*client.Region)(...)
- service: EnvVarInput/EnvVarInputArray/EnvVarKeyValue moved into the
  client/envvar subpackage
- service: web-service Plan field is now *client.Plan (compute-plan
  rename); convert from the PaidPlan-validated value and update the test

* Regenerate client with oapi-codegen v2.6.0

Regenerate the REST client with oapi-codegen v2.6.0. Each enum type now
carries a generated Valid() bool method (oapi-codegen/oapi-codegen#2227),
a native replacement for hand-maintained valid-value lists. Bump
github.com/oapi-codegen/runtime v1.1.1 -> v1.2.0, required by
v2.6.0-generated code.

GitOrigin-RevId: c23e1297d93838d242cbe66fd36ac38ae53472b3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants