Data Validation - Result

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

type Error interface {
    Code() gcode.Code
    Current() error
    Error() string
    FirstItem() (key string, messages map[string]error)
    FirstRule() (rule string, err error)
    FirstError() (err error)
    Items() (items []map[string]map[string]error)
    Map() map[string]error
    Maps() map[string]map[string]error
    String() string
    Strings() (errs []string)
}

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 - 图1 tip

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:

Method Description
Code Common method. Implements the gerror Code interface, in which this method consistently returns the error code gcode.CodeValidationFailed within the validation component.
Error Common 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.
Current Common method. Implements the gerror Current interface for obtaining the first error object among validation errors.
Items In 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.
Map Returns the erroneous sub-rule and corresponding error message map obtained from FirstItem.
Maps Returns all error keys and corresponding error rules and error messages ( map[string]map[string]error ).
String Returns 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.
Strings Returns all error messages formed into a []string type. Its sequence is only valid when using sequential validation rules; otherwise, the result is random.
FirstItem When 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.
FirstRule Returns 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.
FirstString Returns 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:

package main

import (
    "github.com/gogf/gf/v2/errors/gerror"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gctx"
    "github.com/gogf/gf/v2/util/gvalid"
)

func main() {
    type User struct {
        Name string `v:"required#Please enter user name"`
        Type int    `v:"required|min:1#|Please select user type"`
    }
    var (
        err  error
        ctx  = gctx.New()
        user = User{}
    )
    if err = g.Validator().Data(user).Run(ctx); err != nil {
        g.Dump(err.(gvalid.Error).Maps())
        g.Dump(gerror.Current(err))
    }
}

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

{
    "Name": {
        "required": "Please enter user name",
    },
    "Type": {
        "min": "Please select user type",
    },
}
"Please enter user name"

Data Validation - Result - 图2 warning

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.