validator icon indicating copy to clipboard operation
validator copied to clipboard

Partial struct validation does not validate child structs

Open josefguenther opened this issue 7 months ago • 1 comments

  • [ X ] I have looked at the documentation here first?
  • [ X ] I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

When using StructPartial(), we should expect all fields specified to be validated. It does not validate struct properties, however, unless you specify the full path to individual fields inside child structs.

Code sample, to showcase or reproduce:


type Order struct {
	Id   string `validate:"required,number"`
	Item Item   `validate:"required"`
}
type Item struct {
	Id string `validate:"required,number"`
}
func TestMain(m *testing.M) {
	v := validator.New(validator.WithRequiredStructEnabled())
	order := Order{
		Id: "123",
		Item: Item{
			Id: "invalid",
		},
	}
	log.Printf("validation full: %+v", v.Struct(order))
	log.Printf("validation partial: %+v", v.StructPartial(order, "Item"))
	log.Printf("validation partial with path: %+v", v.StructPartial(order, "Item.Id"))
	log.Fatalf("done.")
	return
}

Output is as follows:

2024/07/19 15:26:07 validation full: Key: 'Order.Item.Id' Error:Field validation for 'Id' failed on the 'number' tag
2024/07/19 15:26:07 validation partial: <nil>
2024/07/19 15:26:07 validation partial with path: Key: 'Order.Item.Id' Error:Field validation for 'Id' failed on the 'number' tag
2024/07/19 15:26:07 done.

josefguenther avatar Jul 19 '24 22:07 josefguenther