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 (e *Error) Current() error
- func (e *Error) Error() string
- func (e *Error) FirstItem() (key string, messages map[string]string)
- func (e *Error) FirstRule() (rule string, err string)
- func (e *Error) FirstString() (err string)
- func (e *Error) Map() map[string]string
- func (e *Error) Maps() ErrorMap
- func (e *Error) String() string
- func (e *Error) Strings() (errs []string)
- type ErrorMap
- type RuleFunc
- type Validator
- func (v *Validator) Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error
- func (v *Validator) CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error
- func (v *Validator) CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error
- func (v *Validator) Clone() *Validator
- func (v *Validator) I18n(language string) *Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteRule ¶
func DeleteRule(rule string)
DeleteRule deletes custom defined validation rule and its function from global package.
func RegisterRule ¶
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 ( "errors" "fmt" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/util/gconv" "github.com/gogf/gf/util/gvalid" ) func main() { rule := "unique-name" gvalid.RegisterRule(rule, func(rule string, value interface{}, message string, params map[string]interface{}) error { var ( id = gconv.Int(params["Id"]) name = gconv.String(value) ) n, err := g.Table("user").Where("id != ? and name = ?", id, name).Count() if err != nil { return err } if n > 0 { return errors.New(message) } return nil }) 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", } err := gvalid.CheckStruct(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 struct {
// contains filtered or unexported fields
}
Error is the validation error for validation result.
func Check ¶
Check 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.
func CheckMap ¶
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 ( "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(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 ¶
CheckStruct validates strcut 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 ( "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(obj, nil) fmt.Println(err == nil) }
Output: true
func (*Error) Current ¶
Current is alis of FirstString, which implements interface gerror.ApiCurrent.
func (*Error) FirstItem ¶
FirstItem returns the field name and error messages for the first validation rule error.
func (*Error) FirstString ¶
FirstString returns the first error message as string. Note that the returned message might be different if it has no sequence.
type RuleFunc ¶
type RuleFunc func(rule string, value interface{}, message string, params map[string]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 <params> specifies all the parameters that needs. You can ignore parameter <params> if you do not really need it in your custom validation rule.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is the validation manager.
func (*Validator) Check ¶
func (v *Validator) Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error
Check 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.
func (*Validator) CheckMap ¶
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.
func (*Validator) CheckStruct ¶
func (v *Validator) CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error
CheckStruct validates strcut 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.
Source Files ¶
- gvalid.go
- gvalid_custom_rule.go
- gvalid_error.go
- gvalid_validator.go
- gvalid_validator_check.go
- gvalid_validator_check_map.go
- gvalid_validator_check_struct.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