可选校验规则

示例1,空字符串

示例2,空指针属性

  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. Page int `v:"required|min:1 # page is required"`
  10. Size int `v:"required|between:1,100 # size is required"`
  11. ProjectId *gvar.Var `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. err := g.Validator().Data(obj).Run(ctx)
  20. fmt.Println(err)
  21. // Output:
  22. // <nil>
  23. }

示例3,空整型属性

  1. project id must between 1, 10000

示例4,通过map传参

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