github.com/sandwich-go/boost@v1.3.29/validator/README.md (about)

     1  # validator
     2  
     3  通过 `tag` 或者 `rules` 进行数据校验
     4  
     5  # 例子
     6  
     7  ```go
     8  type Test struct {
     9      Name string `val:"len=4"`
    10  }
    11  
    12  func main() {
    13      s := &Test{
    14          Name: "TEST",
    15      }
    16      err := Default.Struct(context.Background(), s)
    17      if err == nil {
    18          fmt.Println("validate ok")
    19      }   
    20  
    21      s.Name = ""
    22      err = Default.Struct(context.Background(), s)
    23      if err != nil {
    24          fmt.Println(err.Error())
    25      }
    26  }
    27  
    28  ```
    29  
    30  Output:
    31  ```text
    32  validate ok
    33  Key: 'Test.Name' Error:Field validation for 'Name' failed on the 'len' tag
    34  ```