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

  1. type Validator
  2. func New() *Validator
  3. func (v *Validator) Assoc(assoc interface{}) *Validator
  4. func (v *Validator) Bail() *Validator
  5. func (v *Validator) Ci() *Validator
  6. func (v *Validator) Clone() *Validator
  7. func (v *Validator) Data(data interface{}) *Validator
  8. func (v *Validator) I18n(i18nManager *gi18n.Manager) *Validator
  9. func (v *Validator) Messages(messages interface{}) *Validator
  10. func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator
  11. func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator
  12. func (v *Validator) Rules(rules interface{}) *Validator
  13. 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.

Data Validation - Object - 图1tip

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

  1. var (
  2. err error
  3. ctx = gctx.New()
  4. )
  5. err = g.Validator().
  6. Rules("min:18").
  7. Data(16).
  8. Messages("Minors are not allowed to register").
  9. Run(ctx)
  10. fmt.Println(err.Error())
  11. // Output:
  12. // Minors are not allowed to register
  1. var (
  2. err error
  3. ctx = gctx.New()
  4. data = g.Map{
  5. "password": "123",
  6. }
  7. )
  8. err = g.Validator().Data("").Assoc(data).
  9. Rules("required-with:password").
  10. Messages("Please enter the confirmation password").
  11. Run(ctx)
  12. fmt.Println(err.Error())

Struct Data Validation

  1. type User struct {
  2. Name string `v:"required#Please enter the user name"`
  3. Type int `v:"required#Please select the user type"`
  4. }
  5. var (
  6. err error
  7. ctx = gctx.New()
  8. user = User{}
  9. data = g.Map{
  10. "name": "john",
  11. }
  12. )
  13. if err = gconv.Scan(data, &user); err != nil {
  14. panic(err)
  15. }
  16. err = g.Validator().Assoc(data).Data(user).Run(ctx)
  17. if err != nil {
  18. fmt.Println(err.(gvalid.Error).Items())
  19. }
  20. // Output:
  21. // [map[Type:map[required:Please select the user type]]]

Map Data Validation

  1. params := map[string]interface{}{
  2. "passport": "",
  3. "password": "123456",
  4. "password2": "1234567",
  5. }
  6. rules := map[string]string{
  7. "passport": "required|length:6,16",
  8. "password": "required|length:6,16|same:password2",
  9. "password2": "required|length:6,16",
  10. }
  11. messages := map[string]interface{}{
  12. "passport": "Account cannot be empty|Account length should be between {min} and {max}",
  13. "password": map[string]string{
  14. "required": "Password cannot be empty",
  15. "same": "The passwords entered are not the same",
  16. },
  17. }
  18. err := g.Validator().Messages(messages).Rules(rules).Data(params).Run(gctx.New())
  19. if err != nil {
  20. g.Dump(err.Maps())
  21. }

After execution, the terminal output is:

  1. {
  2. "passport": {
  3. "length": "Account length should be between 6 and 16",
  4. "required": "Account cannot be empty"
  5. },
  6. "password": {
  7. "same": "The passwords entered are not the same"
  8. }
  9. }