Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method Struct(...) forms keys for embedded structures incorrectly #776

Open
adeptxaoca opened this issue May 28, 2021 · 0 comments · May be fixed by #1211
Open

Method Struct(...) forms keys for embedded structures incorrectly #776

adeptxaoca opened this issue May 28, 2021 · 0 comments · May be fixed by #1211

Comments

@adeptxaoca
Copy link

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

The validator sees no difference between an additional field of type "struct" and embedded structures. This complicates further post-processing and forces you to pre-validate each structure and then merge the results.

Is there any way to correct this behavior? For example, by adding an additional tag.

Code sample, to showcase or reproduce:

package main

import (
	"fmt"

	"github.com/go-playground/validator/v10"
)

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

type GroupA struct {
	Admin User
}

type GroupB struct {
	User
}

func main() {
	v := validator.New()

	errs := v.Struct(new(GroupA))
	fmt.Println("GroupA: ", errs)

	errs = v.Struct(new(GroupB))
	fmt.Println("GroupB: ", errs)
}

Output

GroupA:  Key: 'GroupA.Admin.Name' Error:Field validation for 'Name' failed on the 'required' tag
GroupB:  Key: 'GroupB.User.Name' Error:Field validation for 'Name' failed on the 'required' tag

But for "GroupB" the key "GroupB.Name" is expected.

@abemedia abemedia linked a pull request Jan 10, 2024 that will close this issue
1 task
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 a pull request may close this issue.

1 participant