validate icon indicating copy to clipboard operation
validate copied to clipboard

Identifying the First Failed Field During Validation (StopOnError=true)

Open cubonguilty opened this issue 1 year ago • 1 comments

I'm using the go-playground/validator library in Go, with the StopOnError=true setting enabled. During validation, I want to determine which field failed the validation and why. For example:

type SomeStruct struct { Name string validate:"required"Email stringvalidate:"required,email"` }

d := &SomeStruct{ Name: "John", Email: "not an email", }

err := validate.Struct(d) if err != nil { // How can I extract the specific field (Email) that failed validation here? } `

In this case, I expect the validation to stop at the first error (StopOnError=true) and want to extract the following details:

  • The name of the failed field (e.g., Email).

  • The validation tag or reason for failure (e.g., email).

How can I achieve this with the go-playground/validator library

cubonguilty avatar Dec 01 '24 15:12 cubonguilty

go-playground/validator ?

:) Please check his documentation

inhere avatar Dec 02 '24 01:12 inhere