Skip to content

Slice validation errors miss data on marshal #282

@manicar2093

Description

@manicar2093

System (please complete the following information):

  • OS: masOs
  • GO Version: 1.23.3
  • Pkg Version: 1.5.2

Describe the bug

When an struct has json tags and has an slice type in it error path is not complete

To Reproduce

package main

import (
	"encoding/json"
	"fmt"
	"github.com/gookit/validate"
	"os"
)

type (
	InputGood struct {
		Data []DataGood
	}
	DataGood struct {
		Name string `validate:"required"`
	}

	InputBad struct {
		Data []DataBad `json:"data"`
	}

	DataBad struct {
		Name string `json:"name" validate:"required"`
	}
)

func main() {
	inputGood := InputGood{
		Data: []DataGood{
			{},
			{},
		},
	}
	validateData(inputGood)

	inputBad := InputBad{
		Data: []DataBad{
			{},
			{},
		},
	}
	validateData(inputBad)
}

func validateData(i any) {
	v := validate.Struct(i)
	v.StopOnError = false
	v.Validate()
	_ = json.NewEncoder(os.Stdout).Encode(v.Errors)
	fmt.Println()
	fmt.Println()
	fmt.Println()

}

Expected behavior

Show error path using json tags instead struct attributes' names

Screenshots

This is the result of running above code
image

Additional context

It's just that 😃 Thank you for your time! 🎆

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions