Skip to content

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

@adeptxaoca

Description

@adeptxaoca

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions