Skip to content

feat: implement ValidatorValuer interface feature#1416

Merged
nodivbyzero merged 1 commit intogo-playground:masterfrom
thommeo:feat/validation-valuer
Mar 9, 2026
Merged

feat: implement ValidatorValuer interface feature#1416
nodivbyzero merged 1 commit intogo-playground:masterfrom
thommeo:feat/validation-valuer

Conversation

@thommeo
Copy link
Contributor

@thommeo thommeo commented Apr 16, 2025

Problem

In order to have custom type validations for generic types, every possible type has to be registered with RegisterCustomTypeFunc to return the underlying type. It is especially relevant for nullable types e.g. sql.Null[T].

Discussed here: #1232

Fixes Or Enhances

  • Adds a mechanism for validating generic types without using RegisterCustomTypeFunc by introducing ValidatorValuer interface.

Example:

type Nullable[T any] struct {
	Data T
}

func (t Nullable[T]) ValidatorValue() any {
	return t.Data
}

type Config struct {
	Name string `validate:"required"`
}

type Record struct {
	Config Nullable[Config] `validate:"required"`
}

func TestValidatorValuerInterface2(t *testing.T) {
	s := Record{
		Config: Nullable[Config]{},
	}
	v := New()
	errs := v.Struct(s)
	t.Fatalf("Error: %v", errs.Error())
}

// Key: 'Record.Config.Name' Error:Field validation for 'Name' failed on the 'required' tag
// Notice: no Data in the Record.Config.Name

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@thommeo thommeo requested a review from a team as a code owner April 16, 2025 12:54
@coveralls
Copy link

coveralls commented Apr 16, 2025

Coverage Status

coverage: 73.683% (+0.006%) from 73.677%
when pulling 05867e9 on thommeo:feat/validation-valuer
into 6905468 on go-playground:master.

@thommeo thommeo force-pushed the feat/validation-valuer branch from 7222de7 to a4b2773 Compare April 27, 2025 19:48
zemzale
zemzale previously approved these changes Apr 30, 2025
Copy link
Member

@zemzale zemzale left a comment

Choose a reason for hiding this comment

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

The good looks good.

My only concerns would maybe be the naming of the interface and it's function.

@thommeo thommeo force-pushed the feat/validation-valuer branch from a4b2773 to 2996074 Compare May 1, 2025 12:48
@thommeo thommeo requested a review from zemzale May 2, 2025 14:45
@thommeo thommeo force-pushed the feat/validation-valuer branch from 2996074 to 62dc169 Compare May 5, 2025 07:32
@vinijabes
Copy link

Do we have any updates on this?

@thommeo thommeo force-pushed the feat/validation-valuer branch from 62dc169 to 05867e9 Compare September 19, 2025 21:10
@thommeo
Copy link
Contributor Author

thommeo commented Nov 23, 2025

Do we have any updates on this?

Still waiting for review by @go-playground/validator-maintainers

Copy link
Member

@zemzale zemzale left a comment

Choose a reason for hiding this comment

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

The code itself is good.

Just not fully sure about the whole thing just as written in the discussion, that this is very implicit API that would tie in the validator with your types.

Would want more opinions on this before merging.

@kayla-barenz-kr
Copy link

kayla-barenz-kr commented Feb 20, 2026

Very much looking forward to this feature. @zemzale Any updates here?

@mrWumpus
Copy link

Is this issue just waiting for more input before merging? I think this is a fine implementation for the problem. I've had to work around this so far and this simple fix would help a lot actually.

Seems the concern is that using an interface on your type would make you have to be aware of the validation framework for that type, but I'd argue that in these custom cases you have to do that any way. Especially without this fix you have to put in extra validator code for that type. Granted, it doesn't have to be directly tied to that type. But if that's an issue, you don't have to use it.

So I vote to push this through. Thanks for the fix @thommeo and for the time to review @zemzale

@nodivbyzero
Copy link
Contributor

Sorry for joining the conversation late.
I think this is a valuable feature, but the PR is missing some key pieces - specifically documentation and examples. Let's add those, and then we can move forward with merging it.

@kayla-barenz-kr
Copy link

@thommeo are you able to add the documentation? or if they are unable to, is this something maintainers can add in a separate PR @nodivbyzero ?

@nodivbyzero
Copy link
Contributor

@kayla-barenz-kr Yep, we can add it in a separate PR 👍
Feel free to open a new issue so we can track the request.

@nodivbyzero nodivbyzero merged commit 42927a0 into go-playground:master Mar 9, 2026
8 checks passed
wofiporia added a commit to wofiporia/validator that referenced this pull request Mar 10, 2026
Add documentation for the Valuer interface
  introduced in go-playground#1416.

  - add Valuer interface section and example snippet
  in doc.go
  - add a new runnable example at _examples/valuer/
  main.go
  - add Valuer Interface entry in README examples list

  Refs go-playground#1538
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.

7 participants