Struct validation is often used in the following chain operation method:

  1. g.Validator().Data(object).Run(ctx)

Validation tag Rules Introduction

Before introducing Struct parameter type validation, let’s introduce some common validation tag rules. The rules are as follows:

  1. [FieldAlias@]ValidationRule[#ErrorMessage]

Where:

  • FieldAlias and ErrorMessage are optional fields, and ValidationRule is a mandatory field.
  • FieldAlias is an optional field, specifying the alias of the corresponding struct field used in validation. The error object returned after validation will use this alias. This is particularly useful when dealing with request forms, as field names in forms often do not match the property names in the struct. In most scenarios, you don’t need to set the field alias and can directly use the property name by default.
  • ValidationRule is the validation rule for the current property. Multiple validation rules can be combined using the | symbol, for example: required|between:1,100.
  • ErrorMessage is an optional field, representing a custom error message that overrides the default error message when the rule validation fails.

Validation tag Usage Example

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. )
  6. type User struct {
  7. Uid int `v:"uid @integer|min:1#|Please enter the user ID"`
  8. Name string `v:"name @required|length:6,30#Please enter the user name|Invalid length for user name"`
  9. Pass1 string `v:"password1@required|password3"`
  10. Pass2 string `v:"password2@required|password3|same:Pass1#|Invalid password format|The two passwords do not match, please re-enter"`
  11. }
  12. func main() {
  13. var (
  14. ctx = gctx.New()
  15. user = &User{
  16. Name: "john",
  17. Pass1: "Abc123!@#",
  18. Pass2: "123",
  19. }
  20. )
  21. err := g.Validator().Data(user).Run(ctx)
  22. if err != nil {
  23. g.Dump(err.Items())
  24. }
  25. }

As you can see, we can bind validation rules and error messages using gvalid tag when defining the struct. In this sample code, the rule same:password1 is the same as using same:Pass1. This means that data validation can use both the original struct property name and the alias simultaneously. However, the returned result will only use the alias, which is the primary purpose of the alias. Also, when validating a struct object, you can pass validation and error message parameters, which will override the corresponding parameters bound when the struct is defined.

After executing the above example, the output is:

  1. [
  2. {
  3. "uid": {
  4. "min": "Please enter the user ID",
  5. },
  6. },
  7. {
  8. "name": {
  9. "length": "Invalid length for user name",
  10. },
  11. },
  12. {
  13. "password2": {
  14. "password3": "Invalid password format",
  15. },
  16. },
  17. ]

Using map to Specify Validation Rules

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. )
  6. func main() {
  7. type User struct {
  8. Age int
  9. Name string
  10. }
  11. var (
  12. ctx = gctx.New()
  13. user = User{Name: "john"}
  14. rules = map[string]string{
  15. "Name": "required|length:6,16",
  16. "Age": "between:18,30",
  17. }
  18. messages = map[string]interface{}{
  19. "Name": map[string]string{
  20. "required": "Name cannot be empty",
  21. "length": "Name length must be between {min} and {max} characters",
  22. },
  23. "Age": "Age must be between 18 to 30 years old",
  24. }
  25. )
  26. err := g.Validator().Rules(rules).Messages(messages).Data(user).Run(ctx)
  27. if err != nil {
  28. g.Dump(err.Maps())
  29. }
  30. }

In the above example, the Age property has a default value of 0, which can cause the required rule to be ineffective. Therefore, we use the between rule for validation instead. After executing the example, the terminal outputs:

  1. {
  2. "Age": {
  3. "between": "Age must be between 18 to 30 years old"
  4. },
  5. "Name": {
  6. "length": "Name length must be between 6 to 16 characters"
  7. }
  8. }

Struct Recursive Validation (Nested Validation)

Supports recursive struct validation (nested validation), meaning if a property is a struct (nested structs are also supported), it will automatically perform recursive validation on that property. Example usage:

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. )
  6. func main() {
  7. type Pass struct {
  8. Pass1 string `v:"password1@required|same:password2#Please enter your password|The two passwords do not match"`
  9. Pass2 string `v:"password2@required|same:password1#Please re-enter your password|The two passwords do not match"`
  10. }
  11. type User struct {
  12. Pass
  13. Id int
  14. Name string `valid:"name@required#Please enter your name"`
  15. }
  16. var (
  17. ctx = gctx.New()
  18. user = &User{
  19. Name: "john",
  20. Pass: Pass{
  21. Pass1: "1",
  22. Pass2: "2",
  23. },
  24. }
  25. )
  26. err := g.Validator().Data(user).Run(ctx)
  27. g.Dump(err.Maps())
  28. }

Or when properties are nested structs (embedded):

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. )
  6. func main() {
  7. type Pass struct {
  8. Pass1 string `v:"password1@required|same:password2#Please enter your password|The two passwords do not match"`
  9. Pass2 string `v:"password2@required|same:password1#Please re-enter your password|The two passwords do not match"`
  10. }
  11. type User struct {
  12. Id int
  13. Name string `valid:"name@required#Please enter your name"`
  14. Pass Pass
  15. }
  16. var (
  17. ctx = gctx.New()
  18. user = &User{
  19. Name: "john",
  20. Pass: Pass{
  21. Pass1: "1",
  22. Pass2: "2",
  23. },
  24. }
  25. )
  26. err := g.Validator().Data(user).Run(ctx)
  27. g.Dump(err.Maps())
  28. }

Once executed, the terminal output is:

  1. {
  2. "password1": {
  3. "same": "The two passwords do not match",
  4. },
  5. "password2": {
  6. "same": "The two passwords do not match",
  7. },
  8. }

For more introductions on recursive validation, please refer to the section: Data Validation - Recursive