Skip to content

fix(gen): generate valid code for object-level x-ogen-validate - #1772

Merged
ernado merged 2 commits into
ogen-go:mainfrom
belomaxorka:fix/object-level-ogen-validate-1718
Nov 13, 2026
Merged

ernado merged 2 commits into
ogen-go:mainfrom
belomaxorka:fix/object-level-ogen-validate-1718

Conversation

@belomaxorka

Copy link
Copy Markdown

Object-level x-ogen-validate (set on an object schema rather than on a property) generated a call to validate.ValidateWith, which doesn't exist, so the generated package didn't build:

oas_validators_gen.go:18:22: undefined: validate.ValidateWith

Changes:

  • The struct's Validate() now calls validate.Ogen(name, *s, params). The validator receives the struct value, the same way field- and map-level validators receive their values.
  • The generic validate template no longer emits validate.Ogen for struct types at call sites, because Validate() already runs these validators. Without this, every request/response validation would call the validator twice: once from Validate() with *T, and once at the call site with T on the server (or *T on the client). A validator that type-asserts either type would then reject valid input.

Field-level and map-level validators are unchanged. The generated code is in a separate chore: commit generated files commit, and the only generated file that changes is test_ogen_validate/oas_validators_gen.go.

Testing:

  • Added an object-level validator (minAge) to _testdata/positive/ogen_validate.yaml. In ogen_validate_test.go, it type-asserts User, and I added a failing-input case plus a server request that checks the validator runs exactly once.
  • go test ./..., go test ./... in examples, go vet ./... and golangci-lint run pass (Go 1.26). make generate examples produces no other diff.

Fixes #1718

Object-level validators were generated as calls to validate.ValidateWith,
which does not exist, so the generated package did not compile.

Call validate.Ogen with the struct value instead, the same way field and
map validators receive their values. Since the struct's Validate() now
runs object-level validators, skip them in the generic validate template
for struct types. Otherwise every request/response validation would call
them twice, once from Validate() and once at the call site, with the
value passed as a pointer in one place and by value in the other.
@ernado ernado closed this Nov 13, 2026
@ernado ernado reopened this Nov 13, 2026
@ernado
ernado merged commit c72b101 into ogen-go:main Nov 13, 2026
17 checks passed
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.

When using a custom validator on an object, an invalid code is generated.

2 participants