Skip to main content
Version: 2.8.x(Latest)

Validator Object

The data validation component provides a validator object for unified configuration management and convenient chained operations for data validation.

API Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/util/gvalid

type Validator
func New() *Validator
func (v *Validator) Assoc(assoc interface{}) *Validator
func (v *Validator) Bail() *Validator
func (v *Validator) Ci() *Validator
func (v *Validator) Clone() *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
func (v *Validator) Run(ctx context.Context) Error

Brief Explanation:

  1. The New method is used to create a new validation object.
  2. Assoc is used for associating data validation; see subsequent sections for details.
  3. The Bail method is used to set that validation stops immediately and returns an error result if any rule fails in multiple subsequent validations.
  4. The Ci method is used for setting case-insensitive field names when comparing values.
  5. The Run method performs validation operations on data given rules and messages.
  6. The I18n method is used to set the I18N internationalization component for the current validator object. By default, the validator component uses the framework's global default i18n component object.
  7. The Data method is used to pass the data set for joint validation, often map type or struct type.
  8. The Rules method is used to pass custom validation rules for the current chained operation, often using []string type or map type.
  9. The Messages method is used to pass custom error messages for the current chained operation, often using map type for passing; see subsequent code examples for details.
tip

Since the validator object is also a very commonly used object, the g module defines the Validator method for quickly creating validator objects. In most scenarios, we recommend using the g module's g.Validator() method to quickly create a validator object. For an introduction to the g module, refer to the section: Objects

Usage Examples

Single Data Validation

var (
err error
ctx = gctx.New()
)
err = g.Validator().
Rules("min:18").
Data(16).
Messages("Minors are not allowed to register").
Run(ctx)
fmt.Println(err.Error())

// Output:
// Minors are not allowed to register
var (
err error
ctx = gctx.New()
data = g.Map{
"password": "123",
}
)

err = g.Validator().Data("").Assoc(data).
Rules("required-with:password").
Messages("Please enter the confirmation password").
Run(ctx)

fmt.Println(err.Error())

Struct Data Validation

type User struct {
Name string `v:"required#Please enter the user name"`
Type int `v:"required#Please select the user type"`
}
var (
err error
ctx = gctx.New()
user = User{}
data = g.Map{
"name": "john",
}
)
if err = gconv.Scan(data, &user); err != nil {
panic(err)
}
err = g.Validator().Assoc(data).Data(user).Run(ctx)
if err != nil {
fmt.Println(err.(gvalid.Error).Items())
}

// Output:
// [map[Type:map[required:Please select the user type]]]

Map Data Validation

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": "Account cannot be empty|Account length should be between {min} and {max}",
"password": map[string]string{
"required": "Password cannot be empty",
"same": "The passwords entered are not the same",
},
}
err := g.Validator().Messages(messages).Rules(rules).Data(params).Run(gctx.New())
if err != nil {
g.Dump(err.Maps())
}

After execution, the terminal output is:

{
"passport": {
"length": "Account length should be between 6 and 16",
"required": "Account cannot be empty"
},
"password": {
"same": "The passwords entered are not the same"
}
}