Documentation ¶
Overview ¶
Package gvalid implements powerful and useful data/form validation functionality.
Index ¶
- func DeleteRule(rule string)
- func RegisterRule(rule string, f RuleFunc) error
- type CustomMsg
- type Error
- func CheckMap(ctx context.Context, params interface{}, rules interface{}, ...) Error
- func CheckStruct(ctx context.Context, object interface{}, rules interface{}, ...) Error
- func CheckStructWithData(ctx context.Context, object interface{}, data interface{}, rules interface{}, ...) Error
- func CheckValue(ctx context.Context, value interface{}, rules string, messages interface{}, ...) Error
- type RuleFunc
- type Validator
- func (v *Validator) Bail() *Validator
- func (v *Validator) CheckMap(params interface{}) Error
- func (v *Validator) CheckStruct(object interface{}) Error
- func (v *Validator) CheckValue(value interface{}) Error
- func (v *Validator) Clone() *Validator
- func (v *Validator) Ctx(ctx context.Context) *Validator
- func (v *Validator) Data(data interface{}) *Validator
- func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator
- func (v *Validator) Messages(messages interface{}) *Validator
- func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator
- func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator
- func (v *Validator) Rules(rules interface{}) *Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteRule ¶ added in v1.13.7
func DeleteRule(rule string)
DeleteRule deletes custom defined validation rule and its function from global package.
func RegisterRule ¶ added in v1.13.2
RegisterRule registers custom validation rule and function for package. It returns error if there's already the same rule registered previously.
Example ¶
package main import ( "context" "errors" "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/gvalid" ) func main() { type User struct { Id int Name string `v:"required|unique-name # 请输入用户名称|用户名称已被占用"` Pass string `v:"required|length:6,18"` } user := &User{ Id: 1, Name: "john", Pass: "123456", } rule := "unique-name" gvalid.RegisterRule(rule, func(ctx context.Context, rule string, value interface{}, message string, data interface{}) error { var ( id = data.(*User).Id name = gconv.String(value) ) n, err := g.Model("user").Where("id != ? and name = ?", id, name).Count() if err != nil { return err } if n > 0 { return errors.New(message) } return nil }) err := gvalid.CheckStruct(context.TODO(), user, nil) fmt.Println(err.Error()) // May Output: // 用户名称已被占用 }
Output:
Types ¶
type CustomMsg ¶
type CustomMsg = map[string]interface{}
CustomMsg is the custom error message type, like: map[field] => string|map[rule]string
type Error ¶
type Error interface { Code() gcode.Code Current() error Error() string FirstItem() (key string, messages map[string]string) FirstRule() (rule string, err string) FirstString() (err string) Items() (items []map[string]map[string]string) Map() map[string]string Maps() map[string]map[string]string String() string Strings() (errs []string) }
Error is the validation error for validation result.
func CheckMap ¶
func CheckMap(ctx context.Context, params interface{}, rules interface{}, messages ...CustomMsg) Error
CheckMap validates map and returns the error result. It returns nil if with successful validation.
The parameter `rules` can be type of []string/map[string]string. It supports sequence in error result if `rules` is type of []string. The optional parameter `messages` specifies the custom error messages for specified keys and rules.
Example ¶
package main import ( "context" "fmt" "github.com/gogf/gf/util/gvalid" ) func main() { params := map[string]interface{}{ "passport": "", "password": "123456", "password2": "1234567", } rules := []string{ "passport@required|length:6,16#账号不能为空|账号长度应当在:min到:max之间", "password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等", "password2@required|length:6,16#", } if e := gvalid.CheckMap(context.TODO(), params, rules); e != nil { fmt.Println(e.Map()) fmt.Println(e.FirstItem()) fmt.Println(e.FirstString()) } // May Output: // map[required:账号不能为空 length:账号长度应当在6到16之间] // passport map[required:账号不能为空 length:账号长度应当在6到16之间] // 账号不能为空 }
Output:
func CheckStruct ¶
func CheckStruct(ctx context.Context, object interface{}, rules interface{}, messages ...CustomMsg) Error
CheckStruct validates struct and returns the error result.
The parameter `object` should be type of struct/*struct. The parameter `rules` can be type of []string/map[string]string. It supports sequence in error result if `rules` is type of []string. The optional parameter `messages` specifies the custom error messages for specified keys and rules.
Example ¶
Empty string attribute.
package main import ( "context" "fmt" "github.com/gogf/gf/util/gvalid" ) func main() { type Params struct { Page int `v:"required|min:1 # page is required"` Size int `v:"required|between:1,100 # size is required"` ProjectId string `v:"between:1,10000 # project id must between :min, :max"` } obj := &Params{ Page: 1, Size: 10, } err := gvalid.CheckStruct(context.TODO(), obj, nil) fmt.Println(err == nil) }
Output: true
func CheckStructWithData ¶ added in v1.16.0
func CheckStructWithData(ctx context.Context, object interface{}, data interface{}, rules interface{}, messages ...CustomMsg) Error
CheckStructWithData validates struct with given parameter map and returns the error result.
The parameter `object` should be type of struct/*struct. The parameter `rules` can be type of []string/map[string]string. It supports sequence in error result if `rules` is type of []string. The optional parameter `messages` specifies the custom error messages for specified keys and rules.
func CheckValue ¶ added in v1.16.0
func CheckValue(ctx context.Context, value interface{}, rules string, messages interface{}, params ...interface{}) Error
CheckValue checks single value with specified rules. It returns nil if successful validation.
The parameter `value` can be any type of variable, which will be converted to string for validation. The parameter `rules` can be one or more rules, multiple rules joined using char '|'. The parameter `messages` specifies the custom error messages, which can be type of: string/map/struct/*struct. The optional parameter `params` specifies the extra validation parameters for some rules like: required-*、same、different, etc.
type RuleFunc ¶ added in v1.13.2
type RuleFunc func(ctx context.Context, rule string, value interface{}, message string, data interface{}) error
RuleFunc is the custom function for data validation. The parameter `rule` specifies the validation rule string, like "required", "between:1,100", etc. The parameter `value` specifies the value for this rule to validate. The parameter `message` specifies the custom error message or configured i18n message for this rule. The parameter `data` specifies the `data` which is passed to the Validator. It might be type of map/struct or a nil value. You can ignore the parameter `data` if you do not really need it in your custom validation rule.
type Validator ¶ added in v1.15.2
type Validator struct {
// contains filtered or unexported fields
}
Validator is the validation manager for chaining operations.
func (*Validator) Bail ¶ added in v1.16.5
Bail sets the mark for stopping validation after the first validation error.
func (*Validator) CheckMap ¶ added in v1.15.2
CheckMap validates map and returns the error result. It returns nil if with successful validation. The parameter `params` should be type of map.
Example ¶
package main import ( "github.com/gogf/gf/frame/g" ) func main() { params := map[string]interface{}{ "passport": "", "password": "123456", "password2": "1234567", } rules := map[string]string{ "passport": "required|length:6,16", "password": "required|length:6,16|same:password2", "password2": "required|length:6,16", } messages := map[string]interface{}{ "passport": "账号不能为空|账号长度应当在:min到:max之间", "password": map[string]string{ "required": "密码不能为空", "same": "两次密码输入不相等", }, } err := g.Validator().Messages(messages).Rules(rules).CheckMap(params) if err != nil { g.Dump(err.Maps()) } // May Output: //{ // "passport": { // "length": "账号长度应当在6到16之间", // "required": "账号不能为空" //}, // "password": { // "same": "两次密码输入不相等" //} //} }
Output:
func (*Validator) CheckStruct ¶ added in v1.15.2
CheckStruct validates struct and returns the error result. The parameter `object` should be type of struct/*struct.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gconv" ) func main() { type User struct { Name string `v:"required#请输入用户姓名"` Type int `v:"required#请选择用户类型"` } data := g.Map{ "name": "john", } user := User{} if err := gconv.Scan(data, &user); err != nil { panic(err) } err := g.Validator().Data(data).CheckStruct(user) if err != nil { fmt.Println(err.Items()) } }
Output: [map[Type:map[required:请选择用户类型]]]
func (*Validator) CheckValue ¶ added in v1.16.0
CheckValue checks single value with specified rules. It returns nil if successful validation.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/frame/g" ) func main() { err := g.Validator().Rules("min:18").Messages("未成年人不允许注册哟").CheckValue(16) fmt.Println(err.String()) }
Output: 未成年人不允许注册哟
func (*Validator) Clone ¶ added in v1.15.2
Clone creates and returns a new Validator which is a shallow copy of current one.
func (*Validator) Ctx ¶ added in v1.16.0
Ctx is a chaining operation function, which sets the context for next validation.
func (*Validator) Data ¶ added in v1.16.0
Data is a chaining operation function, which sets validation data for current operation. The parameter `data` usually be type of map, which specifies the parameter map used in validation. Calling this function also sets `useDataInsteadOfObjectAttributes` true no mather the `data` is nil or not.
func (*Validator) Messages ¶ added in v1.16.0
Messages is a chaining operation function, which sets custom error messages for current operation. The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result if `rules` is type of []string.
func (*Validator) RuleFunc ¶ added in v1.16.0
RuleFunc registers one custom rule function to current Validator.
func (*Validator) RuleFuncMap ¶ added in v1.16.0
RuleFuncMap registers multiple custom rule functions to current Validator.
func (*Validator) Rules ¶ added in v1.16.0
Rules is a chaining operation function, which sets custom validation rules for current operation.
Example ¶
package main import ( "fmt" "github.com/gogf/gf/frame/g" ) func main() { data := g.Map{ "password": "123", } err := g.Validator().Data(data).Rules("required-with:password").Messages("请输入确认密码").CheckValue("") fmt.Println(err.String()) }
Output: 请输入确认密码
Source Files ¶
- gvalid.go
- gvalid_custom_rule.go
- gvalid_error.go
- gvalid_validator.go
- gvalid_validator_check_map.go
- gvalid_validator_check_struct.go
- gvalid_validator_check_value.go
- gvalid_validator_message.go
- gvalid_validator_rule_length.go
- gvalid_validator_rule_luhn.go
- gvalid_validator_rule_range.go
- gvalid_validator_rule_required.go
- gvalid_validator_rule_resident_id.go