github.com/zhongdalu/gf@v1.0.0/g/util/gvalid/gvalid_unit_checkmap_test.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gvalid_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/zhongdalu/gf/g/test/gtest"
    13  	"github.com/zhongdalu/gf/g/util/gvalid"
    14  )
    15  
    16  func Test_CheckMap(t *testing.T) {
    17  
    18  	var params interface{}
    19  	if m := gvalid.CheckMap(params, nil, nil); m == nil {
    20  		t.Error("CheckMap校验失败")
    21  	}
    22  
    23  	kvmap := map[string]interface{}{
    24  		"id":   "0",
    25  		"name": "john",
    26  	}
    27  	rules := map[string]string{
    28  		"id":   "required|between:1,100",
    29  		"name": "required|length:6,16",
    30  	}
    31  	msgs := gvalid.CustomMsg{
    32  		"id": "ID不能为空|ID范围应当为:min到:max",
    33  		"name": map[string]string{
    34  			"required": "名称不能为空",
    35  			"length":   "名称长度为:min到:max个字符",
    36  		},
    37  	}
    38  	if m := gvalid.CheckMap(kvmap, rules, msgs); m == nil {
    39  		t.Error("CheckMap校验失败")
    40  	}
    41  
    42  	kvmap = map[string]interface{}{
    43  		"id":   "1",
    44  		"name": "john",
    45  	}
    46  	rules = map[string]string{
    47  		"id":   "required|between:1,100",
    48  		"name": "required|length:4,16",
    49  	}
    50  	msgs = map[string]interface{}{
    51  		"id": "ID不能为空|ID范围应当为:min到:max",
    52  		"name": map[string]string{
    53  			"required": "名称不能为空",
    54  			"length":   "名称长度为:min到:max个字符",
    55  		},
    56  	}
    57  	if m := gvalid.CheckMap(kvmap, rules, msgs); m != nil {
    58  		t.Error(m)
    59  	}
    60  
    61  	kvmap = map[string]interface{}{
    62  		"id":   "1",
    63  		"name": "john",
    64  	}
    65  	rules = map[string]string{
    66  		"id":   "",
    67  		"name": "",
    68  	}
    69  	msgs = map[string]interface{}{
    70  		"id": "ID不能为空|ID范围应当为:min到:max",
    71  		"name": map[string]string{
    72  			"required": "名称不能为空",
    73  			"length":   "名称长度为:min到:max个字符",
    74  		},
    75  	}
    76  	if m := gvalid.CheckMap(kvmap, rules, msgs); m != nil {
    77  		t.Error(m)
    78  	}
    79  
    80  	kvmap = map[string]interface{}{
    81  		"id":   "1",
    82  		"name": "john",
    83  	}
    84  	rules2 := []string{
    85  		"@required|between:1,100",
    86  		"@required|length:4,16",
    87  	}
    88  	msgs = map[string]interface{}{
    89  		"id": "ID不能为空|ID范围应当为:min到:max",
    90  		"name": map[string]string{
    91  			"required": "名称不能为空",
    92  			"length":   "名称长度为:min到:max个字符",
    93  		},
    94  	}
    95  	if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil {
    96  		t.Error(m)
    97  	}
    98  
    99  	kvmap = map[string]interface{}{
   100  		"id":   "1",
   101  		"name": "john",
   102  	}
   103  	rules2 = []string{
   104  		"id@required|between:1,100",
   105  		"name@required|length:4,16#名称不能为空|",
   106  	}
   107  	msgs = map[string]interface{}{
   108  		"id": "ID不能为空|ID范围应当为:min到:max",
   109  		"name": map[string]string{
   110  			"required": "名称不能为空",
   111  			"length":   "名称长度为:min到:max个字符",
   112  		},
   113  	}
   114  	if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil {
   115  		t.Error(m)
   116  	}
   117  
   118  	kvmap = map[string]interface{}{
   119  		"id":   "1",
   120  		"name": "john",
   121  	}
   122  	rules2 = []string{
   123  		"id@required|between:1,100",
   124  		"name@required|length:4,16#名称不能为空",
   125  	}
   126  	msgs = map[string]interface{}{
   127  		"id": "ID不能为空|ID范围应当为:min到:max",
   128  		"name": map[string]string{
   129  			"required": "名称不能为空",
   130  			"length":   "名称长度为:min到:max个字符",
   131  		},
   132  	}
   133  	if m := gvalid.CheckMap(kvmap, rules2, msgs); m != nil {
   134  		t.Error(m)
   135  	}
   136  }
   137  
   138  // 如果值为nil,并且不需要require*验证时,其他验证失效
   139  func Test_CheckMapWithNilAndNotRequiredField(t *testing.T) {
   140  	data := map[string]interface{}{
   141  		"id": "1",
   142  	}
   143  	rules := map[string]string{
   144  		"id":   "required",
   145  		"name": "length:4,16",
   146  	}
   147  	if m := gvalid.CheckMap(data, rules); m != nil {
   148  		t.Error(m)
   149  	}
   150  }
   151  
   152  func Test_Sequence(t *testing.T) {
   153  	gtest.Case(t, func() {
   154  		params := map[string]interface{}{
   155  			"passport":  "",
   156  			"password":  "123456",
   157  			"password2": "1234567",
   158  		}
   159  		rules := []string{
   160  			"passport@required|length:6,16#账号不能为空|账号长度应当在:min到:max之间",
   161  			"password@required|length:6,16|same:password2#密码不能为空|密码长度应当在:min到:max之间|两次密码输入不相等",
   162  			"password2@required|length:6,16#",
   163  		}
   164  		err := gvalid.CheckMap(params, rules)
   165  		gtest.AssertNE(err, nil)
   166  		gtest.Assert(len(err.Map()), 2)
   167  		gtest.Assert(err.Map()["required"], "账号不能为空")
   168  		gtest.Assert(err.Map()["length"], "账号长度应当在6到16之间")
   169  		gtest.Assert(len(err.Maps()), 2)
   170  
   171  		gtest.Assert(err.String(), "账号不能为空; 账号长度应当在6到16之间; 两次密码输入不相等")
   172  		gtest.Assert(err.Strings(), []string{"账号不能为空", "账号长度应当在6到16之间", "两次密码输入不相等"})
   173  
   174  		k, m := err.FirstItem()
   175  		gtest.Assert(k, "passport")
   176  		gtest.Assert(m, err.Map())
   177  
   178  		r, s := err.FirstRule()
   179  		gtest.Assert(r, "required")
   180  		gtest.Assert(s, "账号不能为空")
   181  	})
   182  }