Introduction

The validation result is an error object, internally implemented using the gvalid.Error object. When data rule validation succeeds, the validation method returns nil. When data rule validation fails, the returned object is a structured hierarchical map containing multiple fields and their rules along with the corresponding error messages, allowing the receiver to accurately pinpoint the error rules. The related data structure and methods are as follows: https://pkg.go.dev/github.com/gogf/gf/v2/util/gvalid

  1. type Error interface {
  2. Code() gcode.Code
  3. Current() error
  4. Error() string
  5. FirstItem() (key string, messages map[string]error)
  6. FirstRule() (rule string, err error)
  7. FirstError() (err error)
  8. Items() (items []map[string]map[string]error)
  9. Map() map[string]error
  10. Maps() map[string]map[string]error
  11. String() string
  12. Strings() (errs []string)
  13. }

This data structure can be understood in conjunction with subsequent examples. We can obtain the raw error message data structure map through the Maps() method. However, in most cases, specific error messages can be conveniently obtained through other methods of the Error interface.

Data Validation - Result - 图1tip

In most cases, we are not concerned with the specific failed validation rules and can use the Error/String method to directly return all error messages. The order of results when obtaining error messages may vary depending on whether the validation rule is sequential.

Brief Description:

The value of the validation result can be obtained through multiple validation result methods. To provide developers with a comprehensive understanding, the following details are provided:

MethodDescription
CodeCommon method. Implements the gerror Code interface, in which this method consistently returns the error code gcode.CodeValidationFailed within the validation component.
ErrorCommon method. Implements the standard library error.Error interface to obtain a string composed of all validation errors. Its internal logic is the same as the String method.
CurrentCommon method. Implements the gerror Current interface for obtaining the first error object among validation errors.
ItemsIn sequential validation, returns an array of validation errors in the order of validation rules; this order is only valid in sequential validation. Otherwise, the result is random.
MapReturns the erroneous sub-rule and corresponding error message map obtained from FirstItem.
MapsReturns all error keys and corresponding error rules and error messages ( map[string]map[string]error ).
StringReturns all error messages as a single string, with multiple rule error messages connected by ;. Its sequence is only valid when using sequential validation rules; otherwise, the result is random.
StringsReturns all error messages formed into a []string type. Its sequence is only valid when using sequential validation rules; otherwise, the result is random.
FirstItemWhen there are multiple key/attribute validation errors, it retrieves the first erroneous key name, along with its corresponding error rule and message. Its sequence is only valid when using sequential validation rules; otherwise, the result is random.
FirstRuleReturns the first erroneous rule and corresponding error message from FirstItem. Its sequence is only valid when using sequential validation rules; otherwise, the result is random.
FirstStringReturns the first rule error message from FirstRule. Its sequence is only valid when using sequential validation rules; otherwise, the result is random.

Support for gerror.Current

We can see that gvalid.Error implements the Current() error interface, allowing the first error message to be retrieved via the gerror.Current method, which is quite convenient for returning error messages when interface validation fails. Let’s look at an example:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/errors/gerror"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/util/gvalid"
  7. )
  8. func main() {
  9. type User struct {
  10. Name string `v:"required#Please enter user name"`
  11. Type int `v:"required|min:1#|Please select user type"`
  12. }
  13. var (
  14. err error
  15. ctx = gctx.New()
  16. user = User{}
  17. )
  18. if err = g.Validator().Data(user).Run(ctx); err != nil {
  19. g.Dump(err.(gvalid.Error).Maps())
  20. g.Dump(gerror.Current(err))
  21. }
  22. }

Here, gerror.Current(err) is used to obtain the first validation error message. After execution, the terminal outputs:

  1. {
  2. "Name": {
  3. "required": "Please enter user name",
  4. },
  5. "Type": {
  6. "min": "Please select user type",
  7. },
  8. }
  9. "Please enter user name"

Data Validation - Result - 图2warning

It is important to note that during data validation, sequential validation and non-sequential validation exist, which will affect the result of obtaining the first error message. For details about sequential and non-sequential validation, refer to subsequent chapters.