示例1,递归校验:struct

执行后,终端输出:

  1. The Size value `10000` must be equal or lesser than 100

示例2,递归校验:slice

  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 Student struct {
  9. Name string `v:"required#Student Name is required"`
  10. Age int
  11. }
  12. type Teacher struct {
  13. Name string
  14. Students []Student
  15. }
  16. var (
  17. ctx = gctx.New()
  18. teacher = Teacher{}
  19. data = g.Map{
  20. "name": "john",
  21. "students": `[{"age":2},{"name":"jack", "age":4}]`,
  22. }
  23. fmt.Println(err)
  24. }

示例3,递归校验:map

  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 Student struct {
  9. Name string `v:"required#Student Name is required"`
  10. Age int
  11. }
  12. type Teacher struct {
  13. Name string
  14. Students map[string]Student
  15. }
  16. var (
  17. ctx = gctx.New()
  18. teacher = Teacher{
  19. Name: "Smith",
  20. Students: map[string]Student{
  21. "john": {Name: "", Age: 18},
  22. },
  23. }
  24. err := g.Validator().Data(teacher).Run(ctx)
  25. fmt.Println(err)
  26. }

执行后,终端输出:

注意事项:空对象对递归校验的影响

  1. Student Name is required

有同学可能会觉得奇怪,明明我都没有传递Student字段值,为什么还会递归校验Student结构体里面的Name字段?这是因为这里的 Student 属性是个空结构体,是带有默认值的(Name默认值为空字符串)。递归校验里面,虽然 Student 不是必须参数,这个意思是你可以不传递,但是只要传递了就会按照里面属性的校验规则执行校验(带有默认值的空对象也算是有值)。可以对比和以下代码的差别:

  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 Student struct {
  9. Name string `v:"required"`
  10. }
  11. type Teacher struct {
  12. Students *Student
  13. }
  14. var (
  15. ctx = gctx.New()
  16. teacher = Teacher{}
  17. data = g.Map{
  18. "students": nil,
  19. }
  20. )
  21. err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
  22. }