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

     1  // Copyright 2017 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"
    13  	"github.com/zhongdalu/gf/g/test/gtest"
    14  	"github.com/zhongdalu/gf/g/util/gvalid"
    15  )
    16  
    17  func Test_Check(t *testing.T) {
    18  
    19  	gtest.Case(t, func() {
    20  		rule := "abc:6,16"
    21  		val1 := 0
    22  		val2 := 7
    23  		val3 := 20
    24  		err1 := gvalid.Check(val1, rule, nil)
    25  		err2 := gvalid.Check(val2, rule, nil)
    26  		err3 := gvalid.Check(val3, rule, nil)
    27  		gtest.Assert(err1, "invalid rules:abc:6,16")
    28  		gtest.Assert(err2, "invalid rules:abc:6,16")
    29  		gtest.Assert(err3, "invalid rules:abc:6,16")
    30  	})
    31  }
    32  
    33  func Test_Required(t *testing.T) {
    34  	if m := gvalid.Check("1", "required", nil); m != nil {
    35  		t.Error(m)
    36  	}
    37  	if m := gvalid.Check("", "required", nil); m == nil {
    38  		t.Error(m)
    39  	}
    40  	if m := gvalid.Check("", "required-if:id,1,age,18", nil, map[string]interface{}{"id": 1, "age": 19}); m == nil {
    41  		t.Error("Required校验失败")
    42  	}
    43  	if m := gvalid.Check("", "required-if:id,1,age,18", nil, map[string]interface{}{"id": 2, "age": 19}); m != nil {
    44  		t.Error("Required校验失败")
    45  	}
    46  }
    47  
    48  func Test_RequiredIf(t *testing.T) {
    49  	gtest.Case(t, func() {
    50  		rule := "required-if:100,200"
    51  		val1 := ""
    52  		val2 := "100"
    53  		val3 := "200"
    54  		err1 := gvalid.Check(val1, rule, nil)
    55  		err2 := gvalid.Check(val2, rule, nil)
    56  		err3 := gvalid.Check(val3, rule, nil)
    57  		gtest.Assert(err1, nil)
    58  		gtest.Assert(err2, nil)
    59  		gtest.Assert(err3, nil)
    60  	})
    61  }
    62  
    63  func Test_RequiredUnless(t *testing.T) {
    64  	gtest.Case(t, func() {
    65  		rule := "required-unless:100,200"
    66  		val1 := ""
    67  		val2 := "100"
    68  		val3 := "200"
    69  		err1 := gvalid.Check(val1, rule, nil)
    70  		err2 := gvalid.Check(val2, rule, nil)
    71  		err3 := gvalid.Check(val3, rule, nil)
    72  		gtest.AssertNE(err1, nil)
    73  		gtest.Assert(err2, nil)
    74  		gtest.Assert(err3, nil)
    75  	})
    76  }
    77  
    78  func Test_RequiredWith(t *testing.T) {
    79  	gtest.Case(t, func() {
    80  		rule := "required-with:id,name"
    81  		val1 := ""
    82  		params1 := g.Map{
    83  			"age": 18,
    84  		}
    85  		params2 := g.Map{
    86  			"id": 100,
    87  		}
    88  		params3 := g.Map{
    89  			"id":   100,
    90  			"name": "john",
    91  		}
    92  		err1 := gvalid.Check(val1, rule, nil, params1)
    93  		err2 := gvalid.Check(val1, rule, nil, params2)
    94  		err3 := gvalid.Check(val1, rule, nil, params3)
    95  		gtest.Assert(err1, nil)
    96  		gtest.AssertNE(err2, nil)
    97  		gtest.AssertNE(err3, nil)
    98  	})
    99  }
   100  
   101  func Test_RequiredWithAll(t *testing.T) {
   102  	gtest.Case(t, func() {
   103  		rule := "required-with-all:id,name"
   104  		val1 := ""
   105  		params1 := g.Map{
   106  			"age": 18,
   107  		}
   108  		params2 := g.Map{
   109  			"id": 100,
   110  		}
   111  		params3 := g.Map{
   112  			"id":   100,
   113  			"name": "john",
   114  		}
   115  		err1 := gvalid.Check(val1, rule, nil, params1)
   116  		err2 := gvalid.Check(val1, rule, nil, params2)
   117  		err3 := gvalid.Check(val1, rule, nil, params3)
   118  		gtest.Assert(err1, nil)
   119  		gtest.Assert(err2, nil)
   120  		gtest.AssertNE(err3, nil)
   121  	})
   122  }
   123  
   124  func Test_RequiredWithOut(t *testing.T) {
   125  	gtest.Case(t, func() {
   126  		rule := "required-without:id,name"
   127  		val1 := ""
   128  		params1 := g.Map{
   129  			"age": 18,
   130  		}
   131  		params2 := g.Map{
   132  			"id": 100,
   133  		}
   134  		params3 := g.Map{
   135  			"id":   100,
   136  			"name": "john",
   137  		}
   138  		err1 := gvalid.Check(val1, rule, nil, params1)
   139  		err2 := gvalid.Check(val1, rule, nil, params2)
   140  		err3 := gvalid.Check(val1, rule, nil, params3)
   141  		gtest.AssertNE(err1, nil)
   142  		gtest.AssertNE(err2, nil)
   143  		gtest.Assert(err3, nil)
   144  	})
   145  }
   146  
   147  func Test_RequiredWithOutAll(t *testing.T) {
   148  	gtest.Case(t, func() {
   149  		rule := "required-without-all:id,name"
   150  		val1 := ""
   151  		params1 := g.Map{
   152  			"age": 18,
   153  		}
   154  		params2 := g.Map{
   155  			"id": 100,
   156  		}
   157  		params3 := g.Map{
   158  			"id":   100,
   159  			"name": "john",
   160  		}
   161  		err1 := gvalid.Check(val1, rule, nil, params1)
   162  		err2 := gvalid.Check(val1, rule, nil, params2)
   163  		err3 := gvalid.Check(val1, rule, nil, params3)
   164  		gtest.AssertNE(err1, nil)
   165  		gtest.Assert(err2, nil)
   166  		gtest.Assert(err3, nil)
   167  	})
   168  }
   169  
   170  func Test_Date(t *testing.T) {
   171  	gtest.Case(t, func() {
   172  		rule := "date"
   173  		val1 := "2010"
   174  		val2 := "201011"
   175  		val3 := "20101101"
   176  		val4 := "2010-11-01"
   177  		val5 := "2010.11.01"
   178  		val6 := "2010/11/01"
   179  		err1 := gvalid.Check(val1, rule, nil)
   180  		err2 := gvalid.Check(val2, rule, nil)
   181  		err3 := gvalid.Check(val3, rule, nil)
   182  		err4 := gvalid.Check(val4, rule, nil)
   183  		err5 := gvalid.Check(val5, rule, nil)
   184  		err6 := gvalid.Check(val6, rule, nil)
   185  		gtest.AssertNE(err1, nil)
   186  		gtest.AssertNE(err2, nil)
   187  		gtest.Assert(err3, nil)
   188  		gtest.Assert(err4, nil)
   189  		gtest.Assert(err5, nil)
   190  		gtest.Assert(err6, nil)
   191  	})
   192  }
   193  
   194  func Test_DateFormat(t *testing.T) {
   195  	gtest.Case(t, func() {
   196  		val1 := "2010"
   197  		val2 := "201011"
   198  		val3 := "2010.11"
   199  		val4 := "201011-01"
   200  		val5 := "2010~11~01"
   201  		val6 := "2010-11~01"
   202  		err1 := gvalid.Check(val1, "date-format:Y", nil)
   203  		err2 := gvalid.Check(val2, "date-format:Ym", nil)
   204  		err3 := gvalid.Check(val3, "date-format:Y.m", nil)
   205  		err4 := gvalid.Check(val4, "date-format:Ym-d", nil)
   206  		err5 := gvalid.Check(val5, "date-format:Y~m~d", nil)
   207  		err6 := gvalid.Check(val6, "date-format:Y~m~d", nil)
   208  		gtest.Assert(err1, nil)
   209  		gtest.Assert(err2, nil)
   210  		gtest.Assert(err3, nil)
   211  		gtest.Assert(err4, nil)
   212  		gtest.Assert(err5, nil)
   213  		gtest.AssertNE(err6, nil)
   214  	})
   215  }
   216  
   217  func Test_Email(t *testing.T) {
   218  	gtest.Case(t, func() {
   219  		rule := "email"
   220  		value1 := "m@johngcn"
   221  		value2 := "m@www@johngcn"
   222  		value3 := "m-m_m@mail.johng.cn"
   223  		value4 := "m.m-m@johng.cn"
   224  		err1 := gvalid.Check(value1, rule, nil)
   225  		err2 := gvalid.Check(value2, rule, nil)
   226  		err3 := gvalid.Check(value3, rule, nil)
   227  		err4 := gvalid.Check(value4, rule, nil)
   228  		gtest.AssertNE(err1, nil)
   229  		gtest.AssertNE(err2, nil)
   230  		gtest.Assert(err3, nil)
   231  		gtest.Assert(err4, nil)
   232  	})
   233  }
   234  
   235  func Test_Phone(t *testing.T) {
   236  	gtest.Case(t, func() {
   237  		err1 := gvalid.Check("1361990897", "phone", nil)
   238  		err2 := gvalid.Check("13619908979", "phone", nil)
   239  		err3 := gvalid.Check("16719908979", "phone", nil)
   240  		err4 := gvalid.Check("19719908989", "phone", nil)
   241  		gtest.AssertNE(err1, nil)
   242  		gtest.Assert(err2, nil)
   243  		gtest.Assert(err3, nil)
   244  		gtest.Assert(err4, nil)
   245  	})
   246  }
   247  
   248  func Test_Telephone(t *testing.T) {
   249  	gtest.Case(t, func() {
   250  		rule := "telephone"
   251  		val1 := "869265"
   252  		val2 := "028-869265"
   253  		val3 := "86292651"
   254  		val4 := "028-8692651"
   255  		val5 := "0830-8692651"
   256  		err1 := gvalid.Check(val1, rule, nil)
   257  		err2 := gvalid.Check(val2, rule, nil)
   258  		err3 := gvalid.Check(val3, rule, nil)
   259  		err4 := gvalid.Check(val4, rule, nil)
   260  		err5 := gvalid.Check(val5, rule, nil)
   261  		gtest.AssertNE(err1, nil)
   262  		gtest.AssertNE(err2, nil)
   263  		gtest.Assert(err3, nil)
   264  		gtest.Assert(err4, nil)
   265  		gtest.Assert(err5, nil)
   266  	})
   267  }
   268  
   269  func Test_Passport(t *testing.T) {
   270  	gtest.Case(t, func() {
   271  		rule := "passport"
   272  		val1 := "123456"
   273  		val2 := "a12345-6"
   274  		val3 := "aaaaa"
   275  		val4 := "aaaaaa"
   276  		val5 := "a123_456"
   277  		err1 := gvalid.Check(val1, rule, nil)
   278  		err2 := gvalid.Check(val2, rule, nil)
   279  		err3 := gvalid.Check(val3, rule, nil)
   280  		err4 := gvalid.Check(val4, rule, nil)
   281  		err5 := gvalid.Check(val5, rule, nil)
   282  		gtest.AssertNE(err1, nil)
   283  		gtest.AssertNE(err2, nil)
   284  		gtest.AssertNE(err3, nil)
   285  		gtest.Assert(err4, nil)
   286  		gtest.Assert(err5, nil)
   287  	})
   288  }
   289  
   290  func Test_Password(t *testing.T) {
   291  	gtest.Case(t, func() {
   292  		rule := "password"
   293  		val1 := "12345"
   294  		val2 := "aaaaa"
   295  		val3 := "a12345-6"
   296  		val4 := ">,/;'[09-"
   297  		val5 := "a123_456"
   298  		err1 := gvalid.Check(val1, rule, nil)
   299  		err2 := gvalid.Check(val2, rule, nil)
   300  		err3 := gvalid.Check(val3, rule, nil)
   301  		err4 := gvalid.Check(val4, rule, nil)
   302  		err5 := gvalid.Check(val5, rule, nil)
   303  		gtest.AssertNE(err1, nil)
   304  		gtest.AssertNE(err2, nil)
   305  		gtest.Assert(err3, nil)
   306  		gtest.Assert(err4, nil)
   307  		gtest.Assert(err5, nil)
   308  	})
   309  }
   310  
   311  func Test_Password2(t *testing.T) {
   312  	gtest.Case(t, func() {
   313  		rule := "password2"
   314  		val1 := "12345"
   315  		val2 := "Naaaa"
   316  		val3 := "a12345-6"
   317  		val4 := ">,/;'[09-"
   318  		val5 := "a123_456"
   319  		val6 := "Nant1986"
   320  		val7 := "Nant1986!"
   321  		err1 := gvalid.Check(val1, rule, nil)
   322  		err2 := gvalid.Check(val2, rule, nil)
   323  		err3 := gvalid.Check(val3, rule, nil)
   324  		err4 := gvalid.Check(val4, rule, nil)
   325  		err5 := gvalid.Check(val5, rule, nil)
   326  		err6 := gvalid.Check(val6, rule, nil)
   327  		err7 := gvalid.Check(val7, rule, nil)
   328  		gtest.AssertNE(err1, nil)
   329  		gtest.AssertNE(err2, nil)
   330  		gtest.AssertNE(err3, nil)
   331  		gtest.AssertNE(err4, nil)
   332  		gtest.AssertNE(err5, nil)
   333  		gtest.Assert(err6, nil)
   334  		gtest.Assert(err7, nil)
   335  	})
   336  }
   337  
   338  func Test_Password3(t *testing.T) {
   339  	gtest.Case(t, func() {
   340  		rule := "password3"
   341  		val1 := "12345"
   342  		val2 := "Naaaa"
   343  		val3 := "a12345-6"
   344  		val4 := ">,/;'[09-"
   345  		val5 := "a123_456"
   346  		val6 := "Nant1986"
   347  		val7 := "Nant1986!"
   348  		err1 := gvalid.Check(val1, rule, nil)
   349  		err2 := gvalid.Check(val2, rule, nil)
   350  		err3 := gvalid.Check(val3, rule, nil)
   351  		err4 := gvalid.Check(val4, rule, nil)
   352  		err5 := gvalid.Check(val5, rule, nil)
   353  		err6 := gvalid.Check(val6, rule, nil)
   354  		err7 := gvalid.Check(val7, rule, nil)
   355  		gtest.AssertNE(err1, nil)
   356  		gtest.AssertNE(err2, nil)
   357  		gtest.AssertNE(err3, nil)
   358  		gtest.AssertNE(err4, nil)
   359  		gtest.AssertNE(err5, nil)
   360  		gtest.AssertNE(err6, nil)
   361  		gtest.Assert(err7, nil)
   362  	})
   363  }
   364  
   365  func Test_Postcode(t *testing.T) {
   366  	gtest.Case(t, func() {
   367  		rule := "postcode"
   368  		val1 := "12345"
   369  		val2 := "610036"
   370  		err1 := gvalid.Check(val1, rule, nil)
   371  		err2 := gvalid.Check(val2, rule, nil)
   372  		gtest.AssertNE(err1, nil)
   373  		gtest.Assert(err2, nil)
   374  	})
   375  }
   376  
   377  func Test_IDNumber(t *testing.T) {
   378  	gtest.Case(t, func() {
   379  		rule := "id-number"
   380  		val1 := "11111111111111"
   381  		val2 := "1111111111111111"
   382  		val3 := "311128500121201"
   383  		val4 := "510521198607185367"
   384  		val5 := "51052119860718536x"
   385  		err1 := gvalid.Check(val1, rule, nil)
   386  		err2 := gvalid.Check(val2, rule, nil)
   387  		err3 := gvalid.Check(val3, rule, nil)
   388  		err4 := gvalid.Check(val4, rule, nil)
   389  		err5 := gvalid.Check(val5, rule, nil)
   390  		gtest.AssertNE(err1, nil)
   391  		gtest.AssertNE(err2, nil)
   392  		gtest.Assert(err3, nil)
   393  		gtest.Assert(err4, nil)
   394  		gtest.Assert(err5, nil)
   395  	})
   396  }
   397  
   398  func Test_QQ(t *testing.T) {
   399  	gtest.Case(t, func() {
   400  		rule := "qq"
   401  		val1 := "100"
   402  		val2 := "1"
   403  		val3 := "10000"
   404  		val4 := "38996181"
   405  		val5 := "389961817"
   406  		err1 := gvalid.Check(val1, rule, nil)
   407  		err2 := gvalid.Check(val2, rule, nil)
   408  		err3 := gvalid.Check(val3, rule, nil)
   409  		err4 := gvalid.Check(val4, rule, nil)
   410  		err5 := gvalid.Check(val5, rule, nil)
   411  		gtest.AssertNE(err1, nil)
   412  		gtest.AssertNE(err2, nil)
   413  		gtest.Assert(err3, nil)
   414  		gtest.Assert(err4, nil)
   415  		gtest.Assert(err5, nil)
   416  	})
   417  }
   418  
   419  func Test_Ip(t *testing.T) {
   420  	if m := gvalid.Check("10.0.0.1", "ip", nil); m != nil {
   421  		t.Error(m)
   422  	}
   423  	if m := gvalid.Check("10.0.0.1", "ipv4", nil); m != nil {
   424  		t.Error(m)
   425  	}
   426  	if m := gvalid.Check("0.0.0.0", "ipv4", nil); m != nil {
   427  		t.Error(m)
   428  	}
   429  	if m := gvalid.Check("1920.0.0.0", "ipv4", nil); m == nil {
   430  		t.Error("ipv4校验失败")
   431  	}
   432  	if m := gvalid.Check("1920.0.0.0", "ip", nil); m == nil {
   433  		t.Error("ipv4校验失败")
   434  	}
   435  	if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ipv6", nil); m != nil {
   436  		t.Error(m)
   437  	}
   438  	if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ipv6", nil); m == nil {
   439  		t.Error(m)
   440  	}
   441  	if m := gvalid.Check("fe80::5484:7aff:fefe:9799", "ip", nil); m != nil {
   442  		t.Error(m)
   443  	}
   444  	if m := gvalid.Check("fe80::5484:7aff:fefe:9799123", "ip", nil); m == nil {
   445  		t.Error(m)
   446  	}
   447  }
   448  
   449  func Test_IPv4(t *testing.T) {
   450  	gtest.Case(t, func() {
   451  		rule := "ipv4"
   452  		val1 := "0.0.0"
   453  		val2 := "0.0.0.0"
   454  		val3 := "1.1.1.1"
   455  		val4 := "255.255.255.0"
   456  		val5 := "127.0.0.1"
   457  		err1 := gvalid.Check(val1, rule, nil)
   458  		err2 := gvalid.Check(val2, rule, nil)
   459  		err3 := gvalid.Check(val3, rule, nil)
   460  		err4 := gvalid.Check(val4, rule, nil)
   461  		err5 := gvalid.Check(val5, rule, nil)
   462  		gtest.AssertNE(err1, nil)
   463  		gtest.Assert(err2, nil)
   464  		gtest.Assert(err3, nil)
   465  		gtest.Assert(err4, nil)
   466  		gtest.Assert(err5, nil)
   467  	})
   468  }
   469  
   470  func Test_IPv6(t *testing.T) {
   471  	gtest.Case(t, func() {
   472  		rule := "ipv6"
   473  		val1 := "192.168.1.1"
   474  		val2 := "CDCD:910A:2222:5498:8475:1111:3900:2020"
   475  		val3 := "1030::C9B4:FF12:48AA:1A2B"
   476  		val4 := "2000:0:0:0:0:0:0:1"
   477  		val5 := "0000:0000:0000:0000:0000:ffff:c0a8:5909"
   478  		err1 := gvalid.Check(val1, rule, nil)
   479  		err2 := gvalid.Check(val2, rule, nil)
   480  		err3 := gvalid.Check(val3, rule, nil)
   481  		err4 := gvalid.Check(val4, rule, nil)
   482  		err5 := gvalid.Check(val5, rule, nil)
   483  		gtest.AssertNE(err1, nil)
   484  		gtest.Assert(err2, nil)
   485  		gtest.Assert(err3, nil)
   486  		gtest.Assert(err4, nil)
   487  		gtest.Assert(err5, nil)
   488  	})
   489  }
   490  
   491  func Test_MAC(t *testing.T) {
   492  	gtest.Case(t, func() {
   493  		rule := "mac"
   494  		val1 := "192.168.1.1"
   495  		val2 := "44-45-53-54-00-00"
   496  		val3 := "01:00:5e:00:00:00"
   497  		err1 := gvalid.Check(val1, rule, nil)
   498  		err2 := gvalid.Check(val2, rule, nil)
   499  		err3 := gvalid.Check(val3, rule, nil)
   500  		gtest.AssertNE(err1, nil)
   501  		gtest.Assert(err2, nil)
   502  		gtest.Assert(err3, nil)
   503  	})
   504  }
   505  
   506  func Test_URL(t *testing.T) {
   507  	gtest.Case(t, func() {
   508  		rule := "url"
   509  		val1 := "127.0.0.1"
   510  		val2 := "https://www.baidu.com"
   511  		val3 := "http://127.0.0.1"
   512  		val4 := "file:///tmp/test.txt"
   513  		err1 := gvalid.Check(val1, rule, nil)
   514  		err2 := gvalid.Check(val2, rule, nil)
   515  		err3 := gvalid.Check(val3, rule, nil)
   516  		err4 := gvalid.Check(val4, rule, nil)
   517  		gtest.AssertNE(err1, nil)
   518  		gtest.Assert(err2, nil)
   519  		gtest.Assert(err3, nil)
   520  		gtest.Assert(err4, nil)
   521  	})
   522  }
   523  
   524  func Test_Domain(t *testing.T) {
   525  	gtest.Case(t, func() {
   526  		rule := "domain"
   527  		val1 := "localhost"
   528  		val2 := "baidu.com"
   529  		val3 := "www.baidu.com"
   530  		val4 := "jn.np"
   531  		val5 := "www.jn.np"
   532  		val6 := "w.www.jn.np"
   533  		err1 := gvalid.Check(val1, rule, nil)
   534  		err2 := gvalid.Check(val2, rule, nil)
   535  		err3 := gvalid.Check(val3, rule, nil)
   536  		err4 := gvalid.Check(val4, rule, nil)
   537  		err5 := gvalid.Check(val5, rule, nil)
   538  		err6 := gvalid.Check(val6, rule, nil)
   539  		gtest.AssertNE(err1, nil)
   540  		gtest.Assert(err2, nil)
   541  		gtest.Assert(err3, nil)
   542  		gtest.Assert(err4, nil)
   543  		gtest.Assert(err5, nil)
   544  		gtest.Assert(err6, nil)
   545  	})
   546  }
   547  
   548  func Test_Length(t *testing.T) {
   549  	rule := "length:6,16"
   550  	if m := gvalid.Check("123456", rule, nil); m != nil {
   551  		t.Error(m)
   552  	}
   553  	if m := gvalid.Check("12345", rule, nil); m == nil {
   554  		t.Error("长度校验失败")
   555  	}
   556  }
   557  
   558  func Test_MinLength(t *testing.T) {
   559  	rule := "min-length:6"
   560  	msgs := map[string]string{
   561  		"min-length": "地址长度至少为:min位",
   562  	}
   563  	if m := gvalid.Check("123456", rule, nil); m != nil {
   564  		t.Error(m)
   565  	}
   566  	if m := gvalid.Check("12345", rule, nil); m == nil {
   567  		t.Error("长度校验失败")
   568  	}
   569  	if m := gvalid.Check("12345", rule, msgs); m == nil {
   570  		t.Error("长度校验失败")
   571  	}
   572  
   573  	rule2 := "min-length:abc"
   574  	if m := gvalid.Check("123456", rule2, nil); m == nil {
   575  		t.Error("长度校验失败")
   576  	}
   577  }
   578  
   579  func Test_MaxLength(t *testing.T) {
   580  	rule := "max-length:6"
   581  	msgs := map[string]string{
   582  		"max-length": "地址长度至大为:max位",
   583  	}
   584  	if m := gvalid.Check("12345", rule, nil); m != nil {
   585  		t.Error(m)
   586  	}
   587  	if m := gvalid.Check("1234567", rule, nil); m == nil {
   588  		t.Error("长度校验失败")
   589  	}
   590  	if m := gvalid.Check("1234567", rule, msgs); m == nil {
   591  		t.Error("长度校验失败")
   592  	}
   593  
   594  	rule2 := "max-length:abc"
   595  	if m := gvalid.Check("123456", rule2, nil); m == nil {
   596  		t.Error("长度校验失败")
   597  	}
   598  }
   599  
   600  func Test_Between(t *testing.T) {
   601  	rule := "between:6.01, 10.01"
   602  	if m := gvalid.Check(10, rule, nil); m != nil {
   603  		t.Error(m)
   604  	}
   605  	if m := gvalid.Check(10.02, rule, nil); m == nil {
   606  		t.Error("大小范围校验失败")
   607  	}
   608  	if m := gvalid.Check("a", rule, nil); m == nil {
   609  		t.Error("大小范围校验失败")
   610  	}
   611  }
   612  
   613  func Test_Min(t *testing.T) {
   614  	gtest.Case(t, func() {
   615  		rule := "min:100"
   616  		val1 := "1"
   617  		val2 := "99"
   618  		val3 := "100"
   619  		val4 := "1000"
   620  		val5 := "a"
   621  		err1 := gvalid.Check(val1, rule, nil)
   622  		err2 := gvalid.Check(val2, rule, nil)
   623  		err3 := gvalid.Check(val3, rule, nil)
   624  		err4 := gvalid.Check(val4, rule, nil)
   625  		err5 := gvalid.Check(val5, rule, nil)
   626  		gtest.AssertNE(err1, nil)
   627  		gtest.AssertNE(err2, nil)
   628  		gtest.Assert(err3, nil)
   629  		gtest.Assert(err4, nil)
   630  		gtest.AssertNE(err5, nil)
   631  
   632  		rule2 := "min:a"
   633  		err6 := gvalid.Check(val1, rule2, nil)
   634  		gtest.AssertNE(err6, nil)
   635  	})
   636  }
   637  
   638  func Test_Max(t *testing.T) {
   639  	gtest.Case(t, func() {
   640  		rule := "max:100"
   641  		val1 := "1"
   642  		val2 := "99"
   643  		val3 := "100"
   644  		val4 := "1000"
   645  		val5 := "a"
   646  		err1 := gvalid.Check(val1, rule, nil)
   647  		err2 := gvalid.Check(val2, rule, nil)
   648  		err3 := gvalid.Check(val3, rule, nil)
   649  		err4 := gvalid.Check(val4, rule, nil)
   650  		err5 := gvalid.Check(val5, rule, nil)
   651  		gtest.Assert(err1, nil)
   652  		gtest.Assert(err2, nil)
   653  		gtest.Assert(err3, nil)
   654  		gtest.AssertNE(err4, nil)
   655  		gtest.AssertNE(err5, nil)
   656  
   657  		rule2 := "max:a"
   658  		err6 := gvalid.Check(val1, rule2, nil)
   659  		gtest.AssertNE(err6, nil)
   660  	})
   661  }
   662  
   663  func Test_Json(t *testing.T) {
   664  	gtest.Case(t, func() {
   665  		rule := "json"
   666  		val1 := ""
   667  		val2 := "."
   668  		val3 := "{}"
   669  		val4 := "[]"
   670  		val5 := "[1,2,3,4]"
   671  		val6 := `{"list":[1,2,3,4]}`
   672  		err1 := gvalid.Check(val1, rule, nil)
   673  		err2 := gvalid.Check(val2, rule, nil)
   674  		err3 := gvalid.Check(val3, rule, nil)
   675  		err4 := gvalid.Check(val4, rule, nil)
   676  		err5 := gvalid.Check(val5, rule, nil)
   677  		err6 := gvalid.Check(val6, rule, nil)
   678  		gtest.AssertNE(err1, nil)
   679  		gtest.AssertNE(err2, nil)
   680  		gtest.Assert(err3, nil)
   681  		gtest.Assert(err4, nil)
   682  		gtest.Assert(err5, nil)
   683  		gtest.Assert(err6, nil)
   684  	})
   685  }
   686  
   687  func Test_Integer(t *testing.T) {
   688  	gtest.Case(t, func() {
   689  		rule := "integer"
   690  		val1 := ""
   691  		val2 := "1.0"
   692  		val3 := "001"
   693  		val4 := "1"
   694  		val5 := "100"
   695  		val6 := `999999999`
   696  		err1 := gvalid.Check(val1, rule, nil)
   697  		err2 := gvalid.Check(val2, rule, nil)
   698  		err3 := gvalid.Check(val3, rule, nil)
   699  		err4 := gvalid.Check(val4, rule, nil)
   700  		err5 := gvalid.Check(val5, rule, nil)
   701  		err6 := gvalid.Check(val6, rule, nil)
   702  		gtest.AssertNE(err1, nil)
   703  		gtest.AssertNE(err2, nil)
   704  		gtest.Assert(err3, nil)
   705  		gtest.Assert(err4, nil)
   706  		gtest.Assert(err5, nil)
   707  		gtest.Assert(err6, nil)
   708  	})
   709  }
   710  
   711  func Test_Float(t *testing.T) {
   712  	gtest.Case(t, func() {
   713  		rule := "float"
   714  		val1 := ""
   715  		val2 := "a"
   716  		val3 := "1"
   717  		val4 := "1.0"
   718  		val5 := "1.1"
   719  		val6 := `0.1`
   720  		err1 := gvalid.Check(val1, rule, nil)
   721  		err2 := gvalid.Check(val2, rule, nil)
   722  		err3 := gvalid.Check(val3, rule, nil)
   723  		err4 := gvalid.Check(val4, rule, nil)
   724  		err5 := gvalid.Check(val5, rule, nil)
   725  		err6 := gvalid.Check(val6, rule, nil)
   726  		gtest.AssertNE(err1, nil)
   727  		gtest.AssertNE(err2, nil)
   728  		gtest.Assert(err3, nil)
   729  		gtest.Assert(err4, nil)
   730  		gtest.Assert(err5, nil)
   731  		gtest.Assert(err6, nil)
   732  	})
   733  }
   734  
   735  func Test_Boolean(t *testing.T) {
   736  	gtest.Case(t, func() {
   737  		rule := "boolean"
   738  		val1 := "a"
   739  		val2 := "-"
   740  		val3 := ""
   741  		val4 := "1"
   742  		val5 := "true"
   743  		val6 := `off`
   744  		err1 := gvalid.Check(val1, rule, nil)
   745  		err2 := gvalid.Check(val2, rule, nil)
   746  		err3 := gvalid.Check(val3, rule, nil)
   747  		err4 := gvalid.Check(val4, rule, nil)
   748  		err5 := gvalid.Check(val5, rule, nil)
   749  		err6 := gvalid.Check(val6, rule, nil)
   750  		gtest.AssertNE(err1, nil)
   751  		gtest.AssertNE(err2, nil)
   752  		gtest.Assert(err3, nil)
   753  		gtest.Assert(err4, nil)
   754  		gtest.Assert(err5, nil)
   755  		gtest.Assert(err6, nil)
   756  	})
   757  }
   758  
   759  func Test_Same(t *testing.T) {
   760  	gtest.Case(t, func() {
   761  		rule := "same:id"
   762  		val1 := "100"
   763  		params1 := g.Map{
   764  			"age": 18,
   765  		}
   766  		params2 := g.Map{
   767  			"id": 100,
   768  		}
   769  		params3 := g.Map{
   770  			"id":   100,
   771  			"name": "john",
   772  		}
   773  		err1 := gvalid.Check(val1, rule, nil, params1)
   774  		err2 := gvalid.Check(val1, rule, nil, params2)
   775  		err3 := gvalid.Check(val1, rule, nil, params3)
   776  		gtest.AssertNE(err1, nil)
   777  		gtest.Assert(err2, nil)
   778  		gtest.Assert(err3, nil)
   779  	})
   780  }
   781  
   782  func Test_Different(t *testing.T) {
   783  	gtest.Case(t, func() {
   784  		rule := "different:id"
   785  		val1 := "100"
   786  		params1 := g.Map{
   787  			"age": 18,
   788  		}
   789  		params2 := g.Map{
   790  			"id": 100,
   791  		}
   792  		params3 := g.Map{
   793  			"id":   100,
   794  			"name": "john",
   795  		}
   796  		err1 := gvalid.Check(val1, rule, nil, params1)
   797  		err2 := gvalid.Check(val1, rule, nil, params2)
   798  		err3 := gvalid.Check(val1, rule, nil, params3)
   799  		gtest.Assert(err1, nil)
   800  		gtest.AssertNE(err2, nil)
   801  		gtest.AssertNE(err3, nil)
   802  	})
   803  }
   804  
   805  func Test_In(t *testing.T) {
   806  	gtest.Case(t, func() {
   807  		rule := "in:100,200"
   808  		val1 := ""
   809  		val2 := "1"
   810  		val3 := "100"
   811  		val4 := "200"
   812  		err1 := gvalid.Check(val1, rule, nil)
   813  		err2 := gvalid.Check(val2, rule, nil)
   814  		err3 := gvalid.Check(val3, rule, nil)
   815  		err4 := gvalid.Check(val4, rule, nil)
   816  		gtest.AssertNE(err1, nil)
   817  		gtest.AssertNE(err2, nil)
   818  		gtest.Assert(err3, nil)
   819  		gtest.Assert(err4, nil)
   820  	})
   821  }
   822  
   823  func Test_NotIn(t *testing.T) {
   824  	gtest.Case(t, func() {
   825  		rule := "not-in:100,200"
   826  		val1 := ""
   827  		val2 := "1"
   828  		val3 := "100"
   829  		val4 := "200"
   830  		err1 := gvalid.Check(val1, rule, nil)
   831  		err2 := gvalid.Check(val2, rule, nil)
   832  		err3 := gvalid.Check(val3, rule, nil)
   833  		err4 := gvalid.Check(val4, rule, nil)
   834  		gtest.Assert(err1, nil)
   835  		gtest.Assert(err2, nil)
   836  		gtest.AssertNE(err3, nil)
   837  		gtest.AssertNE(err4, nil)
   838  	})
   839  }
   840  
   841  func Test_Regex1(t *testing.T) {
   842  	rule := `regex:\d{6}|\D{6}|length:6,16`
   843  	if m := gvalid.Check("123456", rule, nil); m != nil {
   844  		t.Error(m)
   845  	}
   846  	if m := gvalid.Check("abcde6", rule, nil); m == nil {
   847  		t.Error("校验失败")
   848  	}
   849  }
   850  
   851  func Test_Regex2(t *testing.T) {
   852  	gtest.Case(t, func() {
   853  		rule := `required|min-length:6|regex:^data:image\/(jpeg|png);base64,`
   854  		str1 := ""
   855  		str2 := "data"
   856  		str3 := "data:image/jpeg;base64,/9jrbattq22r"
   857  		err1 := gvalid.Check(str1, rule, nil)
   858  		err2 := gvalid.Check(str2, rule, nil)
   859  		err3 := gvalid.Check(str3, rule, nil)
   860  		gtest.AssertNE(err1, nil)
   861  		gtest.AssertNE(err2, nil)
   862  		gtest.Assert(err3, nil)
   863  
   864  		gtest.AssertNE(err1.Map()["required"], nil)
   865  		gtest.AssertNE(err2.Map()["min-length"], nil)
   866  	})
   867  }