Optional Validation Rules

When the given data validation rules do not include the required* rule, it indicates that the rule is not a mandatory rule. If the given value is nil or an empty string, its validation will be ignored.

Example 1: Empty String

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. func main() {
  8. type Params struct {
  9. Page int `v:"required|min:1 # page is required"`
  10. Size int `v:"required|between:1,100 # size is required"`
  11. ProjectId string `v:"between:1,10000 # project id must between {min}, {max}"`
  12. }
  13. var (
  14. ctx = gctx.New()
  15. obj = &Params{
  16. Page: 1,
  17. Size: 10,
  18. }
  19. )
  20. err := g.Validator().Data(obj).Run(ctx)
  21. fmt.Println(err)
  22. // Output:
  23. // <nil>
  24. }

Example 2: Null Pointer Attribute

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gvar"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. )
  8. func main() {
  9. type Params struct {
  10. Page int `v:"required|min:1 # page is required"`
  11. Size int `v:"required|between:1,100 # size is required"`
  12. ProjectId *gvar.Var `v:"between:1,10000 # project id must between {min}, {max}"`
  13. }
  14. var (
  15. ctx = gctx.New()
  16. obj = &Params{
  17. Page: 1,
  18. Size: 10,
  19. }
  20. )
  21. err := g.Validator().Data(obj).Run(ctx)
  22. fmt.Println(err)
  23. // Output:
  24. // <nil>
  25. }

Example 3: Empty Integer Attribute

Note that if the key value is 0 or false, the parameter value will still be validated.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. )
  7. func main() {
  8. type Params struct {
  9. Page int `v:"required|min:1 # page is required"`
  10. Size int `v:"required|between:1,100 # size is required"`
  11. ProjectId int `v:"between:1,10000 # project id must between {min}, {max}"`
  12. }
  13. var (
  14. ctx = gctx.New()
  15. obj = &Params{
  16. Page: 1,
  17. Size: 10,
  18. }
  19. )
  20. err := g.Validator().Data(obj).Run(ctx)
  21. fmt.Println(err)
  22. // Output:
  23. // <nil>
  24. }

After execution, the terminal outputs:

  1. project id must between 1, 10000

Example 4: Parameter Passing Through map

  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. var (
  8. ctx = gctx.New()
  9. params = map[string]interface{}{
  10. "passport": "",
  11. "password": "123456",
  12. "password2": "1234567",
  13. }
  14. rules = []string{
  15. "passport@length:6,16",
  16. "password@required|length:6,16|same:password2",
  17. "password2@required|length:6,16",
  18. }
  19. )
  20. err := g.Validator().Rules(rules).Data(params).Run(ctx)
  21. if err != nil {
  22. g.Dump(err.Maps())
  23. }
  24. }

Note that the passport key does not have the required rule, so even if the given passport parameter is an empty string and does not meet the rule, it does not trigger an error, since the validation component considers it an optional validation rule.

After execution, the terminal outputs:

  1. {
  2. "password": {
  3. "same": "The password value `123456` must be the same as field password2",
  4. },
  5. }