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

validate.Errors希望增加一个trans功能 #11

Open
dulumao opened this issue May 13, 2019 · 13 comments
Open

validate.Errors希望增加一个trans功能 #11

dulumao opened this issue May 13, 2019 · 13 comments
Assignees
Labels
help wanted Extra attention is needed suggestion suggestion

Comments

@dulumao
Copy link
Member

dulumao commented May 13, 2019

在集成echo的发现的问题,最后单独实现了一个validate接口给echo,内部调用struct,最终给前端的时候,是error接口,强行转回validate.Errors类型,但是这个时候已经没办法重新格式化语种

@dulumao
Copy link
Member Author

dulumao commented May 13, 2019

目前的实现的方式是struct,指定,message方法,来统一调用一个语言转化方法,但是拿不到当前的context,判断游览器是什么语言,有点麻烦

@inhere inhere added the help wanted Extra attention is needed label May 13, 2019
@inhere
Copy link
Member

inhere commented May 13, 2019

😄 不是太明白,你可以贴下伪代码说明下现在的不足,和希望实现的效果。

@dulumao
Copy link
Member Author

dulumao commented May 13, 2019

echo 框架

type Validator struct {
}

func (Validator) Valdate(i interface{}) error {
	v := validate.Struct(i)

	if v.Validate() {
		return nil
	}

	return v.Errors
}

func main() {
	e := echo.New()

	/* echo framework validator inteface
	Validator interface {
		Validate(i interface{}) error
	}
	*/
	e.Validator = new(Validator)
	e.GET("/", func(c echo.Context) error {
		var form = new(struct {
			Name string `validate:"required" json:"page" form:"name" query:"name"`
		})

		if err := c.Bind(form); err != nil {
			panic(err)
		}

		if err:=c.Validate(form);err !=nil {
			errs := err.(validate.Errors)
			// this code is default translator
			dump.DD(errs.All())
			// need add func like this
			// errs.Trans(map[string]inteface{}{
			// "Name.required":"user name must required"
			// })
			// and we can use custom locale like this
			// GetBrowserLocale() = zh-cn,GetBrowserLocalePack will retun map type
			// errs.Trans(GetBrowserLocalePack(GetBrowserLocale()))
		}
	})
}

@inhere
Copy link
Member

inhere commented May 20, 2019

没想好你这种要怎么做 😄

@dulumao
Copy link
Member Author

dulumao commented May 20, 2019

没想好你这种要怎么做 😄

也没什么大事,我自己重写了context,在外层重新封装了一个返回*validate.Validation来解决这个问题,不过有没有什么办法能拿具体每个字段的错误类型,比如是required错误还是其他的错误类型,现在就是比如字段名字Name,里面一个数组字典,对应的错误信息,没有具体的错误类型

@inhere
Copy link
Member

inhere commented May 20, 2019

字段名 和 错误消息 比较重要。 验证错误的类型(验证器名称)好像没啥用啊

@dulumao
Copy link
Member Author

dulumao commented May 21, 2019

还是很有用的,有些特殊的验证器,在前端要特别提示用户,如果没有这个来判断,就没法知道,具体哪些才是重点了

@inhere
Copy link
Member

inhere commented May 21, 2019

😄 前端不是该根据 字段来提示吗

@inhere
Copy link
Member

inhere commented Jul 24, 2019

现在错误是这样存储的。 好像不太好放 验证器名称进去。。。

{	
	"username": ["error msg 0", "error msg 1"]
	"age": ["error msg 2"]
}

如果改成这样子设计,到是可以加个字段存 验证器名称

[
	{
		"field": "username",
		"error": "error msg 0"
	},
	{
		"field": "username",
		"error": "error msg 1"
	},
	{
		"field": "age",
		"error": "error msg 2"
	}
]

@dulumao
Copy link
Member Author

dulumao commented Jul 29, 2019

现在错误是这样存储的。 好像不太好放 验证器名称进去。。。

{	
	"username": ["error msg 0", "error msg 1"]
	"age": ["error msg 2"]
}

如果改成这样子设计,到是可以加个字段存 验证器名称

[
	{
		"field": "username",
		"error": "error msg 0"
	},
	{
		"field": "username",
		"error": "error msg 1"
	},
	{
		"field": "age",
		"error": "error msg 2"
	}
]

这样不好啊,还是

{	
	"username": {required:"field must be required", email:"field must be email type"}
	"age": [int:"field must int type"]
}

@inhere inhere added the suggestion suggestion label Jul 29, 2019
@inhere
Copy link
Member

inhere commented Jul 29, 2019

这样啊 也可以。 不过要改的话跟之前数据格式不兼容了,估计得改到 1.2 去

@dulumao
Copy link
Member Author

dulumao commented Jul 29, 2019

这样啊 也可以。 不过要改的话跟之前数据格式不兼容了,估计得改到 1.2 去

嗯,我发现你的包做的都不错啊,有没有微信可以交流

@inhere
Copy link
Member

inhere commented Jul 29, 2019

@dulumao 哈哈 感谢支持。都是一些简单的库,自己有用到,就自己写了。

有想法欢迎参与维护,非常支持。

可以交流啊,我微信 inhereat 搜一下就可以了。

@inhere inhere self-assigned this Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed suggestion suggestion
Projects
None yet
Development

No branches or pull requests

2 participants