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

Slice validation errors miss data on marshal #282

Open
manicar2093 opened this issue Jan 10, 2025 · 0 comments
Open

Slice validation errors miss data on marshal #282

manicar2093 opened this issue Jan 10, 2025 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@manicar2093
Copy link

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! 🎆

@inhere inhere added the bug Something isn't working label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants