Skip to content

Commit

Permalink
chore: update readme and some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 7, 2022
1 parent 5fcd698 commit 5815ccb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The package is a generic Go data validate and filter tool library.
- Support add custom filter/validator func
- Support scene settings, verify different fields in different scenes
- Support custom error messages, field translates.
- Can use `message`, `label` tags in struct
- Customizable i18n aware error messages, built in `en`, `zh-CN`, `zh-TW`
- Built-in common data type filter/converter. see [Built In Filters](#built-in-filters)
- Many commonly used validators have been built in(**> 70**), see [Built In Validators](#built-in-validators)
Expand Down Expand Up @@ -45,6 +46,7 @@ The struct can implement three interfaces methods, which is convenient to do som

- Support configuration field mapping through structure tag, read the value of `json` tag by default
- Support configuration error message via structure's `message` tag
- Support configuration field translation via structure's `label` tag

```go
package main
Expand All @@ -59,7 +61,7 @@ import (
// UserForm struct
type UserForm struct {
Name string `validate:"required|minLen:7"`
Email string `validate:"email" message:"email is invalid"`
Email string `validate:"email" message:"email is invalid" label:"User Email"`
Age int `validate:"required|int|min:1|max:99" message:"int:age must int| min: age min value is 1"`
CreateAt int `validate:"min:1"`
Safe int `validate:"-"`
Expand Down Expand Up @@ -265,18 +267,27 @@ You can adjust some processing logic of the validator by changing the global opt
```go
// GlobalOption settings for validate
type GlobalOption struct {
// FilterTag name in the struct tags. default: filter
// FilterTag name in the struct tags.
//
// default: filter
FilterTag string
// ValidateTag in the struct tags. default: validate
// ValidateTag in the struct tags.
//
// default: validate
ValidateTag string
// FieldTag the output field name in the struct tags.
// it as placeholder on error message.
//
// default: json
FieldTag string
// LabelTag the display name in the struct tags.
// use for define field translate name on error. default: label
// use for define field translate name on error.
//
// default: label
LabelTag string
// MessageTag define error message for the field.
//
// default: message
MessageTag string
// StopOnError If true: An error occurs, it will cease to continue to verify
StopOnError bool
Expand Down
9 changes: 6 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Go通用的数据验证与过滤库,使用简单,内置大部分常用验证
- 已经内置了超多(**>20** 个)常用的过滤器,查看 [内置过滤器](#built-in-filters)
- 方便的获取错误信息,验证后的安全数据获取(_只会收集有规则检查过的数据_)
- 支持自定义每个验证的错误消息,字段翻译,消息翻译(内置`en` `zh-CN` `zh-TW`)
- 在结构体上可以使用 `message`, `label` 标签定义消息翻译
- 完善的单元测试,测试覆盖率 **> 90%**

> 受到 [albrow/forms](https://github.com/albrow/forms) [asaskevich/govalidator](https://github.com/asaskevich/govalidator) [inhere/php-validate](https://github.com/inhere/php-validate) 这些项目的启发. 非常感谢它们
Expand All @@ -41,8 +42,10 @@ Please see the English introduction **[README](README.md)**

**`v1.2.1+` 更新**:

- 支持通过结构体tag配置字段映射,默认读取 `json` 标签的值
- 支持通过结构体配置字段输出名称,默认读取 `json` 标签的值
- 支持通过结构体的 `message` tag 配置错误消息
- 支持通过结构体的 `label` tag 字段映射/翻译


```go
package main
Expand All @@ -57,7 +60,7 @@ import (
// UserForm struct
type UserForm struct {
Name string `validate:"required|minLen:7"`
Email string `validate:"email" message:"email is invalid"`
Email string `validate:"email" message:"email is invalid" label:"用户邮箱"`
Age int `validate:"required|int|min:1|max:99" message:"int:age must int| min: age min value is 1"`
CreateAt int `validate:"min:1"`
Safe int `validate:"-"`
Expand Down Expand Up @@ -96,7 +99,7 @@ func (f UserForm) Messages() map[string]string {
func (f UserForm) Translates() map[string]string {
return validate.MS{
"Name": "用户名称",
"Email": "用户Email",
"Email": "用户邮箱",
"ExtInfo.Homepage": "用户主页",
}
}
Expand Down
12 changes: 7 additions & 5 deletions data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ type CustomMessagesFace interface {
Messages() map[string]string
}

// StructData definition
// StructData definition.
//
// more struct tags define please see GlobalOption
type StructData struct {
// source struct data, from user setting
src interface{}
Expand All @@ -186,13 +188,13 @@ type StructData struct {
fieldValues map[string]reflect.Value
// TODO field reflect values cache
fieldRftValues map[string]interface{}
// FieldTag name in the struct tags. for define filed translate
FieldTag string
// MessageTag define error message for the field.
MessageTag string
// FilterTag name in the struct tags.
//
// see GlobalOption.FilterTag
FilterTag string
// ValidateTag name in the struct tags.
//
// see GlobalOption.ValidateTag
ValidateTag string
}

Expand Down
20 changes: 15 additions & 5 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,27 @@ func (ms MS) String() string {

// GlobalOption settings for validate
type GlobalOption struct {
// FilterTag name in the struct tags. default: filter
// FilterTag name in the struct tags.
//
// default: filter
FilterTag string
// ValidateTag in the struct tags. default: validate
// ValidateTag in the struct tags.
//
// default: validate
ValidateTag string
// FieldTag the output field name in the struct tags.
// it as placeholder on error message. default: json
// it as placeholder on error message.
//
// default: json
FieldTag string
// LabelTag the display name in the struct tags.
// use for define field translate name on error. default: label
// use for define field translate name on error.
//
// default: label
LabelTag string
// MessageTag define error message for the field. default: message
// MessageTag define error message for the field.
//
// default: message
MessageTag string
// StopOnError If true: An error occurs, it will cease to continue to verify
StopOnError bool
Expand Down

0 comments on commit 5815ccb

Please sign in to comment.