Skip to content

Commit

Permalink
chore: update some comments, update examples for use Struct()
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 24, 2022
1 parent 1190e34 commit 38e8d18
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
9 changes: 5 additions & 4 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ var (
// Errors validate errors definition
//
// Example:
// {
// "field": {validator: message, validator1: message1}
// }
//
// {
// "field": {validator: message, validator1: message1}
// }
type Errors map[string]MS

// Empty no error
Expand Down Expand Up @@ -198,7 +199,7 @@ var builtinMessages = map[string]string{
"notContains": "{field} value contains the given %s",
"startsWith": "{field} value does not start with the given %s",
"endsWith": "{field} value does not end with the given %s",
"email": "{field} value is invalid mail",
"email": "{field} value is invalid email address",
"regex": "{field} value does not pass regex check",
"file": "{field} value must be a file",
"image": "{field} value must be an image",
Expand Down
39 changes: 22 additions & 17 deletions rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ func (r *Rule) SetBeforeFunc(fn func(v *Validation) bool) {
// SetMessage set error message.
//
// Usage:
// v.AddRule("name", "required").SetMessage("error message")
//
// v.AddRule("name", "required").SetMessage("error message")
func (r *Rule) SetMessage(errMsg string) *Rule {
r.message = errMsg
return r
Expand All @@ -118,10 +119,11 @@ func (r *Rule) SetMessage(errMsg string) *Rule {
// SetMessages set error message map.
//
// Usage:
// v.AddRule("name,email", "required").SetMessages(MS{
// "name": "error message 1",
// "email": "error message 2",
// })
//
// v.AddRule("name,email", "required").SetMessages(MS{
// "name": "error message 1",
// "email": "error message 2",
// })
func (r *Rule) SetMessages(msgMap MS) *Rule {
r.messages = msgMap
return r
Expand All @@ -135,7 +137,7 @@ func (r *Rule) Fields() []string {
func (r *Rule) errorMessage(field, validator string, v *Validation) (msg string) {
if r.messages != nil {
var ok bool
// use full key. "field.validator"
// use the full key. "field.validator"
fKey := field + "." + validator
if msg, ok = r.messages[fKey]; ok {
return
Expand All @@ -161,9 +163,10 @@ func (r *Rule) errorMessage(field, validator string, v *Validation) (msg string)
// StringRule add field rules by string
//
// Usage:
// v.StringRule("name", "required|string|minLen:6")
// // will try convert to int before apply validate.
// v.StringRule("age", "required|int|min:12", "toInt")
//
// v.StringRule("name", "required|string|minLen:6")
// // will try convert to int before apply validate.
// v.StringRule("age", "required|int|min:12", "toInt")
func (v *Validation) StringRule(field, rule string, filterRule ...string) *Validation {
rule = strings.TrimSpace(rule)
if rule == "" {
Expand Down Expand Up @@ -211,10 +214,11 @@ func (v *Validation) StringRule(field, rule string, filterRule ...string) *Valid
// StringRules add multi rules by string map.
//
// Usage:
// v.StringRules(map[string]string{
// "name": "required|string|min_len:12",
// "age": "required|int|min:12",
// })
//
// v.StringRules(map[string]string{
// "name": "required|string|min_len:12",
// "age": "required|int|min:12",
// })
func (v *Validation) StringRules(mp MS) *Validation {
for name, rule := range mp {
v.StringRule(name, rule)
Expand All @@ -225,10 +229,11 @@ func (v *Validation) StringRules(mp MS) *Validation {
// ConfigRules add multi rules by string map. alias of StringRules()
//
// Usage:
// v.ConfigRules(map[string]string{
// "name": "required|string|min:12",
// "age": "required|int|min:12",
// })
//
// v.ConfigRules(map[string]string{
// "name": "required|string|min:12",
// "age": "required|int|min:12",
// })
func (v *Validation) ConfigRules(mp MS) *Validation {
for name, rule := range mp {
v.StringRule(name, rule)
Expand Down
27 changes: 27 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package validate

import (
"fmt"
"reflect"
"strings"
"testing"
"time"

"github.com/gookit/goutil/dump"
"github.com/stretchr/testify/assert"
Expand All @@ -21,6 +23,31 @@ import (
// StdTranslator.Reset()
// }

func ExampleStruct() {
// UserForm struct
type UserForm struct {
Name string `validate:"required|minLen:7" message:"required:{field} is required" label:"User Name"`
Email string `validate:"email" message:"email:input must be a EMAIL address"`
CreateAt int `validate:"email"`
Safe int `validate:"-"`
UpdateAt time.Time `validate:"required"`
Code string `validate:"customValidator|default:abc"`
Status int `validate:"required|gtField:Extra.0.Status1"`
Extra []ExtraInfo `validate:"required"`
protected string
}

u := &UserForm{
Name: "inhere",
}

v := Struct(u)
ok := v.Validate()

fmt.Println(ok)
dump.P(v.Errors, u)
}

func TestUtil_Func_valueToInt64(t *testing.T) {
noErrTests := []struct {
val interface{}
Expand Down
11 changes: 0 additions & 11 deletions validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ import (
"github.com/stretchr/testify/assert"
)

func ExampleStruct() {
u := &UserForm{
Name: "inhere",
}

v := Struct(u)
ok := v.Validate()

fmt.Println(ok)
}

var mpSample = M{
"age": 100,
"name": "inhere",
Expand Down

0 comments on commit 38e8d18

Please sign in to comment.