Skip to content

Commit

Permalink
chore: update readme, add some doc for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 7, 2022
1 parent b52f1b3 commit 11b83cb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
61 changes: 57 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ type UserForm struct {
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:"-"`
UpdateAt time.Time `validate:"required"`
UpdateAt time.Time `validate:"required" message:"update time is required"`
Code string `validate:"customValidator"`
// ExtInfo nested struct
ExtInfo struct{
Homepage string `validate:"required" label:"Home Page"`
CityName string
}
} `validate:"required" label:"Home Page"`
}

// CustomValidator custom validator in the source struct.
Expand Down Expand Up @@ -109,7 +109,7 @@ type UserForm struct {
ExtInfo struct{
Homepage string `validate:"required"`
CityName string
}
} `validate:"required"`
}

// CustomValidator custom validator in the source struct.
Expand Down Expand Up @@ -148,7 +148,9 @@ func (f UserForm) Translates() map[string]string {
}
```

### Do validating
### Create and validating

Can use `validate.Struct(ptr)` quick create a validation instance. then call `v.Validate()` for validating.

```go
package main
Expand Down Expand Up @@ -318,6 +320,57 @@ v := d.Validation()

## More Usage

### Validate error

`v.Errors` is map data, top key is field name, value is `map[string]string`.

```go
// do validating
if v.Validate() {
return nil
}

// get errors
es := v.Errors

// check
es.Empty() // bool

// returns an random error, if no error returns nil
fmt.Println(v.Errors.OneError())
fmt.Println(v.Errors.ErrOrNil())

fmt.Println(v.Errors) // all error messages
fmt.Println(v.Errors.One()) // returns a random error message text
fmt.Println(v.Errors.Field("Name")) // returns error messages of the field
```

**Encode to JSON**:

- `StopOnError=true`(default), will only one error

```json
{
"field1": {
"required": "error msg0"
}
}
```

- if `StopOnError=false`, will get multi error

```json
{
"field1": {
"minLen": "error msg1",
"required": "error msg0"
},
"field2": {
"min": "error msg2"
}
}
```

### Global Option

You can adjust some processing logic of the validator by changing the global option settings.
Expand Down
51 changes: 51 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,57 @@ v := d.Validation()

## 更多使用

### 验证错误信息

`v.Errors` 是一个Map数据,键是字段名,值是 `map[string]string`

```go
// do validating
if v.Validate() {
return nil
}

// get errors
es := v.Errors

// check
es.Empty() // bool

// 返回一个随机的 error错误,如果没有错误返回 nil
fmt.Println(v.Errors.OneError())
fmt.Println(v.Errors.ErrOrNil())

fmt.Println(v.Errors) // all error messages
fmt.Println(v.Errors.One()) // returns a random error message text
fmt.Println(v.Errors.Field("Name")) // returns error messages of the field
```

**错误转为JSON**:

- `StopOnError=true`(默认),只会有一个错误:

```json
{
"field1": {
"required": "error msg0"
}
}
```

- `StopOnError=false`时,可能会返回多个错误:

```json
{
"field1": {
"minLen": "error msg1",
"required": "error msg0"
},
"field2": {
"min": "error msg2"
}
}
```

### 全局选项

你可以通过改变全局选项设置,来调整验证器的一些处理逻辑。
Expand Down

0 comments on commit 11b83cb

Please sign in to comment.