Validator v10 获取单个字段上的错误列表

huangapple go评论89阅读模式
英文:

Validator v10 get list of error on single field

问题

有没有办法获取每个验证失败的错误列表?
目前我只收到一个错误,而且是来自第一个验证失败的地方。

我正在使用golang和Validator v10。

type User struct {
    ID          string   `param:"id" json:"id" validate:"required"`
    Name        string   `json:"name" validate:"required,max=100"`
    Permissions []string `json:"permissions" validate:"permission_list,distinct_list"`
}

所以,如果Permissions字段在两个验证中都失败了,我可以得到以下结果:

{
    "validationErrors": {
        "permissions": [
            "permissions must be a list of valid permissions",
            "permissions must be uniques"
        ]
    }
}
英文:

Is there a way to get the list of errors for each validation that fails?
Currently I'm just receiving one error, and comes from the first validation that fails.

I'm using golang and Validator v10

type User struct {
	ID          string   `param:"id" json:"id" validate:"required"`
	Name        string   `json:"name" validate:"required,max=100"`
	Permissions []string `json:"permissions" validate:"permission_list,distinct_list"`
}

So if the field Permissions fails on both I can get as result:

{
    "validationErrors": {
        "permissions": [
            "permissions must be a list of valid permissions",
            "permissions must be uniques",
        ]
    }
}

答案1

得分: 3

你需要检查返回的错误。

package main

import (
	"fmt"
	"github.com/go-playground/validator/v10"
)

type User struct {
	FirstName      string     `validate:"required"`
	LastName       string     `validate:"required"`
}

func main() {
	testUser := User{}

	v := validator.New()
	
	if err := v.Struct(testUser); err != nil {
		validationErrors := err.(validator.ValidationErrors)
		for _, validationError := range validationErrors {
			fmt.Println(validationError.Error())
		}
	}
}

示例:
https://play.golang.com/p/HFdHrPKiYwF

请在 https://github.com/go-playground/validator/blob/master/errors.go 上检查代码,了解 ValidationErrorsFieldError 上可用的方法。

英文:

You'll need to inspect the error returned.

package main

import (
	"fmt"
	"github.com/go-playground/validator/v10"
)

type User struct {
	FirstName      string     `validate:"required"`
	LastName       string     `validate:"required"`
}

func main() {
	testUser := User{}

	v := validator.New()
	
	if err := v.Struct(testUser); err != nil {
		validationErrors := err.(validator.ValidationErrors)
		for _, validationError := range validationErrors {
			fmt.Println(validationError.Error())
		}
	}
}

Example:
https://play.golang.com/p/HFdHrPKiYwF

Check the code at https://github.com/go-playground/validator/blob/master/errors.go for methods available on ValidationErrors and FieldError.

答案2

得分: 0

如果你需要的不仅仅是检查一个有效条件并返回结果,而是需要检查每个失败的有效条件,那么你需要对该包进行一些改进,就像我的情况一样。我需要从validator.go 源代码的第480行中删除return语句。

英文:

If what you need is not just only check one validate condition then return it, but need to check every validate that failed instead, you need to do a little enhancement on the package, like my case. I need to remove return statement from validator.go source on line 480

huangapple
  • 本文由 发表于 2022年3月10日 02:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/71414179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定