+
Skip to content

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

@cubonguilty

Description

@cubonguilty

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

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载