github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zvalid/json_test.go (about)

     1  package zvalid
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/sohaha/zlsgo"
     7  	"github.com/sohaha/zlsgo/zjson"
     8  )
     9  
    10  func TestValids(t *testing.T) {
    11  	tt := zlsgo.NewTest(t)
    12  
    13  	j := zjson.Parse(`{"name":"zls","age":18}`)
    14  	rules := map[string]Engine{
    15  		"name": New().MinLength(3),
    16  		"age":  New().MinInt(18),
    17  	}
    18  
    19  	err := JSON(j, rules)
    20  	tt.NoError(err)
    21  
    22  	j = zjson.Parse(`{"age":8}`)
    23  	err = JSON(j, rules)
    24  	t.Log(err)
    25  	tt.EqualTrue(err != nil)
    26  
    27  	j = zjson.Parse(`{"name":8}`)
    28  	err = JSON(j, rules)
    29  	t.Log(err)
    30  	tt.EqualTrue(err != nil)
    31  
    32  	j = zjson.Parse(``)
    33  	err = JSON(j, map[string]Engine{
    34  		"password": New().Required("密码不能为空"),
    35  	})
    36  	t.Log(err)
    37  	tt.EqualTrue(err != nil)
    38  
    39  	j = zjson.Parse(`{"password":"123456"}`)
    40  	err = JSON(j, map[string]Engine{
    41  		"password": New().Required("密码不能为空").StrongPassword("密码必须是强密码"),
    42  	})
    43  	t.Log(err)
    44  	tt.EqualTrue(err != nil)
    45  
    46  	j = zjson.Parse(`{"password":"123456Abc."}`)
    47  	err = JSON(j, map[string]Engine{
    48  		"password": New().Required("密码不能为空").StrongPassword("密码必须是强密码"),
    49  	})
    50  	tt.NoError(err)
    51  
    52  }